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() (ext.ErrorRequestHandler, 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) error {
20 var (
21 f = r.PathValue("file")
22 e = filepath.Ext(f)
23 m = mime.TypeByExtension(e)
24 )
25 ext.SetMIME(w, m)
26 w.Header().Add("Cache-Control", "max-age=31536000")
27 http.ServeFileFS(w, r, staticFs, f)
28 return nil
29 }, nil
30}