...
1 package middleware
2
3
4
5
6 import (
7 "net/http"
8 "time"
9 )
10
11
12 var epoch = time.Unix(0, 0).Format(time.RFC1123)
13
14
15 var noCacheHeaders = map[string]string{
16 "Expires": epoch,
17 "Cache-Control": "no-cache, no-store, no-transform, must-revalidate, private, max-age=0",
18 "Pragma": "no-cache",
19 "X-Accel-Expires": "0",
20 }
21
22 var etagHeaders = []string{
23 "ETag",
24 "If-Modified-Since",
25 "If-Match",
26 "If-None-Match",
27 "If-Range",
28 "If-Unmodified-Since",
29 }
30
31
32
33
34
35
36
37
38
39 func NoCache(h http.Handler) http.Handler {
40 fn := func(w http.ResponseWriter, r *http.Request) {
41
42
43 for _, v := range etagHeaders {
44 if r.Header.Get(v) != "" {
45 r.Header.Del(v)
46 }
47 }
48
49
50 for k, v := range noCacheHeaders {
51 w.Header().Set(k, v)
52 }
53
54 h.ServeHTTP(w, r)
55 }
56
57 return http.HandlerFunc(fn)
58 }
59
View as plain text