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:
14
main.go
14
main.go
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
v1 "github.com/wild-cloud/wild-central/internal/api/v1"
|
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/frontend"
|
||||||
"github.com/wild-cloud/wild-central/internal/logging"
|
"github.com/wild-cloud/wild-central/internal/logging"
|
||||||
"github.com/wild-cloud/wild-central/internal/natsbus"
|
"github.com/wild-cloud/wild-central/internal/natsbus"
|
||||||
@@ -36,7 +37,7 @@ func splitAndTrim(s string, sep string) []string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildAllowedOrigins() []string {
|
func buildAllowedOrigins(dataDir string) []string {
|
||||||
if corsOrigins := os.Getenv("WILD_CENTRAL_CORS_ORIGINS"); corsOrigins != "" {
|
if corsOrigins := os.Getenv("WILD_CENTRAL_CORS_ORIGINS"); corsOrigins != "" {
|
||||||
origins := splitAndTrim(corsOrigins, ",")
|
origins := splitAndTrim(corsOrigins, ",")
|
||||||
slog.Info("CORS configured with explicit origins", "origins", origins)
|
slog.Info("CORS configured with explicit origins", "origins", origins)
|
||||||
@@ -69,6 +70,11 @@ func buildAllowedOrigins() []string {
|
|||||||
"http://127.0.0.1:3000",
|
"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
|
return allowedOrigins
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +108,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedOrigins := buildAllowedOrigins()
|
allowedOrigins := buildAllowedOrigins(dataDir)
|
||||||
|
|
||||||
api, err := v1.NewAPI(dataDir, Version, allowedOrigins)
|
api, err := v1.NewAPI(dataDir, Version, allowedOrigins)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -150,10 +156,10 @@ func main() {
|
|||||||
fmt.Sscanf(v, "%d", &port)
|
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.
|
// (if a domain is configured), and reconcile all networking.
|
||||||
api.SetPort(port)
|
api.SetPort(port)
|
||||||
api.EnsureCentralService()
|
api.EnsureCentralDomain()
|
||||||
api.Reconcile()
|
api.Reconcile()
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", host, port)
|
addr := fmt.Sprintf("%s:%d", host, port)
|
||||||
|
|||||||
Reference in New Issue
Block a user