rbac/config/config.go
zhangkun9038@dingtalk.com 6669be1923 up
2025-02-16 17:55:27 +08:00

50 lines
1.0 KiB
Go

package config
import (
"os"
"strconv"
)
type Config struct {
DBHost string
DBPort int
DBUser string
DBPassword string
DBName string
JWTSecret string
RedisHost string
RedisPort int
RedisPassword string
}
var AppConfig Config
func Init() {
AppConfig = Config{
DBHost: getEnv("DB_HOST", "localhost"),
DBPort: getEnvAsInt("DB_PORT", 3306),
DBUser: getEnv("DB_USER", "root"),
DBPassword: getEnv("DB_PASSWORD", ""),
DBName: 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
}