Skip to content

Commit

Permalink
Merge pull request #20 from isd-sgcu/JOH-37/migrate-to-envs
Browse files Browse the repository at this point in the history
[JOH-37] Migrate from Config to Envs
  • Loading branch information
bookpanda authored Jan 16, 2024
2 parents 8570958 + dfd7299 commit e6ec640
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
APP_PORT=3003
APP_ENV=development

DB_URL=postgres://root:root@localhost:5432/johnjud_db

SERVICE_FILE=localhost:3004
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
config.yaml
.env
.env.prod
.env.file

# idea
.idea
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/.
13 changes: 0 additions & 13 deletions config/config.example.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions config/file/config.example.yaml

This file was deleted.

55 changes: 55 additions & 0 deletions docker-compose-prod.yaml
Original file line number Diff line number Diff line change
@@ -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:
21 changes: 17 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +33,10 @@ services:
- postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- johnjud

volumes:
postgres:
postgres:
networks:
johnjud:
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
65 changes: 40 additions & 25 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 1 addition & 6 deletions src/database/postgresql.connection.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
16 changes: 16 additions & 0 deletions tools/export-env.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e6ec640

Please sign in to comment.