59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
AppEnv string
|
|
AppAddr string
|
|
MySQLDSN string
|
|
RedisAddr string
|
|
RedisPassword string
|
|
RedisDB int
|
|
JWTSecret string
|
|
Storage StorageConfig
|
|
}
|
|
|
|
type StorageConfig struct {
|
|
Endpoint string
|
|
Bucket string
|
|
}
|
|
|
|
func Load() Config {
|
|
return Config{
|
|
AppEnv: getEnv("APP_ENV", "development"),
|
|
AppAddr: getEnv("APP_ADDR", ":8080"),
|
|
MySQLDSN: getEnv("MYSQL_DSN", "hfb:secret@tcp(127.0.0.1:3306)/hfb_sys?charset=utf8mb4&parseTime=True&loc=Local"),
|
|
RedisAddr: getEnv("REDIS_ADDR", "127.0.0.1:6379"),
|
|
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
|
RedisDB: getEnvInt("REDIS_DB", 0),
|
|
JWTSecret: getEnv("JWT_SECRET", "change-me"),
|
|
Storage: StorageConfig{
|
|
Endpoint: getEnv("STORAGE_ENDPOINT", "http://127.0.0.1:9000"),
|
|
Bucket: getEnv("STORAGE_BUCKET", "hfb-sys"),
|
|
},
|
|
}
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|
|
|
|
func getEnvInt(key string, fallback int) int {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|