59 lines
975 B
Go
59 lines
975 B
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Auth AuthConfig
|
|
DB DBConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int
|
|
}
|
|
|
|
type AuthConfig struct {
|
|
Username string
|
|
Password string
|
|
Secret string
|
|
}
|
|
|
|
type DBConfig struct {
|
|
DSN string
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnvInt("ENERGY_PORT", 8080),
|
|
},
|
|
Auth: AuthConfig{
|
|
Username: getEnv("ENERGY_AUTH_USER", "admin"),
|
|
Password: getEnv("ENERGY_AUTH_PASSWORD", ""),
|
|
Secret: getEnv("ENERGY_AUTH_SECRET", "change-me-in-production"),
|
|
},
|
|
DB: DBConfig{
|
|
DSN: getEnv("ENERGY_DB_DSN", "postgres://energy:changeme@localhost:5433/energy"),
|
|
},
|
|
}
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvInt(key string, fallback int) int {
|
|
if v := os.Getenv(key); v != "" {
|
|
if i, err := strconv.Atoi(v); err == nil {
|
|
return i
|
|
}
|
|
}
|
|
return fallback
|
|
}
|