From f4bf66ea1b7bb0f6fef442371fa42e1d68560a21 Mon Sep 17 00:00:00 2001 From: yash Date: Thu, 18 Jan 2024 19:48:27 +0300 Subject: [PATCH] add migrator --- .env | 8 ++ Makefile | 6 + cmd/migrator/main.go | 59 ++++++++++ cmd/recipes/main.go | 4 + config/local.yaml | 7 +- docker-compose.yml | 107 ++++++++++++++++++ go.mod | 11 +- go.sum | 49 +++++++- internal/cache_provider/cache.go | 1 + .../redis/redis.go | 4 +- internal/config/config.go | 25 ++-- internal/media_storage/media_storage.go | 1 + .../{storage => media_storage}/minio/minio.go | 6 +- internal/storage/postgresql/postgresql.go | 14 ++- internal/storage/storage.go | 15 --- migrations/000001_init.down.sql | 6 + migrations/000001_init.up.sql | 37 ++++++ 17 files changed, 316 insertions(+), 44 deletions(-) create mode 100644 .env create mode 100644 cmd/migrator/main.go create mode 100644 docker-compose.yml create mode 100644 internal/cache_provider/cache.go rename internal/{storage => cache_provider}/redis/redis.go (73%) create mode 100644 internal/media_storage/media_storage.go rename internal/{storage => media_storage}/minio/minio.go (62%) create mode 100644 migrations/000001_init.down.sql create mode 100644 migrations/000001_init.up.sql diff --git a/.env b/.env new file mode 100644 index 0000000..fd5246c --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +DB_NAME=pguser +DB_USER=pguser +DB_PASSWORD=password + +MINIO_ROOT_USER=user +MINIO_ROOT_PASSWORD=password + +RDB_PASSWORD=password diff --git a/Makefile b/Makefile index 5081d7c..826be4d 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,8 @@ run: go run ./cmd/recipes/main.go -config ./config/local.yaml +migrate_create: + migrate create -ext sql -dir ./migrations -seq ${NAME} +migrate_up: + CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations +migrate_down: + CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations --down diff --git a/cmd/migrator/main.go b/cmd/migrator/main.go new file mode 100644 index 0000000..442c321 --- /dev/null +++ b/cmd/migrator/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "recipes/internal/config" + + "github.com/golang-migrate/migrate/v4" + _ "github.com/golang-migrate/migrate/v4/database/postgres" + _ "github.com/golang-migrate/migrate/v4/source/file" +) + +func main() { + var migrationsPath string + var down bool + + flag.StringVar(&migrationsPath, "migrations-path", "", "path to migrations") + flag.BoolVar(&down, "down", false, "") + // flag.StringVar(&configFile, "config", "", "config file location") + + flag.Parse() + + if migrationsPath == "" { + panic("migrations-path is required") + } + + // load config + cfg := config.MustLoad() + + m, err := migrate.New("file://"+migrationsPath, fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", cfg.Postgresql.User, cfg.Postgresql.Password, cfg.Postgresql.Address, cfg.Postgresql.DBName)) + if err != nil { + panic(err) + } + + if down { + if err := m.Down(); err != nil { + if errors.Is(err, migrate.ErrNoChange) { + fmt.Println("no migrations downed") + return + } + + panic(err) + } + fmt.Println("migrations downed successfully") + return + } + + if err := m.Up(); err != nil { + if errors.Is(err, migrate.ErrNoChange) { + fmt.Println("no migrations apply") + return + } + + panic(err) + } + + fmt.Println("migrations applied successfully") +} diff --git a/cmd/recipes/main.go b/cmd/recipes/main.go index b18f797..5a439b0 100644 --- a/cmd/recipes/main.go +++ b/cmd/recipes/main.go @@ -25,6 +25,10 @@ func main() { log.Debug("Application config", slog.Any("config", fmt.Sprintf("%+v", *cfg))) // init storage + // init cache + + // init media storage + // init app // graceful shutdown diff --git a/config/local.yaml b/config/local.yaml index b7bf15a..7cd2644 100644 --- a/config/local.yaml +++ b/config/local.yaml @@ -6,13 +6,12 @@ http-server: idle_timeout: 60s postgresql: - db_name: "user" - user: "user" + db_name: "pguser" + user: "pguser" password: "password" - address: "127.0.0.1:5432" + address: "192.168.4.4:5432" redis: - user: "user" password: "password" address: "127.0.0.1:6379" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..85517e7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,107 @@ +version: '3' + +services: +# # traefik +# traefik: +# image: "traefik:latest" +# network_mode: "host" +# container_name: "traefik" +# ports: +# - "80:80" +# - "443:443" +# volumes: +# - ./conf/traefik:/etc/traefik +# - ./conf/traefik_custom:/custom/:ro +# - /var/run/docker.sock:/var/run/docker.sock:ro + + # # rest api + # app: + # build: + # context: . + # dockerfile: Dockerfile + # container_name: app + # restart: unless-stopped + # env_file: .env + # environment: + # DB_ADDR: app_pg:5432 + # MINIO_ADDR: minio:9000 + # depends_on: + # - app_pg + # labels: + # - "traefik.enable=true" + # - "traefik.http.routers.app.entrypoints=websecure" + # - "traefik.http.routers.app.tls=true" + # - "traefik.http.routers.app.tls.certresolver=production" + # - "traefik.http.routers.app.rule=Host(`$API_HOST`) && PathPrefix(`/api/v1/`)" + # networks: + # microservicesnet: + # ipv4_address: 192.168.4.3 + + # postgresql database for app + app_pg: + image: postgres:16-alpine + container_name: app_pg + restart: unless-stopped + env_file: .env + environment: + POSTGRES_DB: $DB_NAME + POSTGRES_USER: $DB_USER + POSTGRES_PASSWORD: $DB_PASSWORD + PGDATA: "/var/lib/postgresql/data/pgdata" + volumes: + - ../docker_data/recipes2_data/pg/pg_data:/var/lib/postgresql/data + networks: + microservicesnet: + ipv4_address: 192.168.4.4 + + # minio + minio: + image: bitnami/minio:latest + container_name: minio + restart: unless-stopped + env_file: .env + environment: + - MINIO_ROOT_USER=$MINIO_ROOT_USER + - MINIO_ROOT_PASSWORD=$MINIO_ROOT_PASSWORD + volumes: + - ../docker_data/recipes2_data/minio:/bitnami/minio/data + ports: + - '9000:9000' + - '9001:9001' + networks: + microservicesnet: + ipv4_address: 192.168.4.5 + + redis: + image: redis:7.2-alpine + container_name: redis + restart: unless-stopped + env_file: .env + command: redis-server --save 20 1 --loglevel warning --requirepass $RDB_PASSWORD + volumes: + - ../docker_data/recipes2_data/cache:/date + networks: + microservicesnet: + ipv4_address: 192.168.4.7 + + # # frontend + # frontend_service: + # image: registry.computernetthings.ru/parallel/frontend:latest + # container_name: frontend_service + # restart: unless-stopped + # labels: + # - "traefik.enable=true" + # - "traefik.http.routers.frontend_service.entrypoints=websecure" + # - "traefik.http.routers.frontend_service.tls=true" + # - "traefik.http.routers.frontend_service.tls.certresolver=production" + # - "traefik.http.routers.frontend_service.rule=Host(`$FRONTEND_HOST`)" + # networks: + # microservicesnet: + # ipv4_address: 192.168.4.6 + +networks: + microservicesnet: + ipam: + driver: default + config: + - subnet: "192.168.4.0/24" \ No newline at end of file diff --git a/go.mod b/go.mod index d47b392..a4e192b 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,11 @@ go 1.21.5 require ( github.com/charmbracelet/log v0.3.1 + github.com/golang-migrate/migrate/v4 v4.17.0 github.com/ilyakaznacheev/cleanenv v1.5.0 github.com/jackc/pgx/v5 v5.5.2 + github.com/minio/minio-go/v7 v7.0.66 + github.com/redis/go-redis/v9 v9.4.0 ) require ( @@ -17,6 +20,8 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/google/uuid v1.5.0 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect @@ -25,25 +30,25 @@ require ( github.com/klauspost/compress v1.17.4 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/kr/text v0.2.0 // indirect + github.com/lib/pq v1.10.9 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.18 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/minio/md5-simd v1.1.2 // indirect - github.com/minio/minio-go/v7 v7.0.66 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect - github.com/redis/go-redis/v9 v9.4.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rs/xid v1.5.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect + go.uber.org/atomic v1.7.0 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect golang.org/x/net v0.19.0 // indirect - golang.org/x/sync v0.1.0 // indirect + golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 58d94a4..c14e2c8 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,15 @@ +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg= @@ -14,13 +22,32 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/dhui/dktest v0.4.0 h1:z05UmuXZHO/bgj/ds2bGMBu8FI4WA+Ag/m3ghL+om7M= +github.com/dhui/dktest v0.4.0/go.mod h1:v/Dbz1LgCBOi2Uki2nUqLBGa83hWBGFMu5MrgMDCc78= +github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8= +github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM= +github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang-migrate/migrate/v4 v4.17.0 h1:rd40H3QXU0AA4IoLllFcEAEo9dYKRHYND2gB4p7xcaU= +github.com/golang-migrate/migrate/v4 v4.17.0/go.mod h1:+Cp2mtLP4/aXDTKb9wmXYitdrNx2HGs45rbWAo6OsKM= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -44,6 +71,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= @@ -57,15 +86,25 @@ github.com/minio/minio-go/v7 v7.0.66 h1:bnTOXOHjOqv/gcMuiVbN9o2ngRItvqE774dG9nq0 github.com/minio/minio-go/v7 v7.0.66/go.mod h1:DHAgmyQEGdW3Cif0UooKOyrT3Vxs82zNdV6tkKhRtbs= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= +github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= +github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= +github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk= @@ -84,14 +123,18 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -99,6 +142,8 @@ golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/internal/cache_provider/cache.go b/internal/cache_provider/cache.go new file mode 100644 index 0000000..452644d --- /dev/null +++ b/internal/cache_provider/cache.go @@ -0,0 +1 @@ +package cache_provider diff --git a/internal/storage/redis/redis.go b/internal/cache_provider/redis/redis.go similarity index 73% rename from internal/storage/redis/redis.go rename to internal/cache_provider/redis/redis.go index 89ef79f..2d17d70 100644 --- a/internal/storage/redis/redis.go +++ b/internal/cache_provider/redis/redis.go @@ -8,6 +8,6 @@ type Cache struct { rdb *redis.Client } -func New() Cache { - +func New() *Cache { + return &Cache{} } diff --git a/internal/config/config.go b/internal/config/config.go index 02de88b..f41a7b3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,25 +12,24 @@ import ( type Config struct { Env string `yaml:"env" env-required:"true"` HTTPServer struct { - Address string `yaml:"address"` - Timeout time.Duration `yaml:"timeout"` - IdleTimeout time.Duration `yaml:"idle_timeout"` + Address string `yaml:"address" env-required:"true"` + Timeout time.Duration `yaml:"timeout" env-default:"4s"` + IdleTimeout time.Duration `yaml:"idle_timeout" env-default:"60s"` } `yaml:"http-server"` Postgresql struct { - DBName string `yaml:"db_name"` - User string `yaml:"user"` - Password string `yaml:"password"` - Address string `yaml:"address"` + DBName string `yaml:"db_name" env-required:"true"` + User string `yaml:"user" env-required:"true"` + Password string `yaml:"password" env-required:"true"` + Address string `yaml:"address" env-required:"true"` } `yaml:"postgresql"` Redis struct { - User string `yaml:"user"` - Password string `yaml:"password"` - Address string `yaml:"address"` + Password string `yaml:"password" env-required:"true"` + Address string `yaml:"address" env-required:"true"` } `yaml:"redis"` Minio struct { - User string `yaml:"user"` - Password string `yaml:"password"` - Address string `yaml:"address"` + User string `yaml:"user" env-required:"true"` + Password string `yaml:"password" env-required:"true"` + Address string `yaml:"address" env-required:"true"` } `yaml:"minio"` } diff --git a/internal/media_storage/media_storage.go b/internal/media_storage/media_storage.go new file mode 100644 index 0000000..3a2bd4d --- /dev/null +++ b/internal/media_storage/media_storage.go @@ -0,0 +1 @@ +package media_storage diff --git a/internal/storage/minio/minio.go b/internal/media_storage/minio/minio.go similarity index 62% rename from internal/storage/minio/minio.go rename to internal/media_storage/minio/minio.go index 134c9f9..02dabd5 100644 --- a/internal/storage/minio/minio.go +++ b/internal/media_storage/minio/minio.go @@ -5,10 +5,10 @@ import ( _ "github.com/minio/minio-go/v7" ) -type Storage struct { +type ObjStorage struct { minio *minio.Client } -func New() Storage { - +func New() *ObjStorage { + return &ObjStorage{} } diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index 60ba3e7..d278fd1 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -1,13 +1,23 @@ package postgresql import ( + "context" + "fmt" + "github.com/jackc/pgx/v5/pgxpool" ) type Database struct { - db *pgxpool.Conn + db *pgxpool.Pool } -func New() Database { +func New(user, password, addr, dbname string) (*Database, error) { + const op = "storage.postgresql.New" + pool, err := pgxpool.New(context.Background(), fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", user, password, addr, dbname)) + if err != nil { + return nil, fmt.Errorf("%s: %w", op, err) + } + + return &Database{db: pool}, nil } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index a39f5db..a2a6584 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -2,23 +2,8 @@ package storage import ( "errors" - "recipes/internal/storage/minio" - "recipes/internal/storage/postgresql" - "recipes/internal/storage/redis" ) var ( ErrNotFound = errors.New("not found") ) - -type Storage struct { - db postgresql.Database - cache redis.Cache - objStorage minio.Storage -} - -func New() *Storage { - var storage *Storage - - return storage -} diff --git a/migrations/000001_init.down.sql b/migrations/000001_init.down.sql new file mode 100644 index 0000000..ccde055 --- /dev/null +++ b/migrations/000001_init.down.sql @@ -0,0 +1,6 @@ +drop table if exists recipe; +drop table if exists recipe_ingredients_group; +drop table if exists recipe_ingredients; +drop table if exists recipe_steps; +drop table if exists recipe_advices; +drop table if exists recipe_categories; \ No newline at end of file diff --git a/migrations/000001_init.up.sql b/migrations/000001_init.up.sql new file mode 100644 index 0000000..96bf75e --- /dev/null +++ b/migrations/000001_init.up.sql @@ -0,0 +1,37 @@ +-- recipe +create table if not exists recipe ( + id serial primary key, + title varchar(60) not null UNIQUE, + description text not null default '', + image varchar(42) not null, + cooking_time varchar(16) not null, + servings smallint not null default 1, + cal varchar(16) not null +); +-- recipe ingredients group +create table if not exists recipe_ingredients_group ( + id serial primary key, + recipe_id integer references recipe(id) on delete cascade, + title varchar(26) default '' +); +-- recipe ingredients +create table if not exists recipe_ingredients ( + recipe_ingredients_group_id integer references recipe_ingredients_group(id) on delete cascade, + ingredient text not null +); +-- recipe steps +create table if not exists recipe_steps ( + recipe_id integer references recipe(id) on delete cascade, + step_num integer not null, + step_text text not null +); +-- recipe advices +create table if not exists recipe_advices ( + recipe_id integer references recipe(id) on delete cascade, + advice text +); +-- recipe categories +create table if not exists recipe_categories ( + recipe_id integer references recipe(id) on delete cascade, + category text +); \ No newline at end of file