2021-11-01 17:39:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-02-11 18:39:07 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2021-11-01 17:39:08 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
|
|
|
)
|
|
|
|
|
2022-02-11 18:39:07 +00:00
|
|
|
func (a *goBlog) serveFs(f fs.FS, basePath string) http.HandlerFunc {
|
2021-11-01 17:39:08 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fileName := strings.TrimPrefix(r.URL.Path, basePath)
|
2022-02-11 18:39:07 +00:00
|
|
|
file, err := f.Open(fileName)
|
2021-11-01 17:39:08 +00:00
|
|
|
if err != nil {
|
|
|
|
a.serve404(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch path.Ext(fileName) {
|
|
|
|
case ".js":
|
2022-02-11 18:39:07 +00:00
|
|
|
w.Header().Set(contentType, contenttype.JSUTF8)
|
2022-04-10 09:46:35 +00:00
|
|
|
_ = a.min.Get().Minify(contenttype.JS, w, file)
|
2021-11-01 17:39:08 +00:00
|
|
|
case ".css":
|
2022-02-11 18:39:07 +00:00
|
|
|
w.Header().Set(contentType, contenttype.CSSUTF8)
|
2022-04-10 09:46:35 +00:00
|
|
|
_ = a.min.Get().Minify(contenttype.CSS, w, file)
|
|
|
|
default:
|
|
|
|
_, _ = io.Copy(w, file)
|
2021-11-01 17:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|