This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/config/config.go
2026-02-20 22:24:47 +00:00

64 lines
1.5 KiB
Go

package config
import (
"os"
"github.com/joho/godotenv"
)
// Config holds all application configuration.
type Config struct {
Port string
DatabaseURL string
JWTSecret string
Environment string
BasePath string // URL path prefix (e.g. "/dev", "/test", or "" for root)
// Admin bootstrap (optional — set via K8s secret)
AdminUsername string
AdminPassword string
AdminEmail string
}
// Load reads configuration from environment variables.
// A .env file is loaded if present but not required.
func Load() *Config {
// Best-effort .env load (not required in production)
_ = godotenv.Load()
return &Config{
Port: getEnv("PORT", "8080"),
DatabaseURL: getEnv("DATABASE_URL", ""),
JWTSecret: getEnv("JWT_SECRET", "dev-secret-change-me"),
Environment: getEnv("ENVIRONMENT", "development"),
BasePath: sanitizeBasePath(getEnv("BASE_PATH", "")),
AdminUsername: getEnv("SWITCHBOARD_ADMIN_USERNAME", ""),
AdminPassword: getEnv("SWITCHBOARD_ADMIN_PASSWORD", ""),
AdminEmail: getEnv("SWITCHBOARD_ADMIN_EMAIL", ""),
}
}
// sanitizeBasePath ensures the path starts with / and doesn't end with /.
// Empty string means root (no prefix).
func sanitizeBasePath(p string) string {
if p == "" || p == "/" {
return ""
}
// Ensure leading slash
if p[0] != '/' {
p = "/" + p
}
// Strip trailing slash
for len(p) > 1 && p[len(p)-1] == '/' {
p = p[:len(p)-1]
}
return p
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}