diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..e115817 --- /dev/null +++ b/.env.template @@ -0,0 +1,6 @@ +APP_PORT=3003 +APP_ENV=development + +DB_URL=postgres://root:root@localhost:5432/johnjud_db + +SERVICE_FILE=localhost:3004 diff --git a/.gitignore b/.gitignore index e921962..572dda1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ config.yaml .env .env.prod +.env.file # idea .idea diff --git a/Makefile b/Makefile index bc834cc..0dce9f5 100644 --- a/Makefile +++ b/Makefile @@ -13,4 +13,4 @@ test: go tool cover -html=coverage.out -o coverage.html server: - go run ./src/. + . ./tools/export-env.sh ; go run ./src/. diff --git a/config/config.example.yaml b/config/config.example.yaml deleted file mode 100644 index 34bc114..0000000 --- a/config/config.example.yaml +++ /dev/null @@ -1,13 +0,0 @@ -app: - port: 3003 - debug: true - -database: - host: localhost - port: 5432 - name: johnjud_db - username: root - password: root - -service: - file: localhost:3004 \ No newline at end of file diff --git a/config/file/config.example.yaml b/config/file/config.example.yaml deleted file mode 100644 index f1ecd10..0000000 --- a/config/file/config.example.yaml +++ /dev/null @@ -1,14 +0,0 @@ -app: - port: 3004 - debug: true - -database: - host: local-db - port: 5432 - name: johnjud_db - username: root - password: root - -s3: - bucket_name: - region: \ No newline at end of file diff --git a/docker-compose-prod.yaml b/docker-compose-prod.yaml new file mode 100644 index 0000000..8d80e27 --- /dev/null +++ b/docker-compose-prod.yaml @@ -0,0 +1,55 @@ +version: "3.9" + +services: + backend: + container_name: johnjud-backend + restart: unless-stopped + build: . + ports: + - 3003:3003 + environment: + - APP_PORT=3003 + - APP_ENV=production + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - SERVICE_FILE=localhost:3004 + networks: + - johnjud + local-file: + image: ghcr.io/isd-sgcu/johnjud-file + container_name: johnjud-file + depends_on: + - local-db + restart: unless-stopped + environment: + - APP_PORT=3004 + - APP_ENV=production + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - BUCKET_ENDPOINT=BUCKET_ENDPOINT + - BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY + - BUCKET_SECRET_KEY=BUCKET_SECRET_KEY + - BUCKET_NAME=johnjud-pet-images + - BUCKET_USE_SSL=false + ports: + - "3004:3004" + networks: + - johnjud + + local-db: + image: postgres:15.1-alpine3.17 + container_name: johnjud-local-db + restart: unless-stopped + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: johnjud_db + volumes: + - postgres:/var/lib/postgresql/data + ports: + - "5432:5432" + networks: + - johnjud + +volumes: + postgres: +networks: + johnjud: \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 707a72e..c40bdfe 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,14 +3,23 @@ version: "3.9" services: local-file: image: ghcr.io/isd-sgcu/johnjud-file - container_name: file + container_name: johnjud-file depends_on: - local-db restart: unless-stopped - volumes: - - ./config/file:/app/config + environment: + - APP_PORT=3004 + - APP_ENV=development + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - BUCKET_ENDPOINT=BUCKET_ENDPOINT + - BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY + - BUCKET_SECRET_KEY=BUCKET_SECRET_KEY + - BUCKET_NAME=johnjud-pet-images + - BUCKET_USE_SSL=false ports: - "3004:3004" + networks: + - johnjud local-db: image: postgres:15.1-alpine3.17 @@ -24,6 +33,10 @@ services: - postgres:/var/lib/postgresql/data ports: - "5432:5432" + networks: + - johnjud volumes: - postgres: \ No newline at end of file + postgres: +networks: + johnjud: \ No newline at end of file diff --git a/go.mod b/go.mod index 6b1437b..ba7e136 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,8 @@ require ( github.com/bxcodec/faker/v3 v3.8.1 github.com/google/uuid v1.5.0 github.com/isd-sgcu/johnjud-go-proto v0.5.0 - github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 - github.com/spf13/viper v1.18.2 + github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.60.1 gorm.io/driver/postgres v1.5.4 diff --git a/go.sum b/go.sum index cf9a865..458a30d 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,6 @@ 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/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/isd-sgcu/johnjud-go-proto v0.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= -github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= github.com/isd-sgcu/johnjud-go-proto v0.5.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -49,7 +47,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= -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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= @@ -71,8 +68,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= -github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/spf13/viper v1.18.1 h1:rmuU42rScKWlhhJDyXZRKJQHXFX02chSVW1IvkPGiVM= +github.com/spf13/viper v1.18.1/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= diff --git a/src/config/config.go b/src/config/config.go index fc6876a..9992687 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,50 +1,65 @@ package config import ( - "github.com/pkg/errors" "github.com/spf13/viper" ) type Database struct { - Host string `mapstructure:"host"` - Port int `mapstructure:"port"` - Name string `mapstructure:"name"` - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - SSL string `mapstructure:"ssl"` + Url string `mapstructure:"URL"` } type App struct { - Port int `mapstructure:"port"` - Debug bool `mapstructure:"debug"` + Port int `mapstructure:"PORT"` + Env string `mapstructure:"ENV"` } type Service struct { - File string `mapstructure:"file"` + File string `mapstructure:"FILE"` } type Config struct { - App App `mapstructure:"app"` - Database Database `mapstructure:"database"` - Service Service `mapstructure:"service"` + App App + Database Database + Service Service } -func LoadConfig() (config *Config, err error) { - viper.AddConfigPath("./config") - viper.SetConfigName("config") - viper.SetConfigType("yaml") +func LoadConfig() (*Config, error) { + dbCfgLdr := viper.New() + dbCfgLdr.SetEnvPrefix("DB") + dbCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + dbConfig := Database{} + if err := dbCfgLdr.Unmarshal(&dbConfig); err != nil { + return nil, err + } - viper.AutomaticEnv() + appCfgLdr := viper.New() + appCfgLdr.SetEnvPrefix("APP") + appCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + appConfig := App{} + if err := appCfgLdr.Unmarshal(&appConfig); err != nil { + return nil, err + } - err = viper.ReadInConfig() - if err != nil { - return nil, errors.Wrap(err, "error occurs while reading the config") + serviceCfgLdr := viper.New() + serviceCfgLdr.SetEnvPrefix("SERVICE") + serviceCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + serviceConfig := Service{} + if err := serviceCfgLdr.Unmarshal(&serviceConfig); err != nil { + return nil, err } - err = viper.Unmarshal(&config) - if err != nil { - return nil, errors.Wrap(err, "error occurs while unmarshal the config") + config := &Config{ + Database: dbConfig, + App: appConfig, + Service: serviceConfig, } - return + return config, nil +} + +func (ac *App) IsDevelopment() bool { + return ac.Env == "development" } diff --git a/src/database/postgresql.connection.go b/src/database/postgresql.connection.go index 8369137..2acc6de 100644 --- a/src/database/postgresql.connection.go +++ b/src/database/postgresql.connection.go @@ -1,9 +1,6 @@ package database import ( - "fmt" - "strconv" - "github.com/isd-sgcu/johnjud-backend/src/app/model/like" "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" "github.com/isd-sgcu/johnjud-backend/src/app/model/user" @@ -14,15 +11,13 @@ import ( ) func InitPostgresDatabase(conf *config.Database, isDebug bool) (db *gorm.DB, err error) { - dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", conf.Host, strconv.Itoa(conf.Port), conf.Username, conf.Password, conf.Name, conf.SSL) - gormConf := &gorm.Config{} if !isDebug { gormConf.Logger = gormLogger.Default.LogMode(gormLogger.Silent) } - db, err = gorm.Open(postgres.Open(dsn), gormConf) + db, err = gorm.Open(postgres.Open(conf.Url), gormConf) if err != nil { return nil, err } diff --git a/src/main.go b/src/main.go index e23b1b4..7ee7b44 100644 --- a/src/main.go +++ b/src/main.go @@ -93,7 +93,7 @@ func main() { Msg("Failed to load config") } - db, err := database.InitPostgresDatabase(&conf.Database, conf.App.Debug) + db, err := database.InitPostgresDatabase(&conf.Database, conf.App.IsDevelopment()) if err != nil { log.Fatal(). Err(err). diff --git a/tools/export-env.sh b/tools/export-env.sh new file mode 100644 index 0000000..39688ee --- /dev/null +++ b/tools/export-env.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +## Usage: +## . ./export-env.sh ; $COMMAND +## . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE} + +unamestr=$(uname) +if [ "$unamestr" = 'Linux' ]; then + + export $(grep -v '^#' .env | xargs -d '\n') + +elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then + + export $(grep -v '^#' .env | xargs -0) + +fi \ No newline at end of file