Files
gin-contrib/nocache/nocache.go
T
2024-04-02 15:13:13 +03:00

57 lines
1.4 KiB
Go

package nocache
// Ported from Goji's middleware, source:
// https://github.com/zenazn/goji/tree/master/web/middleware
import (
"net/http"
"time"
"git.company.lan/gopkg/gin"
)
// Unix epoch time
var epoch = time.Unix(0, 0).UTC().Format(http.TimeFormat)
// Taken from https://github.com/mytrile/nocache
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",
}
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
// NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent
// a router (or subrouter) from being cached by an upstream proxy and/or client.
//
// As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:
//
// Expires: Thu, 01 Jan 1970 00:00:00 UTC
// Cache-Control: no-cache, private, max-age=0
// X-Accel-Expires: 0
// Pragma: no-cache (for HTTP/1.0 proxies/clients)
func NoCache() gin.HandlerFunc {
return func(c *gin.Context) {
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if c.Request.Header.Get(v) != "" {
c.Request.Header.Del(v)
}
}
// Set our NoCache headers
for k, v := range noCacheHeaders {
c.Writer.Header().Set(k, v)
}
}
}