summaryrefslogtreecommitdiff
path: root/system/admin/cache.go
diff options
context:
space:
mode:
Diffstat (limited to 'system/admin/cache.go')
-rw-r--r--system/admin/cache.go28
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)
+ })
+}