goveal/internal/app/routing/no_cache_handler.go
Peter Kurfer e8d6de4d3c
Update to Reveal.JS 4.1.2 and Go 1.17
- Replace pkger with Go 1.17 embedded file system
2021-11-12 13:08:56 +01:00

56 lines
1.1 KiB
Go

package routing
import (
"net/http"
"strings"
"time"
)
var epoch = time.Unix(0, 0).Format(time.RFC1123)
var noCacheHeaders = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
func NoCache(h http.Handler, pathsToDisableCache []string) http.Handler {
pathLookup := make(map[string]bool)
for idx := range pathsToDisableCache {
pathLookup[strings.ToLower(pathsToDisableCache[idx])] = true
}
fn := func(w http.ResponseWriter, r *http.Request) {
if _, shouldBeHandled := pathLookup[strings.ToLower(r.URL.Path)]; !shouldBeHandled {
h.ServeHTTP(w, r)
return
}
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
// Set our NoCache headers
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}