package storefrontshell import ( "context" "net/http" "strconv" "strings" "hfb_sys/backend/internal/sitecode" "hfb_sys/backend/internal/tenancy" "hfb_sys/backend/pkg/response" "github.com/gin-gonic/gin" "go.uber.org/zap" ) type shellProvider interface { Get(ctx context.Context) ([]byte, error) } // Handler serves the SPA shell for extensionless document requests. Caddy // routes only HTML document paths here; API and static asset paths stay on // their existing handlers. type Handler struct { provider shellProvider logger *zap.Logger } func NewHandler(provider shellProvider, logger *zap.Logger) *Handler { return &Handler{provider: provider, logger: logger} } func (h *Handler) Serve(c *gin.Context) { if h == nil || h.provider == nil || !IsDocumentPath(c.Request.URL.Path) { response.NotFound(c, "页面不存在") return } if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead { response.NotFound(c, "页面不存在") return } shell, err := h.provider.Get(c.Request.Context()) if err != nil { if h.logger != nil { h.logger.Error("读取前台 HTML 模板失败", zap.Error(err)) } writeUnavailable(c) return } meta := MetaFromRequest(c) page := Inject(shell, meta) etag := ETag(meta, shell) c.Header("Cache-Control", "no-cache") c.Header("Vary", "Host") c.Header("ETag", etag) if etagMatches(c.GetHeader("If-None-Match"), etag) { c.Status(http.StatusNotModified) c.Writer.WriteHeaderNow() return } if c.Request.Method == http.MethodHead { c.Header("Content-Type", "text/html; charset=utf-8") c.Header("Content-Length", strconv.Itoa(len(page))) c.Status(http.StatusOK) c.Writer.WriteHeaderNow() return } c.Data(http.StatusOK, "text/html; charset=utf-8", page) } // IsDocumentPath guards the NoRoute handler. Caddy already filters document // requests, but this keeps direct backend requests from turning API/static // misses into HTML responses. func IsDocumentPath(path string) bool { if path == "" || path == "/" { return true } for _, prefix := range []string{"/api", "/r", "/health", "/assets", "/swagger", "/docs"} { if path == prefix || strings.HasPrefix(path, prefix+"/") { return false } } segment := path if index := strings.LastIndex(path, "/"); index >= 0 { segment = path[index+1:] } return !strings.Contains(segment, ".") } func MetaFromRequest(c *gin.Context) Meta { profile := map[string]any{} tenantCode := "" tenantName := "" if scope, ok := tenancy.FromContext(c.Request.Context()); ok { profile = scope.Profile() tenantCode = scope.TenantCode tenantName = scope.TenantName } scheme := strings.TrimSpace(c.GetHeader("X-Forwarded-Proto")) if scheme == "" { if c.Request.TLS != nil { scheme = "https" } else { scheme = "http" } } host := sitecode.ExtractHost(c.Request.Host) if host == "" { host = c.Request.Host } path := c.Request.URL.EscapedPath() if path == "" { path = "/" } return BuildMeta(profile, tenantCode, tenantName, scheme, host, path) } func etagMatches(header, etag string) bool { for _, part := range strings.Split(header, ",") { if strings.TrimSpace(part) == etag { return true } } return false } const unavailablePage = ` 页面暂时不可用

页面暂时不可用

请稍后刷新重试。

` func writeUnavailable(c *gin.Context) { c.Header("Cache-Control", "no-store") c.Header("Retry-After", "3") c.Data(http.StatusServiceUnavailable, "text/html; charset=utf-8", []byte(unavailablePage)) }