Files
api-go/internal/config/config.go
T

63 lines
1.6 KiB
Go

// Package config reads runtime configuration from the environment.
package config
import (
"os"
"strconv"
"time"
)
// Config is the process configuration.
type Config struct {
Addr string
DataDir string
FastAPIURL string
ControlUser string
ControlPass string
Heartbeat time.Duration
MLTimeout time.Duration
}
// Load reads configuration from env with local-development defaults.
// CONTROL_USER / CONTROL_PASS have no defaults: POST /api/scenario fails
// closed when they are unset.
func Load() Config {
port := getenv("PORT", "8080")
return Config{
Addr: ":" + port,
DataDir: getenv("DATA_DIR", "testdata/data"),
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
ControlUser: os.Getenv("CONTROL_USER"),
ControlPass: os.Getenv("CONTROL_PASS"),
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
}
}
// ControlAuthConfigured reports whether both control credentials exist.
func (c Config) ControlAuthConfigured() bool {
return c.ControlUser != "" && c.ControlPass != ""
}
func getenv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func durationEnv(key string, fallback time.Duration) time.Duration {
raw := os.Getenv(key)
if raw == "" {
return fallback
}
if d, err := time.ParseDuration(raw); err == nil && d > 0 {
return d
}
// Also accept a plain number of seconds for operational convenience.
if secs, err := strconv.Atoi(raw); err == nil && secs > 0 {
return time.Duration(secs) * time.Second
}
return fallback
}