summaryrefslogtreecommitdiff
path: root/system/admin/cache.go
blob: 2abdb26563236568e6f795300cfe7a5d395c1de1 (plain)
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
package admin

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/bosssauce/ponzu/system/db"
)

// CacheControl sets the default cache policy on static asset responses
func CacheControl(next http.Handler) http.Handler {
	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)
	})
}