Skip to content

Commit

Permalink
changed templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan-Nava authored Jun 12, 2023
1 parent 818a055 commit 0744e6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
33 changes: 0 additions & 33 deletions template/config/config.tmpl

This file was deleted.

17 changes: 14 additions & 3 deletions template/database/db.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions template/env/.env.local
Original file line number Diff line number Diff line change
@@ -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
38 changes: 34 additions & 4 deletions template/env/env.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 0744e6c

Please sign in to comment.