From b41547bda1a215cd1714f3a2649831e4496fa8a3 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 15 Jan 2024 17:10:25 +0700 Subject: [PATCH 1/7] feat: new env --- .env.file.template | 10 ++++++++++ .env.template | 6 ++++++ .gitignore | 1 + 3 files changed, 17 insertions(+) create mode 100644 .env.file.template create mode 100644 .env.template diff --git a/.env.file.template b/.env.file.template new file mode 100644 index 0000000..50b0486 --- /dev/null +++ b/.env.file.template @@ -0,0 +1,10 @@ +APP_PORT=3004 +APP_ENV=development + +DB_URL=postgres://root:root@localhost: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 \ No newline at end of file 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 From 94ea2c3ab750ba738f78d9d7629a5ffe41a505dd Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 15 Jan 2024 17:10:42 +0700 Subject: [PATCH 2/7] fix: config --- go.mod | 1 - go.sum | 3 - src/config/config.go | 113 ++++++++++++++++++++------ src/database/postgresql.connection.go | 7 +- src/main.go | 2 +- 5 files changed, 90 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 6b1437b..44f35c6 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ 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/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index cf9a865..a5d4841 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= diff --git a/src/config/config.go b/src/config/config.go index fc6876a..8be6537 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,50 +1,113 @@ +// 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"` +// } + +// type App struct { +// Port int `mapstructure:"port"` +// Debug bool `mapstructure:"debug"` +// } + +// type Service struct { +// File string `mapstructure:"file"` +// } + +// type Config struct { +// App App `mapstructure:"app"` +// Database Database `mapstructure:"database"` +// Service Service `mapstructure:"service"` +// } + +// func LoadConfig() (config *Config, err error) { +// viper.AddConfigPath("./config") +// viper.SetConfigName("config") +// viper.SetConfigType("yaml") + +// viper.AutomaticEnv() + +// err = viper.ReadInConfig() +// if err != nil { +// return nil, errors.Wrap(err, "error occurs while reading the config") +// } + +// err = viper.Unmarshal(&config) +// if err != nil { +// return nil, errors.Wrap(err, "error occurs while unmarshal the config") +// } + +// return +// } + package config import ( - "github.com/pkg/errors" + "github.com/rs/zerolog/log" "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:"db_url"` } type App struct { - Port int `mapstructure:"port"` - Debug bool `mapstructure:"debug"` + Port int `mapstructure:"app_port"` + Env string `mapstructure:"app_env"` } type Service struct { - File string `mapstructure:"file"` + File string `mapstructure:"service_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) { + viper.SetConfigFile(".env") + err := viper.ReadInConfig() + if err != nil { + log.Fatal().Err(err). + Str("service", "file"). + Msg("Failed to load .env file") + } - viper.AutomaticEnv() + var dbConfig Database + if err := viper.Unmarshal(&dbConfig); err != nil { + return nil, err + } - err = viper.ReadInConfig() - if err != nil { - return nil, errors.Wrap(err, "error occurs while reading the config") + var appConfig App + if err := viper.Unmarshal(&appConfig); err != nil { + return nil, err } - err = viper.Unmarshal(&config) - if err != nil { - return nil, errors.Wrap(err, "error occurs while unmarshal the config") + var serviceConfig Service + if err := viper.Unmarshal(&serviceConfig); err != nil { + return nil, err + } + + 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). From 185a3bbadd76227c58d4c0fbf6c74e6726a166d8 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:04:56 +0700 Subject: [PATCH 3/7] feat: load env viper --- Makefile | 2 +- go.mod | 2 +- go.sum | 4 +- src/config/config.go | 92 +++++++++++--------------------------------- tools/export-env.sh | 16 ++++++++ 5 files changed, 42 insertions(+), 74 deletions(-) create mode 100644 tools/export-env.sh 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/go.mod b/go.mod index 44f35c6..ba7e136 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/google/uuid v1.5.0 github.com/isd-sgcu/johnjud-go-proto v0.5.0 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 a5d4841..458a30d 100644 --- a/go.sum +++ b/go.sum @@ -68,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 8be6537..9992687 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,72 +1,20 @@ -// 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"` -// } - -// type App struct { -// Port int `mapstructure:"port"` -// Debug bool `mapstructure:"debug"` -// } - -// type Service struct { -// File string `mapstructure:"file"` -// } - -// type Config struct { -// App App `mapstructure:"app"` -// Database Database `mapstructure:"database"` -// Service Service `mapstructure:"service"` -// } - -// func LoadConfig() (config *Config, err error) { -// viper.AddConfigPath("./config") -// viper.SetConfigName("config") -// viper.SetConfigType("yaml") - -// viper.AutomaticEnv() - -// err = viper.ReadInConfig() -// if err != nil { -// return nil, errors.Wrap(err, "error occurs while reading the config") -// } - -// err = viper.Unmarshal(&config) -// if err != nil { -// return nil, errors.Wrap(err, "error occurs while unmarshal the config") -// } - -// return -// } - package config import ( - "github.com/rs/zerolog/log" "github.com/spf13/viper" ) type Database struct { - Url string `mapstructure:"db_url"` + Url string `mapstructure:"URL"` } type App struct { - Port int `mapstructure:"app_port"` - Env string `mapstructure:"app_env"` + Port int `mapstructure:"PORT"` + Env string `mapstructure:"ENV"` } type Service struct { - File string `mapstructure:"service_file"` + File string `mapstructure:"FILE"` } type Config struct { @@ -76,26 +24,30 @@ type Config struct { } func LoadConfig() (*Config, error) { - viper.SetConfigFile(".env") - err := viper.ReadInConfig() - if err != nil { - log.Fatal().Err(err). - Str("service", "file"). - Msg("Failed to load .env file") - } - - var dbConfig Database - if err := viper.Unmarshal(&dbConfig); err != nil { + dbCfgLdr := viper.New() + dbCfgLdr.SetEnvPrefix("DB") + dbCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + dbConfig := Database{} + if err := dbCfgLdr.Unmarshal(&dbConfig); err != nil { return nil, err } - var appConfig App - if err := viper.Unmarshal(&appConfig); err != nil { + appCfgLdr := viper.New() + appCfgLdr.SetEnvPrefix("APP") + appCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + appConfig := App{} + if err := appCfgLdr.Unmarshal(&appConfig); err != nil { return nil, err } - var serviceConfig Service - if err := viper.Unmarshal(&serviceConfig); err != nil { + serviceCfgLdr := viper.New() + serviceCfgLdr.SetEnvPrefix("SERVICE") + serviceCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + serviceConfig := Service{} + if err := serviceCfgLdr.Unmarshal(&serviceConfig); err != nil { return nil, 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 From 14b235b13b7f199b9e038417fdbeabd57fc7fc12 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:05:10 +0700 Subject: [PATCH 4/7] fix: compose --- docker-compose.yaml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 707a72e..98edf91 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,10 +7,19 @@ services: 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 From 5b2ad0a031cc33ffb688b1b0ee623e291e28485f Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:16:36 +0700 Subject: [PATCH 5/7] feat: prod compose --- docker-compose.prod.yaml | 55 ++++++++++++++++++++++++++++++++++++++++ docker-compose.yaml | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 docker-compose.prod.yaml 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 98edf91..c40bdfe 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,7 +3,7 @@ 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 From 42237bcc89f406269c6c3254227367f4b3096a88 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:16:43 +0700 Subject: [PATCH 6/7] fix: remove config --- .env.file.template | 10 ---------- config/config.example.yaml | 13 ------------- config/file/config.example.yaml | 14 -------------- 3 files changed, 37 deletions(-) delete mode 100644 .env.file.template delete mode 100644 config/config.example.yaml delete mode 100644 config/file/config.example.yaml diff --git a/.env.file.template b/.env.file.template deleted file mode 100644 index 50b0486..0000000 --- a/.env.file.template +++ /dev/null @@ -1,10 +0,0 @@ -APP_PORT=3004 -APP_ENV=development - -DB_URL=postgres://root:root@localhost: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 \ No newline at end of file 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 From dfd729982d4f8b79c8aa470aff8c632bad574aa3 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:49:57 +0700 Subject: [PATCH 7/7] fix:rename compose --- docker-compose.prod.yaml => docker-compose-prod.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename docker-compose.prod.yaml => docker-compose-prod.yaml (100%) diff --git a/docker-compose.prod.yaml b/docker-compose-prod.yaml similarity index 100% rename from docker-compose.prod.yaml rename to docker-compose-prod.yaml