diff --git a/vehicle-data-platform/apps/api/internal/app/server.go b/vehicle-data-platform/apps/api/internal/app/server.go index 19190702..cd985f82 100644 --- a/vehicle-data-platform/apps/api/internal/app/server.go +++ b/vehicle-data-platform/apps/api/internal/app/server.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "database/sql" + "encoding/json" + "fmt" "log" "net/http" "sync" @@ -49,7 +51,33 @@ func NewServer(cfg config.Config) http.Handler { api := platform.NewHandler(platform.NewServiceWithRuntime(store, platform.RuntimeInfo{ RequestTimeoutMs: int(cfg.RequestTimeout / time.Millisecond), })) - return withRequestTimeout(static.Handler(cfg.StaticDir, api), cfg.RequestTimeout) + return withRequestTimeout(withAppConfig(static.Handler(cfg.StaticDir, api), cfg), cfg.RequestTimeout) +} + +func withAppConfig(next http.Handler, cfg config.Config) http.Handler { + type appConfig struct { + AMapWebJSKey string `json:"amapWebJsKey,omitempty"` + AMapSecurityCode string `json:"amapSecurityJsCode,omitempty"` + AMapServiceHost string `json:"amapSecurityServiceHost,omitempty"` + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/app-config.js" { + next.ServeHTTP(w, r) + return + } + w.Header().Set("Content-Type", "application/javascript; charset=utf-8") + w.Header().Set("Cache-Control", "no-store") + body, err := json.Marshal(appConfig{ + AMapWebJSKey: cfg.AMapWebJSKey, + AMapSecurityCode: cfg.AMapSecurityCode, + AMapServiceHost: cfg.AMapServiceHost, + }) + if err != nil { + http.Error(w, "failed to render app config", http.StatusInternalServerError) + return + } + _, _ = fmt.Fprintf(w, "window.__LINGNIU_APP_CONFIG__=%s;\n", body) + }) } func withRequestTimeout(next http.Handler, timeout time.Duration) http.Handler { diff --git a/vehicle-data-platform/apps/api/internal/app/server_test.go b/vehicle-data-platform/apps/api/internal/app/server_test.go index fe1def55..ea9508e9 100644 --- a/vehicle-data-platform/apps/api/internal/app/server_test.go +++ b/vehicle-data-platform/apps/api/internal/app/server_test.go @@ -2,8 +2,10 @@ package app import ( "encoding/json" + "lingniu/vehicle-data-platform/apps/api/internal/config" "net/http" "net/http/httptest" + "strings" "testing" "time" ) @@ -56,3 +58,28 @@ func TestWithRequestTimeoutReturnsEnvelopeWithTraceID(t *testing.T) { t.Fatalf("unexpected timeout envelope: %+v body=%s", body, rec.Body.String()) } } + +func TestAppConfigScriptIsRuntimeRendered(t *testing.T) { + handler := withAppConfig(http.NotFoundHandler(), config.Config{ + AMapWebJSKey: "web-key", + AMapSecurityCode: "security-code", + AMapServiceHost: "/_AMapService", + }) + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/app-config.js", nil) + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) + } + if got := rec.Header().Get("Cache-Control"); got != "no-store" { + t.Fatalf("Cache-Control = %q, want no-store", got) + } + body := rec.Body.String() + for _, want := range []string{"window.__LINGNIU_APP_CONFIG__=", `"amapWebJsKey":"web-key"`, `"amapSecurityJsCode":"security-code"`, `"amapSecurityServiceHost":"/_AMapService"`} { + if !strings.Contains(body, want) { + t.Fatalf("app config script missing %q: %s", want, body) + } + } +} diff --git a/vehicle-data-platform/apps/api/internal/config/config.go b/vehicle-data-platform/apps/api/internal/config/config.go index 04731300..f2756cda 100644 --- a/vehicle-data-platform/apps/api/internal/config/config.go +++ b/vehicle-data-platform/apps/api/internal/config/config.go @@ -20,6 +20,9 @@ type Config struct { CapacityCheckBin string AuthToken string RequestTimeout time.Duration + AMapWebJSKey string + AMapSecurityCode string + AMapServiceHost string } func Load() Config { @@ -37,6 +40,9 @@ func Load() Config { CapacityCheckBin: os.Getenv("CAPACITY_CHECK_BIN"), AuthToken: os.Getenv("AUTH_TOKEN"), RequestTimeout: time.Duration(envInt("REQUEST_TIMEOUT_MS", 5000)) * time.Millisecond, + AMapWebJSKey: os.Getenv("AMAP_WEB_JS_KEY"), + AMapSecurityCode: os.Getenv("AMAP_SECURITY_JS_CODE"), + AMapServiceHost: os.Getenv("AMAP_SECURITY_SERVICE_HOST"), } } diff --git a/vehicle-data-platform/apps/web/index.html b/vehicle-data-platform/apps/web/index.html index 18d0c83e..83c5c03e 100644 --- a/vehicle-data-platform/apps/web/index.html +++ b/vehicle-data-platform/apps/web/index.html @@ -7,6 +7,7 @@
+