From 3c99d26830fe7fdda7a9be312f3d236e43fff582 Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Fri, 10 Jul 2026 19:12:27 +0000 Subject: [PATCH] Auto-detect Central domain for CORS allowed origins Read the configured Central domain from state.yaml and add it as an HTTPS origin so the web UI at e.g. https://central.payne.io works without manually setting WILD_CENTRAL_CORS_ORIGINS. --- main.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 1568d29..4f55f1b 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/gorilla/mux" v1 "github.com/wild-cloud/wild-central/internal/api/v1" + "github.com/wild-cloud/wild-central/internal/config" "github.com/wild-cloud/wild-central/internal/frontend" "github.com/wild-cloud/wild-central/internal/logging" "github.com/wild-cloud/wild-central/internal/natsbus" @@ -36,7 +37,7 @@ func splitAndTrim(s string, sep string) []string { return result } -func buildAllowedOrigins() []string { +func buildAllowedOrigins(dataDir string) []string { if corsOrigins := os.Getenv("WILD_CENTRAL_CORS_ORIGINS"); corsOrigins != "" { origins := splitAndTrim(corsOrigins, ",") slog.Info("CORS configured with explicit origins", "origins", origins) @@ -69,6 +70,11 @@ func buildAllowedOrigins() []string { "http://127.0.0.1:3000", ) + // Add the configured Central domain (e.g. https://central.payne.io) + if cfg, err := config.LoadState(filepath.Join(dataDir, "state.yaml")); err == nil && cfg.Cloud.Central.Domain != "" { + allowedOrigins = append(allowedOrigins, "https://"+cfg.Cloud.Central.Domain) + } + return allowedOrigins } @@ -102,7 +108,7 @@ func main() { os.Exit(1) } - allowedOrigins := buildAllowedOrigins() + allowedOrigins := buildAllowedOrigins(dataDir) api, err := v1.NewAPI(dataDir, Version, allowedOrigins) if err != nil { @@ -150,10 +156,10 @@ func main() { fmt.Sscanf(v, "%d", &port) } - // Tell the API what port it's running on, register Central as a service + // Tell the API what port it's running on, register Central as a domain // (if a domain is configured), and reconcile all networking. api.SetPort(port) - api.EnsureCentralService() + api.EnsureCentralDomain() api.Reconcile() addr := fmt.Sprintf("%s:%d", host, port)