50 lines
1.0 KiB
Go
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
|
|
}
|