felt/frontend/embed.go
Mikkel Georgsen af13732b2b feat(01-01): initialize Go module, dependency tree, and project scaffold
- Go module at github.com/felt-app/felt with go-libsql pinned to commit hash
- Full directory structure per research recommendations (cmd/leaf, internal/*, frontend/)
- Makefile with build, run, run-dev, test, frontend, all, clean targets
- LibSQL database with WAL mode, foreign keys, and embedded migration runner
- SvelteKit SPA stub served via go:embed
- Package stubs for all internal packages (server, nats, store, auth, clock, etc.)
- go build and go vet pass cleanly

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 03:34:44 +01:00

38 lines
867 B
Go

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)
})
}