diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-07 00:24:53 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-07 00:24:53 -0700 |
commit | b2e4fd9372f27d3202f07a329a5077b93a4b7390 (patch) | |
tree | 7a00e7753fdf3b4b8f8976e35fb4dd4c564c45a9 /system/admin/cache.go | |
parent | b5f028f0a720f1d23d2ce79d3e885fcb524bb79a (diff) |
adding cache control and etags to responses for static assets + moved handlers/helper upload func
Diffstat (limited to 'system/admin/cache.go')
-rw-r--r-- | system/admin/cache.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/system/admin/cache.go b/system/admin/cache.go new file mode 100644 index 0000000..9962249 --- /dev/null +++ b/system/admin/cache.go @@ -0,0 +1,28 @@ +package admin + +import ( + "fmt" + "net/http" + "strings" + + "github.com/nilslice/cms/system/db" +) + +// CacheControl sets the default cache policy on static asset responses +func CacheControl(next http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + etag := db.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) { + res.WriteHeader(http.StatusNotModified) + return + } + } + + next.ServeHTTP(res, req) + }) +} |