diff options
| author | Christine Dodrill <me@christine.website> | 2019-01-26 06:38:22 -0800 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-01-26 06:38:29 -0800 |
| commit | 01089da57a0a68d96641beb53fed73b4e08bfe9e (patch) | |
| tree | e51b763691ee637b6dabfc7d844286df28404002 /cmd | |
| parent | 6ebd8a86923c9fdd350cf72cd7a54bfbf6e69093 (diff) | |
| download | xesite-01089da57a0a68d96641beb53fed73b4e08bfe9e.tar.xz xesite-01089da57a0a68d96641beb53fed73b4e08bfe9e.zip | |
cmd/site: etags
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/site/html.go | 19 | ||||
| -rw-r--r-- | cmd/site/rss.go | 1 |
2 files changed, 17 insertions, 3 deletions
diff --git a/cmd/site/html.go b/cmd/site/html.go index c851342..fe8d190 100644 --- a/cmd/site/html.go +++ b/cmd/site/html.go @@ -10,14 +10,24 @@ import ( "within.website/ln" ) -func logTemplateTime(name string, from time.Time) { +func logTemplateTime(name string, f ln.F, from time.Time) { now := time.Now() - ln.Log(context.Background(), ln.F{"action": "template_rendered", "dur": now.Sub(from).String(), "name": name}) + ln.Log(context.Background(), f, ln.F{"action": "template_rendered", "dur": now.Sub(from).String(), "name": name}) } func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - defer logTemplateTime(templateFname, time.Now()) + fetag := "W/" + Hash(templateFname, etag) + "-1" + + f := ln.F{"etag": fetag, "if-none-match": r.Header.Get("If-None.Match")} + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(r.Context(), f, ln.Info("Cache hit")) + return + } + + defer logTemplateTime(templateFname, f, time.Now()) s.tlock.RLock() defer s.tlock.RUnlock() @@ -43,6 +53,9 @@ func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.H t = s.templates[templateFname] } + w.Header().Set("ETag", fetag) + w.Header().Set("Cache-Control", "max-age=432000") + err = t.Execute(w, data) if err != nil { panic(err) diff --git a/cmd/site/rss.go b/cmd/site/rss.go index b1845cd..935749f 100644 --- a/cmd/site/rss.go +++ b/cmd/site/rss.go @@ -9,6 +9,7 @@ import ( ) var bootTime = time.Now() +var etag = Hash(bootTime.String(), IncrediblySecureSalt) // IncrediblySecureSalt ******* const IncrediblySecureSalt = "hunter2" |
