diff --git a/template/config/config.tmpl b/template/config/config.tmpl deleted file mode 100644 index 5bbaa43..0000000 --- a/template/config/config.tmpl +++ /dev/null @@ -1,33 +0,0 @@ -package config - -import ( - "fmt" - - "github.com/caarlos0/env/v6" - "log" -) - -var CONFIGURATION *Configuration - -type Configuration struct { - // - AppEnv string `env:"APP_ENV"` - LogLevel string `env:"LOG_LEVEL"` - RunningMode string `env:"RUNNING_MODE"` //fallback or main - DBName string `env:"DB_NAME"` - DBUsername string `env:"DB_USERNAME"` - DBPassword string `env:"DB_PASSWORD"` - DBHost string `env:"DB_HOST"` - DBPort string `env:"DB_PORT"` - // -} - - -func SetEnvConfig() { - cfg := Configuration{} - if err := env.Parse(&cfg); err != nil { - fmt.Printf("%+v\n", err) - } - logrus.Info("\nload configuration OK") - CONFIGURATION = &cfg -} \ No newline at end of file diff --git a/template/database/db.tmpl b/template/database/db.tmpl index 8ffeb05..29a011f 100644 --- a/template/database/db.tmpl +++ b/template/database/db.tmpl @@ -2,14 +2,25 @@ package database import ( - "github.com/sirupsen/logrus" + + "go.uber.org/zap" "gorm.io/driver/mysql" "gorm.io/gorm" ) -func InitDB() *gorm.DB { +func InitDB(log *zap.SugaredLogger, config *env.Configuration) *gorm.DB { // - + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", config.DBName, config.DBPassword, config.DbHost, config.DbPort, config.DbName) + conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatalf("error connection on %s, err: %s", dsn, err.Error()) + } + db, _ := conn.DB() + idle, _ := strconv.Atoi(config.DbIdleConn) + max, _ := strconv.Atoi(config.DbMaxConn) + + db.SetMaxIdleConns(idle) + db.SetMaxOpenConns(max) // return conn } \ No newline at end of file diff --git a/template/env/.env.local b/template/env/.env.local new file mode 100644 index 0000000..f924436 --- /dev/null +++ b/template/env/.env.local @@ -0,0 +1,10 @@ +API_SECRET=secret +APP_ENV=local +LOG_LEVEL=debug +DB_HOST=localhost +DB_IDLE_CONN=1 +DB_MAX_CONN=1 +DB_NAME=dbv +DB_PASSWORD=db_password! +DB_PORT=3306 +DB_USERNAME=db_user \ No newline at end of file diff --git a/template/env/env.tmpl b/template/env/env.tmpl index 4865e83..9e4cb4f 100644 --- a/template/env/env.tmpl +++ b/template/env/env.tmpl @@ -4,14 +4,44 @@ import ( "log" "os" + "github.com/caarlos0/env/v6" "github.com/joho/godotenv" + "go.uber.org/zap" ) -func SetupEnv() { - if os.Getenv("APP_ENV") == "local" { - err := godotenv.Load() + +type Configuration struct { + // + AppEnv string `env:"APP_ENV"` + LogLevel string `env:"LOG_LEVEL"` + RunningMode string `env:"RUNNING_MODE"` //fallback or main + DBName string `env:"DB_NAME"` + DBUsername string `env:"DB_USERNAME"` + DBPassword string `env:"DB_PASSWORD"` + DBHost string `env:"DB_HOST"` + DBPort string `env:"DB_PORT"` + // +} + +func GetEnvConfig(log *zap.SugaredLogger) *Configuration { + setupEnv(log) + cfg := Configuration{} + if err := env.Parse(&cfg); err != nil { + fmt.Printf("%+v\n", err) + } + log.Infof("Configuration loaded ", cfg.AppEnv) + return &cfg +} + +func setupEnv(log *zap.SugaredLogger) { + env := os.Getenv("APP_ENV") + log.Infof("Configuration loading ", env) + if env == "local" || env == "dev" || env == "staging" || env == "prod" { + err := godotenv.Load(fmt.Sprintf("./env/.env.%s", env)) if err != nil { - log.Fatal("Error loading .env file") + log.Debug("Error loading .env file") } + return } + godotenv.Load() } \ No newline at end of file