improved static file handling and caching
This commit is contained in:
parent
9789c4c467
commit
b16713de56
24
web/cachedfs.go
Normal file
24
web/cachedfs.go
Normal 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)
|
||||
}
|
||||
}
|
11
web/serve.go
11
web/serve.go
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user