package app import ( "fmt" "log" "os" "github.com/joho/godotenv" ) var ( Config *ConfigStruct ) type ConfigStruct struct { Port string MongoURI string MongoDB string RedisHost string RedisPort string RedisPass string AdminEmail string AdminPass string RefreshSecret string RefreshExpiry string AccessSecret string AccessExpiry string Env string } // LoadConfig loads environment variables into Config struct and validates required fields. func LoadConfig() error { if _, exists := os.LookupEnv("MONGO_URI"); !exists { if err := godotenv.Load(); err != nil { log.Fatal("🔴 Failed to load .env file: ", err) } } Config = &ConfigStruct{ Port: getEnv("PORT", "8080"), MongoURI: os.Getenv("MONGO_URI"), MongoDB: getEnv("MONGO_DB", "test"), RedisHost: os.Getenv("REDIS_HOST"), RedisPort: os.Getenv("REDIS_PORT"), RedisPass: os.Getenv("REDIS_PASSWORD"), AdminEmail: os.Getenv("ADMIN_EMAIL"), AdminPass: os.Getenv("ADMIN_PASSWORD"), RefreshSecret: os.Getenv("REFRESH_SECRET"), RefreshExpiry: os.Getenv("REFRESH_EXPIRY"), AccessSecret: os.Getenv("ACCESS_SECRET"), AccessExpiry: os.Getenv("ACCESS_EXPIRY"), Env: os.Getenv("ENV"), } missing := "" if Config.Port == "" { missing += "PORT " } if Config.MongoURI == "" { missing += "MONGO_URI " } if Config.RedisHost == "" { missing += "REDIS_HOST " } if Config.RedisPort == "" { missing += "REDIS_PORT " } if Config.AccessSecret == "" { missing += "ACCESS_SECRET " } if Config.RefreshSecret == "" { missing += "REFRESH_SECRET " } if missing != "" { return fmt.Errorf("missing required environment variables: %s", missing) } return nil } // GetConfig returns the global config func GetConfig() *ConfigStruct { return Config } func getEnv(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def }