middleware/no_cache.go

42 lines
839 B
Go
Raw Normal View History

2020-11-07 11:59:33 +00:00
package middleware
import (
"net/http"
"time"
)
var epoch = time.Unix(0, 0).Format(time.RFC1123)
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
var noCacheHeaders = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, no-store, no-transform, must-revalidate, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
// NoCache sets a number of HTTP headers to prevent a router from being cached
func NoCache(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}
next.ServeHTTP(w, r)
})
}