1package static
2
3import (
4 "io/fs"
5 "mime"
6 "net/http"
7 "path/filepath"
8
9 "git.gabrielgio.me/cerrado/pkg/ext"
10 "git.gabrielgio.me/cerrado/static"
11)
12
13func ServeStaticHandler() (func(w http.ResponseWriter, r *http.Request), error) {
14 staticFs, err := fs.Sub(static.Static, ".")
15 if err != nil {
16 return nil, err
17 }
18
19 return func(w http.ResponseWriter, r *http.Request) {
20 var (
21 f = r.PathValue("file")
22 e = filepath.Ext(f)
23 m = mime.TypeByExtension(e)
24 )
25 ext.SetMIME(w, m)
26 http.ServeFileFS(w, r, staticFs, f)
27 }, nil
28}