feat: add Static, StaticFS and SPA file serving

- Add Static(prefix, root) to serve files from disk on Kite and Group
- Add StaticFS(prefix, fs) to serve from any http.FileSystem on Kite and Group
- Add SPA(root) and SPAFS(fs) for Single Page Application serving — falls back to index.html for unknown paths so client-side routing works correctly
This commit is contained in:
2026-04-29 22:15:12 +02:00
parent 22b4d9ace8
commit 36c6d76e53
2 changed files with 49 additions and 0 deletions

View File

@@ -8,6 +8,21 @@ type Group struct {
mws []Middleware
}
func (g *Group) Static(prefix, root string) {
g.StaticFS(prefix, http.Dir(root))
}
func (g *Group) StaticFS(prefix string, fs http.FileSystem) {
if prefix == "" || prefix[len(prefix)-1] != '/' {
prefix += "/"
}
fullPrefix := g.prefix + prefix
handler := http.StripPrefix(fullPrefix, http.FileServer(fs))
g.k.r.Handler(http.MethodGet, fullPrefix+"*filepath", handler)
g.k.r.Handler(http.MethodHead, fullPrefix+"*filepath", handler)
}
func (g *Group) Group(prefix string, mws ...Middleware) *Group {
return &Group{
k: g.k,