From 58395536d10528bd7c97cc35f454c99ab0ea31e8 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 18 Sep 2019 22:17:50 -0400 Subject: [PATCH] start handling js like an asset --- util.go | 6 +++--- views/honkpage.html | 10 +++++++--- views/honkpage.js | 16 ++++++---------- web.go | 18 ++++++++++++------ 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/util.go b/util.go index e185b37..e02ad0e 100644 --- a/util.go +++ b/util.go @@ -51,10 +51,10 @@ import ( "humungus.tedunangst.com/r/webs/httpsig" ) -var savedstyleparams = make(map[string]string) +var savedassetparams = make(map[string]string) -func getstyleparam(file string) string { - if p, ok := savedstyleparams[file]; ok { +func getassetparam(file string) string { + if p, ok := savedassetparams[file]; ok { return p } data, err := ioutil.ReadFile(file) diff --git a/views/honkpage.html b/views/honkpage.html index 541865f..5041c95 100644 --- a/views/honkpage.html +++ b/views/honkpage.html @@ -10,7 +10,6 @@ {{ template "honkform.html" . }} {{ end }} -{{ $BonkCSRF := .HonkCSRF }} {{ if .TopXID }}

@@ -18,14 +17,19 @@ {{ end }}

+{{ $BonkCSRF := .HonkCSRF }} {{ range .Honks }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ end }}
-{{ if $BonkCSRF }} +{{ if .HonkCSRF }} + {{ end }} diff --git a/views/honkpage.js b/views/honkpage.js index 5bc002b..6e9c8e3 100644 --- a/views/honkpage.js +++ b/views/honkpage.js @@ -1,4 +1,3 @@ -{{ $BonkCSRF := .HonkCSRF }} function encode(hash) { var s = [] for (var key in hash) { @@ -23,17 +22,17 @@ function get(url, whendone) { function bonk(el, xid) { el.innerHTML = "bonked" el.disabled = true - post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid)) + post("/bonk", encode({"CSRF": csrftoken, "xid": xid})) } function unbonk(el, xid) { el.innerHTML = "unbonked" el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid)) + post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid})) } function muteit(el, convoy) { el.innerHTML = "muted" el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(convoy)) + post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy})) var els = document.querySelectorAll('article.honk') for (var i = 0; i < els.length; i++) { var e = els[i] @@ -45,7 +44,7 @@ function muteit(el, convoy) { function zonkit(el, xid) { el.innerHTML = "zonked" el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid)) + post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid})) var p = el while (p && p.tagName != "ARTICLE") { p = p.parentElement @@ -57,16 +56,13 @@ function zonkit(el, xid) { function ackit(el, xid) { el.innerHTML = "acked" el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid)) + post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "ack", "what": xid})) } function deackit(el, xid) { el.innerHTML = "deacked" el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid)) + post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "deack", "what": xid})) } -var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" } -var honksforpage = { } -var thispagename = "{{ .PageName }}" function fillinhonks(xhr) { var doc = xhr.responseXML topxid[thispagename] = doc.children[0].children[1].children[0].innerText diff --git a/web.go b/web.go index 206ff3e..58b2272 100644 --- a/web.go +++ b/web.go @@ -73,8 +73,9 @@ func getuserstyle(u *login.UserInfo) template.CSS { func getInfo(r *http.Request) map[string]interface{} { u := login.GetUserInfo(r) templinfo := make(map[string]interface{}) - templinfo["StyleParam"] = getstyleparam("views/style.css") - templinfo["LocalStyleParam"] = getstyleparam("views/local.css") + templinfo["StyleParam"] = getassetparam("views/style.css") + templinfo["LocalStyleParam"] = getassetparam("views/local.css") + templinfo["JSParam"] = getassetparam("views/honkpage.js") templinfo["UserStyle"] = getuserstyle(u) templinfo["ServerName"] = serverName templinfo["IconName"] = iconName @@ -1334,6 +1335,10 @@ func servecss(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/css; charset=utf-8") w.Write([]byte(s)) } +func serveasset(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "max-age=7776000") + http.ServeFile(w, r, "views"+r.URL.Path) +} func servehtml(w http.ResponseWriter, r *http.Request) { templinfo := getInfo(r) err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo) @@ -1449,10 +1454,10 @@ func serve() { "views/honkpage.js", ) if !debug { - s := "views/style.css" - savedstyleparams[s] = getstyleparam(s) - s = "views/local.css" - savedstyleparams[s] = getstyleparam(s) + assets := []string{"views/style.css", "views/local.css", "views/honkpage.js"} + for _, s := range assets { + savedassetparams[s] = getassetparam(s) + } } bitethethumbs() @@ -1485,6 +1490,7 @@ func serve() { getters.HandleFunc("/style.css", servecss) getters.HandleFunc("/local.css", servecss) + getters.HandleFunc("/honkpage.js", serveasset) getters.HandleFunc("/about", servehtml) getters.HandleFunc("/login", servehtml) posters.HandleFunc("/dologin", login.LoginFunc)