/
nocache.go
58 lines (48 loc) · 1.51 KB
/
nocache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package middleware
import (
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// Unix epoch time.
var epoch = time.Unix(0, 0).Format(time.RFC1123)
// 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",
}
// NoCacheConfig used to configure the no cache middleware.
type NoCacheConfig struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
}
// NoCache returns a middleware which sets the no cache headers with default configuration.
func NoCache() echo.MiddlewareFunc {
return NoCacheWithConfig(NoCacheConfig{})
}
// NoCacheWithConfig returns a middleware which sets a number of http headers to ensure the resource is not cached.
//
// 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 NoCacheWithConfig(config NoCacheConfig) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = middleware.DefaultSkipper
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if config.Skipper(c) {
return next(c)
}
for k, v := range noCacheHeaders {
c.Response().Header().Set(k, v)
}
return next(c)
}
}
}