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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package db
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// CacheControl sets the default cache policy on static asset responses
func CacheControl(next http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
etag := ConfigCache("etag")
policy := fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 60*60*24*30)
res.Header().Add("Etag", etag)
res.Header().Add("Cache-Control", policy)
if match := res.Header().Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
fmt.Println("matched etag")
res.WriteHeader(http.StatusNotModified)
return
}
fmt.Println("checked, no match")
}
next.ServeHTTP(res, req)
})
}
// NewEtag generates a new Etag for response caching
func NewEtag() string {
now := fmt.Sprintf("%d", time.Now().Unix())
etag := base64.StdEncoding.EncodeToString([]byte(now))
return etag
}
// InvalidateCache sets a new Etag for http responses
func InvalidateCache() error {
kv := make(map[string]interface{})
c, err := ConfigAll()
if err != nil {
return err
}
err = json.Unmarshal(c, &kv)
if err != nil {
return err
}
kv["etag"] = NewEtag()
data := make(url.Values)
for k, v := range kv {
switch v.(type) {
case string:
data.Set(k, v.(string))
case []string:
vv := v.([]string)
for i := range vv {
if i == 0 {
data.Set(k, vv[i])
} else {
data.Add(k, vv[i])
}
}
}
}
err = SetConfig(data)
if err != nil {
}
return nil
}
|