start handling js like an asset
Ted Unangst tedu@tedunangst.com
Wed, 18 Sep 2019 22:17:50 -0400
4 files changed,
28 insertions(+),
22 deletions(-)
M
util.go
→
util.go
@@ -51,10 +51,10 @@ _ "humungus.tedunangst.com/r/go-sqlite3"
"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)
M
views/honkpage.html
→
views/honkpage.html
@@ -10,7 +10,6 @@ {{ if .HonkCSRF }}
{{ template "honkform.html" . }} {{ end }} </div> -{{ $BonkCSRF := .HonkCSRF }} {{ if .TopXID }} <div class="info" id="refreshbox"> <p><button onclick="refreshhonks(this)">refresh</button><span></span>@@ -18,14 +17,19 @@ </div>
{{ end }} <div id="honksonpage"> <div> +{{ $BonkCSRF := .HonkCSRF }} {{ range .Honks }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ end }} </div> </div> </main> -{{ if $BonkCSRF }} +{{ if .HonkCSRF }} <script> -{{ template "honkpage.js" . }} +var csrftoken = {{ .HonkCSRF }} +var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" } +var honksforpage = { } +var thispagename = "{{ .PageName }}" </script> +<script src="/honkpage.js{{ .JSParam }}"></script> {{ end }}
M
views/honkpage.js
→
views/honkpage.js
@@ -1,4 +1,3 @@
-{{ $BonkCSRF := .HonkCSRF }} function encode(hash) { var s = [] for (var key in hash) {@@ -23,17 +22,17 @@ }
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 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 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
M
web.go
→
web.go
@@ -73,8 +73,9 @@
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 @@ w.Header().Set("Cache-Control", "max-age=7776000")
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 @@ "views/onts.html",
"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 @@ getters.HandleFunc("/.well-known/webfinger", fingerlicker)
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)