package api import ( "io/fs" "net/http" "path" "strings" "github.com/gin-gonic/gin" ) // RegisterSPA serves the embedded single-page app for every route gin didn't // otherwise match. A request whose path maps to a real file (e.g. a hashed asset // bundle) is served directly; any other non-API path falls back to index.html so // client-side deep links like /gardens/42 load the app. Unmatched /api paths // return a JSON 404 rather than HTML. func RegisterSPA(r *gin.Engine, dist fs.FS) { fileServer := http.FileServer(http.FS(dist)) // index.html is served on every client-route deep link; read it once at // startup rather than re-reading the embedded FS per request. A build without // it is a packaging bug, so fail loudly. index, err := fs.ReadFile(dist, "index.html") if err != nil { panic("api: embedded web build missing index.html: " + err.Error()) } r.NoRoute(func(c *gin.Context) { reqPath := c.Request.URL.Path // API paths (including a bare "/api") are never the SPA: a miss is a real // JSON 404, not the app shell. if reqPath == "/api" || strings.HasPrefix(reqPath, "/api/") { writeAPIError(c, http.StatusNotFound, "NOT_FOUND", "no such endpoint") return } if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead { writeAPIError(c, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "method not allowed") return } c.Header("X-Content-Type-Options", "nosniff") name := strings.TrimPrefix(path.Clean(reqPath), "/") if name == "" { serveIndex(c, index) return } if info, statErr := fs.Stat(dist, name); statErr == nil { // An embedded directory is neither a client route nor a servable file; // serving it would leak a directory listing, so it is a 404. if info.IsDir() { writeAPIError(c, http.StatusNotFound, "NOT_FOUND", "not found") return } // Vite emits content-hashed filenames under assets/, so those are safe // to cache forever; index.html must always revalidate to pick up new builds. if strings.HasPrefix(name, "assets/") { c.Header("Cache-Control", "public, max-age=31536000, immutable") } else { c.Header("Cache-Control", "no-cache") } fileServer.ServeHTTP(c.Writer, c.Request) return } // A missing file under assets/ is a genuine 404, not a client route: // falling back to index.html would return HTML with a 200 for a .js/.css // request, which the browser rejects with a confusing MIME-type error. if strings.HasPrefix(name, "assets/") { writeAPIError(c, http.StatusNotFound, "NOT_FOUND", "asset not found") return } serveIndex(c, index) }) } // serveIndex writes the cached index.html as the SPA fallback (no-cache so new // builds are picked up). net/http suppresses the body for HEAD requests. func serveIndex(c *gin.Context, index []byte) { c.Header("Cache-Control", "no-cache") c.Data(http.StatusOK, "text/html; charset=utf-8", index) } // writeAPIError writes pansy's standard JSON error envelope. func writeAPIError(c *gin.Context, status int, code, message string) { c.JSON(status, gin.H{"error": gin.H{"code": code, "message": message}}) }