feat(platform): integrate runtime amap vehicle maps

This commit is contained in:
lingniu
2026-07-04 10:52:11 +08:00
parent bb1b2f1c46
commit adb9d146d9
12 changed files with 402 additions and 51 deletions

View File

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

View File

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

View File

@@ -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"),
}
}