- Add internal/config/config.go with viper-backed Config struct - Explicit BindEnv calls for reliable env var -> config mapping (mapstructure v2 compat) - Config loads from config.json + .env, env vars take precedence - Add config.json with non-secret defaults (port, timeouts, URLs) - Fix SPA fallback: spaHandler serves index.html for unknown paths (client-side routing) - All 5 tests pass: TestHealth, TestLoadDefaults, TestLoadEnvOverride, TestLoadNetBoxURL - Add Makefile with build/dev/test/clean targets
48 lines
1 KiB
Go
48 lines
1 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.georgsen.dk/hwlab/internal/config"
|
|
)
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
// Unset env vars that might interfere
|
|
os.Unsetenv("HWLAB_PORT")
|
|
os.Unsetenv("HWLAB_NETBOX_URL")
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Port != 8080 {
|
|
t.Errorf("default port: want 8080, got %d", cfg.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvOverride(t *testing.T) {
|
|
os.Setenv("HWLAB_PORT", "9999")
|
|
defer os.Unsetenv("HWLAB_PORT")
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Port != 9999 {
|
|
t.Errorf("env override port: want 9999, got %d", cfg.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoadNetBoxURL(t *testing.T) {
|
|
os.Setenv("HWLAB_NETBOX_URL", "http://10.5.0.130:8000/api")
|
|
defer os.Unsetenv("HWLAB_NETBOX_URL")
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.NetBoxURL != "http://10.5.0.130:8000/api" {
|
|
t.Errorf("netbox url: want http://10.5.0.130:8000/api, got %s", cfg.NetBoxURL)
|
|
}
|
|
}
|