// Package config loads workout-suggester settings from the environment and // an optional .env file. package config import ( "bufio" "fmt" "os" "strings" "time" ) // Config holds all runtime settings. type Config struct { Addr string // ADDR: where the web UI listens BaseURL string // LLM_BASE_URL: OpenAI-compatible API root, e.g. http://127.0.0.1:8000/v1 APIKey string // LLM_API_KEY: optional bearer token for the model server Model string // LLM_MODEL: model id; empty means "first model the server lists" Timeout time.Duration // LLM_TIMEOUT: upper bound for a single reply OverpassURL string // OVERPASS_URL: OpenStreetMap Overpass API endpoint BRouterURL string // BROUTER_URL: BRouter endpoint for loops from your door; "-" disables DefaultPlace string // DEFAULT_PLACE: prefilled town in the form ForecastURL string // OPEN_METEO_URL: forecast endpoint (Open-Meteo is self-hostable) GeocodeURL string // OPEN_METEO_GEOCODE_URL: place search endpoint } // Load reads the configuration from environment variables, applying defaults. func Load() (Config, error) { c := Config{ Addr: env("ADDR", "127.0.0.1:3000"), BaseURL: strings.TrimRight(env("LLM_BASE_URL", "http://127.0.0.1:8000/v1"), "/"), APIKey: os.Getenv("LLM_API_KEY"), Model: os.Getenv("LLM_MODEL"), Timeout: 3 * time.Minute, OverpassURL: env("OVERPASS_URL", "https://overpass-api.de/api/interpreter"), BRouterURL: env("BROUTER_URL", "https://brouter.de/brouter"), DefaultPlace: os.Getenv("DEFAULT_PLACE"), ForecastURL: env("OPEN_METEO_URL", "https://api.open-meteo.com/v1/forecast"), GeocodeURL: env("OPEN_METEO_GEOCODE_URL", "https://geocoding-api.open-meteo.com/v1/search"), } if v := os.Getenv("LLM_TIMEOUT"); v != "" { d, err := time.ParseDuration(v) if err != nil { return c, fmt.Errorf("LLM_TIMEOUT: %w", err) } c.Timeout = d } return c, nil } func env(key, fallback string) string { if v, ok := os.LookupEnv(key); ok && v != "" { return v } return fallback } // LoadEnvFile sets variables from a KEY=VALUE file. Variables that are // already set in the environment win, so the real environment can override // the file. Blank lines and lines starting with # are ignored; values may be // wrapped in single or double quotes. func LoadEnvFile(path string) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() sc := bufio.NewScanner(f) for n := 1; sc.Scan(); n++ { line := strings.TrimSpace(sc.Text()) if line == "" || strings.HasPrefix(line, "#") { continue } key, val, ok := strings.Cut(strings.TrimPrefix(line, "export "), "=") if !ok { return fmt.Errorf("%s:%d: expected KEY=VALUE", path, n) } key, val = strings.TrimSpace(key), strings.TrimSpace(val) if len(val) >= 2 && (val[0] == '"' || val[0] == '\'') && val[len(val)-1] == val[0] { val = val[1 : len(val)-1] } if _, set := os.LookupEnv(key); !set { os.Setenv(key, val) } } return sc.Err() }