-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathhttpFs.go
More file actions
34 lines (31 loc) · 749 Bytes
/
httpFs.go
File metadata and controls
34 lines (31 loc) · 749 Bytes
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
34
package main
import (
"io"
"io/fs"
"net/http"
"path"
"strings"
"go.goblog.app/app/pkgs/contenttype"
)
func (a *goBlog) serveFs(f fs.FS, basePath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fileName := strings.TrimPrefix(r.URL.Path, basePath)
file, err := f.Open(fileName)
if err != nil {
a.serve404(w, r)
return
}
switch path.Ext(fileName) {
case ".js":
w.Header().Set(contentType, contenttype.JSUTF8)
// Can't minify, because minify throws errors for some JS files
_, _ = io.Copy(w, file)
case ".css":
w.Header().Set(contentType, contenttype.CSSUTF8)
_ = a.min.Get().Minify(contenttype.CSS, w, file)
default:
_, _ = io.Copy(w, file)
}
_ = file.Close()
}
}