-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs.go
33 lines (31 loc) · 940 Bytes
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package sandwich
import (
"io/fs"
"net/http"
)
// ServeFS is a simple helper that will serve static files from an fs.FS
// filesystem. It allows serving files identified by a sandwich path parameter
// out of a subdirectory of the filesystem. This is especially useful when
// embedding static files:
//
// //go:embed server_files
// var all_files embed.FS
//
// mux.Get("/css/:path*", sandwich.ServeFS(all_files, "static/css", "path"))
// mux.Get("/js/:path*", sandwich.ServeFS(all_files, "dist/js", "path"))
// mux.Get("/i/:path*", sandwich.ServeFS(all_files, "static/images", "path"))
func ServeFS(
f fs.FS,
fsRoot string,
pathParam string,
) func(w http.ResponseWriter, r *http.Request, p Params) {
sub, err := fs.Sub(f, fsRoot)
if err != nil {
panic(err)
}
handler := http.FileServer(http.FS(sub))
return func(w http.ResponseWriter, r *http.Request, p Params) {
r.URL.Path = p[pathParam]
handler.ServeHTTP(w, r)
}
}