feat(platform): integrate runtime amap vehicle maps
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user