66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package initialize
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/dungnt11/todoms_golang/global"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func LoadConfig() error {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Cấu hình server
|
|
global.Config.Server.Port = os.Getenv("PORT")
|
|
global.Config.Server.AppEnv = os.Getenv("APP_ENV")
|
|
|
|
// Cấu hình database
|
|
global.Config.Database.Host = os.Getenv("BLUEPRINT_DB_HOST")
|
|
global.Config.Database.Port = os.Getenv("BLUEPRINT_DB_PORT")
|
|
global.Config.Database.Database = os.Getenv("BLUEPRINT_DB_DATABASE")
|
|
global.Config.Database.Username = os.Getenv("BLUEPRINT_DB_USERNAME")
|
|
global.Config.Database.Password = os.Getenv("BLUEPRINT_DB_PASSWORD")
|
|
global.Config.Database.RootPassword = os.Getenv("BLUEPRINT_DB_ROOT_PASSWORD")
|
|
|
|
// Chuyển đổi các giá trị cấu hình database từ string sang int
|
|
if maxIdleConns := os.Getenv("BLUEPRINT_DB_MAX_IDLE_CONNS"); maxIdleConns != "" {
|
|
fmt.Sscanf(maxIdleConns, "%d", &global.Config.Database.MaxIdleConns)
|
|
}
|
|
|
|
if maxOpenConns := os.Getenv("BLUEPRINT_DB_MAX_OPEN_CONNS"); maxOpenConns != "" {
|
|
fmt.Sscanf(maxOpenConns, "%d", &global.Config.Database.MaxOpenConns)
|
|
}
|
|
|
|
if connMaxLifetime := os.Getenv("BLUEPRINT_DB_CONN_MAX_LIFETIME"); connMaxLifetime != "" {
|
|
fmt.Sscanf(connMaxLifetime, "%d", &global.Config.Database.ConnMaxLifetime)
|
|
}
|
|
|
|
// Cấu hình logger
|
|
global.Config.Logger.LogLevel = os.Getenv("LOGGER_LOG_LEVEL")
|
|
global.Config.Logger.FileLogName = os.Getenv("LOGGER_FILE_LOG_NAME")
|
|
|
|
// Chuyển đổi từ string sang int và bool
|
|
if maxSize := os.Getenv("LOGGER_MAX_SIZE"); maxSize != "" {
|
|
fmt.Sscanf(maxSize, "%d", &global.Config.Logger.MaxSize)
|
|
}
|
|
|
|
if maxBackups := os.Getenv("LOGGER_MAX_BACKUPS"); maxBackups != "" {
|
|
fmt.Sscanf(maxBackups, "%d", &global.Config.Logger.MaxBackups)
|
|
}
|
|
|
|
if maxAge := os.Getenv("LOGGER_MAX_AGE"); maxAge != "" {
|
|
fmt.Sscanf(maxAge, "%d", &global.Config.Logger.MaxAge)
|
|
}
|
|
|
|
if compress := os.Getenv("LOGGER_COMPRESS"); compress != "" {
|
|
global.Config.Logger.Compress = strings.ToLower(compress) == "true"
|
|
}
|
|
|
|
return nil
|
|
}
|