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.
This commit is contained in:
2026-07-10 19:12:27 +00:00
parent 6aa1e7d438
commit 3c99d26830

14
main.go
View File

@@ -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)