1
mirror of https://github.com/jlelse/GoBlog synced 2024-07-15 10:42:58 +00:00
GoBlog/httpFs.go

35 lines
749 B
Go

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()
}
}