// Package admin embeds the built admin panel (Server/admin, output copied to // dist/ by `npm run build:admin`) and serves it as a single-page app. package admin import ( "embed" "io/fs" "net/http" "path" "strings" ) //go:embed all:dist var dist embed.FS // Handler serves the admin SPA mounted under prefix (e.g. "/admin"). // Unknown paths fall back to index.html so client-side routes work. func Handler(prefix string) http.Handler { files, _ := fs.Sub(dist, "dist") fileServer := http.FileServerFS(files) return http.StripPrefix(prefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(path.Clean("/"+r.URL.Path), "/") if name == "" { name = "index.html" } if _, err := fs.Stat(files, name); err != nil { name = "index.html" } if _, err := fs.Stat(files, "index.html"); err != nil { http.Error(w, "admin panel not built: run `npm run build:admin`", http.StatusServiceUnavailable) return } if name == "index.html" { w.Header().Set("Cache-Control", "no-cache") http.ServeFileFS(w, r, files, "index.html") return } if strings.HasPrefix(name, "assets/") { w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") } fileServer.ServeHTTP(w, r) })) }