package config import ( "os" "strconv" ) type DBConfig struct { Host string Port int User string Password string Name string } func (db *DBConfig) DSN() string { return db.User + ":" + db.Password + "@tcp(" + db.Host + ":" + strconv.Itoa(db.Port) + ")/" + db.Name + "?charset=utf8mb4&parseTime=True&loc=Local" } type Config struct { DB DBConfig JWTSecret string RedisHost string RedisPort int RedisPassword string } var AppConfig Config func Init() { AppConfig = Config{ DB: DBConfig{ Host: getEnv("DB_HOST", "localhost"), Port: getEnvAsInt("DB_PORT", 3306), User: getEnv("DB_USER", "root"), Password: getEnv("DB_PASSWORD", ""), Name: getEnv("DB_NAME", "rbac"), }, JWTSecret: getEnv("JWT_SECRET", "secret"), RedisHost: getEnv("REDIS_HOST", "localhost"), RedisPort: getEnvAsInt("REDIS_PORT", 6379), RedisPassword: getEnv("REDIS_PASSWORD", ""), } } func getEnv(key, defaultValue string) string { if value, exists := os.LookupEnv(key); exists { return value } return defaultValue } func getEnvAsInt(key string, defaultValue int) int { valueStr := getEnv(key, "") if value, err := strconv.Atoi(valueStr); err == nil { return value } return defaultValue }