aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-01-26 06:38:22 -0800
committerChristine Dodrill <me@christine.website>2019-01-26 06:38:29 -0800
commit01089da57a0a68d96641beb53fed73b4e08bfe9e (patch)
treee51b763691ee637b6dabfc7d844286df28404002
parent6ebd8a86923c9fdd350cf72cd7a54bfbf6e69093 (diff)
downloadxesite-01089da57a0a68d96641beb53fed73b4e08bfe9e.tar.xz
xesite-01089da57a0a68d96641beb53fed73b4e08bfe9e.zip
cmd/site: etags
-rw-r--r--Dockerfile2
-rw-r--r--cmd/site/html.go19
-rw-r--r--cmd/site/rss.go1
-rwxr-xr-xscripts/docker.sh2
4 files changed, 19 insertions, 5 deletions
diff --git a/Dockerfile b/Dockerfile
index 307276b..79fc553 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -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
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"
diff --git a/scripts/docker.sh b/scripts/docker.sh
index 05e4c88..8eb3c20 100755
--- a/scripts/docker.sh
+++ b/scripts/docker.sh
@@ -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