71 lines
2.1 KiB
Go
71 lines
2.1 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
|
|
FastAPIURL string
|
|
ControlUser string
|
|
ControlPass string
|
|
ControlToken string
|
|
Heartbeat time.Duration
|
|
MLTimeout time.Duration
|
|
|
|
// RefreshInterval is how often a healthy /bootstrap snapshot is refreshed.
|
|
RefreshInterval time.Duration
|
|
// BootstrapTimeout bounds one /bootstrap request.
|
|
BootstrapTimeout 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. CONTROL_TOKEN is optional and, when set, is
|
|
// forwarded to FastAPI's internal /train endpoints as a Bearer token.
|
|
func Load() Config {
|
|
port := getenv("PORT", "8080")
|
|
return Config{
|
|
Addr: ":" + port,
|
|
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
|
|
ControlUser: os.Getenv("CONTROL_USER"),
|
|
ControlPass: os.Getenv("CONTROL_PASS"),
|
|
ControlToken: os.Getenv("CONTROL_TOKEN"),
|
|
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
|
|
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
|
|
RefreshInterval: durationEnv("BOOTSTRAP_REFRESH", 60*time.Second),
|
|
BootstrapTimeout: durationEnv("BOOTSTRAP_TIMEOUT", 20*time.Second),
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|