package frontend import ( "embed" "io/fs" "net/http" ) //go:embed all:build var files embed.FS // Handler returns an http.Handler that serves the embedded SvelteKit SPA. // Unknown paths fall back to index.html for client-side routing. func Handler() http.Handler { fsys, _ := fs.Sub(files, "build") fileServer := http.FileServer(http.FS(fsys)) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Try to serve static file first path := r.URL.Path if path == "/" { path = "/index.html" } // Check if the file exists in the embedded filesystem cleanPath := path[1:] // Remove leading slash if cleanPath == "" { cleanPath = "index.html" } if _, err := fs.Stat(fsys, cleanPath); err != nil { // SPA fallback: serve index.html for client-side routing r.URL.Path = "/" } fileServer.ServeHTTP(w, r) }) }