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:
15
group.go
15
group.go
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user