forked from cadey/xesite
cmd/site: etags
This commit is contained in:
parent
6ebd8a8692
commit
01089da57a
|
@ -1,4 +1,4 @@
|
|||
FROM xena/go:1.11.1 AS build
|
||||
FROM xena/go:1.11.5 AS build
|
||||
ENV GOPROXY https://cache.greedo.xeserv.us
|
||||
COPY . /site
|
||||
WORKDIR /site
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
)
|
||||
|
||||
var bootTime = time.Now()
|
||||
var etag = Hash(bootTime.String(), IncrediblySecureSalt)
|
||||
|
||||
// IncrediblySecureSalt *******
|
||||
const IncrediblySecureSalt = "hunter2"
|
||||
|
|
|
@ -3,4 +3,4 @@
|
|||
set -e
|
||||
|
||||
docker build -t xena/site .
|
||||
docker run --rm -itp 5000:5000 -e PORT=5000 xena/site
|
||||
exec docker run --rm -itp 5000:5000 -e PORT=5000 xena/site
|
||||
|
|
Loading…
Reference in New Issue