feat: add caarlos0/env package for environment variable management and refactor appConfig structure
This commit is contained in:
1
go.mod
1
go.mod
@@ -4,6 +4,7 @@ go 1.26
|
||||
|
||||
require (
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4
|
||||
github.com/caarlos0/env/v11 v11.4.0
|
||||
github.com/gaspardle/go-mssqlclrgeo v0.0.0-20160129143314-97ceabf987a4
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.9.1
|
||||
|
||||
2
go.sum
2
go.sum
@@ -20,6 +20,8 @@ github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZ
|
||||
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/caarlos0/env/v11 v11.4.0 h1:Kcb6t5kIIr4XkoQC9AF2j+8E1Jsrl3Wz/hhm1LtoGAc=
|
||||
github.com/caarlos0/env/v11 v11.4.0/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
||||
@@ -1,49 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"github.com/joho/godotenv"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type appConfig struct {
|
||||
SourceDbUrl string
|
||||
TargetDbUrl string
|
||||
LogLevel string
|
||||
type AzureConfig struct {
|
||||
AccountName string `env:"AZ_ACCOUNT_NAME"`
|
||||
Container string `env:"AZ_CONTAINER"`
|
||||
AccountKey string `env:"AZ_ACCOUNT_KEY"`
|
||||
UseHTTPS bool `env:"AZ_USE_HTTPS" envDefault:"true"`
|
||||
ServiceURL string `env:"AZ_SERVICE_URL"`
|
||||
Prefix string `env:"AZ_PREFIX"`
|
||||
Enabled bool `env:"AZ_STORAGE_ENABLED"`
|
||||
}
|
||||
|
||||
func loadEnv() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Warn("Warning: could not load .env file")
|
||||
}
|
||||
type appConfig struct {
|
||||
SourceDbUrl string `env:"SOURCE_DB_URL,required"`
|
||||
TargetDbUrl string `env:"TARGET_DB_URL,required"`
|
||||
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
|
||||
Azure AzureConfig
|
||||
}
|
||||
|
||||
func getAppConfig() appConfig {
|
||||
loadEnv()
|
||||
|
||||
sourceDbUrl := os.Getenv("SOURCE_DB_URL")
|
||||
if sourceDbUrl == "" {
|
||||
log.Fatal("SOURCE_DB_URL environment variable not set")
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Warn("Could not load .env file")
|
||||
}
|
||||
|
||||
targetDbUrl := os.Getenv("TARGET_DB_URL")
|
||||
if targetDbUrl == "" {
|
||||
log.Fatal("TARGET_DB_URL environment variable not set")
|
||||
cfg := appConfig{}
|
||||
|
||||
if err := env.Parse(&cfg); err != nil {
|
||||
log.Fatalf("Error al cargar variables de entorno: %v", err)
|
||||
}
|
||||
|
||||
logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL"))
|
||||
if logLevel == "" {
|
||||
logLevel = "INFO"
|
||||
}
|
||||
|
||||
return appConfig{
|
||||
SourceDbUrl: sourceDbUrl,
|
||||
TargetDbUrl: targetDbUrl,
|
||||
LogLevel: logLevel,
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
var App appConfig = getAppConfig()
|
||||
|
||||
Reference in New Issue
Block a user