1
0
forked from MTSR/mapserver

improved static file handling and caching

This commit is contained in:
BuckarooBanzay 2022-03-19 17:23:18 +01:00
parent 9789c4c467
commit b16713de56
2 changed files with 34 additions and 1 deletions

24
web/cachedfs.go Normal file
View File

@ -0,0 +1,24 @@
package web
import (
"fmt"
"net/http"
"strings"
"time"
)
func CachedServeFunc(h http.HandlerFunc) http.HandlerFunc {
var etag = fmt.Sprintf(`"%d"`, time.Now().UnixMicro())
return func(w http.ResponseWriter, r *http.Request) {
if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified)
return
}
}
w.Header().Set("Cache-Control", "max-age=60")
w.Header().Set("ETag", etag)
h.ServeHTTP(w, r)
}
}

View File

@ -23,7 +23,16 @@ func Serve(ctx *app.App) {
mux := http.NewServeMux()
// static files
mux.Handle("/", http.FileServer(getFileSystem(ctx.Config.Webdev, public.Files)))
if ctx.Config.Webdev {
logrus.Print("using live mode")
fs := http.FileServer(http.FS(os.DirFS("public")))
mux.HandleFunc("/", fs.ServeHTTP)
} else {
logrus.Print("using embed mode")
fs := http.FileServer(http.FS(public.Files))
mux.HandleFunc("/", CachedServeFunc(fs.ServeHTTP))
}
tiles := &Tiles{ctx: ctx}
tiles.Init()