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 (
|
require (
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4
|
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/gaspardle/go-mssqlclrgeo v0.0.0-20160129143314-97ceabf987a4
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/jackc/pgx/v5 v5.9.1
|
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/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 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
|||||||
@@ -1,49 +1,40 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"github.com/caarlos0/env/v11"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type appConfig struct {
|
type AzureConfig struct {
|
||||||
SourceDbUrl string
|
AccountName string `env:"AZ_ACCOUNT_NAME"`
|
||||||
TargetDbUrl string
|
Container string `env:"AZ_CONTAINER"`
|
||||||
LogLevel string
|
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() {
|
type appConfig struct {
|
||||||
err := godotenv.Load()
|
SourceDbUrl string `env:"SOURCE_DB_URL,required"`
|
||||||
if err != nil {
|
TargetDbUrl string `env:"TARGET_DB_URL,required"`
|
||||||
log.Warn("Warning: could not load .env file")
|
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
|
||||||
}
|
Azure AzureConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAppConfig() appConfig {
|
func getAppConfig() appConfig {
|
||||||
loadEnv()
|
if err := godotenv.Load(); err != nil {
|
||||||
|
log.Warn("Could not load .env file")
|
||||||
sourceDbUrl := os.Getenv("SOURCE_DB_URL")
|
|
||||||
if sourceDbUrl == "" {
|
|
||||||
log.Fatal("SOURCE_DB_URL environment variable not set")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
targetDbUrl := os.Getenv("TARGET_DB_URL")
|
cfg := appConfig{}
|
||||||
if targetDbUrl == "" {
|
|
||||||
log.Fatal("TARGET_DB_URL environment variable not set")
|
if err := env.Parse(&cfg); err != nil {
|
||||||
|
log.Fatalf("Error al cargar variables de entorno: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL"))
|
return cfg
|
||||||
if logLevel == "" {
|
|
||||||
logLevel = "INFO"
|
|
||||||
}
|
|
||||||
|
|
||||||
return appConfig{
|
|
||||||
SourceDbUrl: sourceDbUrl,
|
|
||||||
TargetDbUrl: targetDbUrl,
|
|
||||||
LogLevel: logLevel,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var App appConfig = getAppConfig()
|
var App appConfig = getAppConfig()
|
||||||
|
|||||||
Reference in New Issue
Block a user