package config import ( "fmt" "os" "time" "github.com/spf13/viper" ) // Config almacena las configuraciones globales type Config struct { DBHost string `mapstructure:"DB_HOST"` DBPort int `mapstructure:"DB_PORT"` DBUser string `mapstructure:"DB_USER"` DBPassword string `mapstructure:"DB_PASSWORD"` DBName string `mapstructure:"DB_NAME"` DBMaxOpenConns int `mapstructure:"DB_MAX_OPEN_CONNS"` DBMaxIdleConns int `mapstructure:"DB_MAX_IDLE_CONNS"` DBConnMaxLifetime time.Duration `mapstructure:"DB_CONN_MAX_LIFETIME"` // Redis RedisHost string `mapstructure:"REDIS_HOST"` RedisPort int `mapstructure:"REDIS_PORT"` RedisPassword string `mapstructure:"REDIS_PASSWORD"` RedisDB int `mapstructure:"REDIS_DB"` RedisTTL time.Duration `mapstructure:"REDIS_TTL"` // tiempo de vida por defecto de claves RedisSubscribe string `mapstructure:"REDIS_SUBSCRIBE"` LogFilePath string `mapstructure:"LOG_FILE_PATH"` LogLevel string `mapstructure:"LOG_LEVEL"` LogMaxSize int `mapstructure:"LOG_MAX_SIZE"` LogMaxBackups int `mapstructure:"LOG_MAX_BACKUPS"` LogMaxAge int `mapstructure:"LOG_MAX_AGE"` LogCompress bool `mapstructure:"LOG_COMPRESS"` Environment string `mapstructure:"ENVIRONMENT"` WhereUnitsBusiness string `mapstructure:"WHERE_UNITS_BUSINESS"` ElasticURL string `mapstructure:"ELASTIC_URL"` ElasticEnabled bool `mapstructure:"ELASTIC_ENABLED"` TimeoutSeconds int `mapstructure:"TIMEOUT_SECONDS"` RetryCount int `mapstructure:"RETRY_COUNT"` TLSSkipVerify bool `mapstructure:"TLS_SKIP_VERIFY"` LogRequests bool `mapstructure:"LOG_REQUESTS"` EnableDebug bool `mapstructure:"ENABLE_DEBUG"` } var GlobalConfig *Config // LoadConfig carga las variables de entorno usando viper func LoadConfig() (*Config, error) { viper.SetConfigName(".env") viper.SetConfigType("env") viper.AddConfigPath(".") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { fmt.Println("⚠️ No se pudo leer el archivo .env, usando variables de entorno del sistema") } var config Config if err := viper.Unmarshal(&config); err != nil { return nil, err } GlobalConfig = &config // --- 👇 **clave**: exportar ENCRYPTION_KEY al entorno del proceso --- if k := viper.GetString("ENCRYPTION_KEY"); k != "" { _ = os.Setenv("ENCRYPTION_KEY", k) } fmt.Println("✅ Configuración .env cargada correctamente.") return &config, nil }