start handling js like an asset

This commit is contained in:
Ted Unangst 2019-09-18 22:17:50 -04:00
parent 2d19f53cb8
commit 58395536d1
4 changed files with 28 additions and 22 deletions

View File

@ -51,10 +51,10 @@ import (
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
) )
var savedstyleparams = make(map[string]string) var savedassetparams = make(map[string]string)
func getstyleparam(file string) string { func getassetparam(file string) string {
if p, ok := savedstyleparams[file]; ok { if p, ok := savedassetparams[file]; ok {
return p return p
} }
data, err := ioutil.ReadFile(file) data, err := ioutil.ReadFile(file)

View File

@ -10,7 +10,6 @@
{{ template "honkform.html" . }} {{ template "honkform.html" . }}
{{ end }} {{ end }}
</div> </div>
{{ $BonkCSRF := .HonkCSRF }}
{{ if .TopXID }} {{ if .TopXID }}
<div class="info" id="refreshbox"> <div class="info" id="refreshbox">
<p><button onclick="refreshhonks(this)">refresh</button><span></span> <p><button onclick="refreshhonks(this)">refresh</button><span></span>
@ -18,14 +17,19 @@
{{ end }} {{ end }}
<div id="honksonpage"> <div id="honksonpage">
<div> <div>
{{ $BonkCSRF := .HonkCSRF }}
{{ range .Honks }} {{ range .Honks }}
{{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }}
{{ end }} {{ end }}
</div> </div>
</div> </div>
</main> </main>
{{ if $BonkCSRF }} {{ if .HonkCSRF }}
<script> <script>
{{ template "honkpage.js" . }} var csrftoken = {{ .HonkCSRF }}
var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" }
var honksforpage = { }
var thispagename = "{{ .PageName }}"
</script> </script>
<script src="/honkpage.js{{ .JSParam }}"></script>
{{ end }} {{ end }}

View File

@ -1,4 +1,3 @@
{{ $BonkCSRF := .HonkCSRF }}
function encode(hash) { function encode(hash) {
var s = [] var s = []
for (var key in hash) { for (var key in hash) {
@ -23,17 +22,17 @@ function get(url, whendone) {
function bonk(el, xid) { function bonk(el, xid) {
el.innerHTML = "bonked" el.innerHTML = "bonked"
el.disabled = true el.disabled = true
post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid)) post("/bonk", encode({"CSRF": csrftoken, "xid": xid}))
} }
function unbonk(el, xid) { function unbonk(el, xid) {
el.innerHTML = "unbonked" el.innerHTML = "unbonked"
el.disabled = true 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) { function muteit(el, convoy) {
el.innerHTML = "muted" el.innerHTML = "muted"
el.disabled = true 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') var els = document.querySelectorAll('article.honk')
for (var i = 0; i < els.length; i++) { for (var i = 0; i < els.length; i++) {
var e = els[i] var e = els[i]
@ -45,7 +44,7 @@ function muteit(el, convoy) {
function zonkit(el, xid) { function zonkit(el, xid) {
el.innerHTML = "zonked" el.innerHTML = "zonked"
el.disabled = true el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid)) post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
var p = el var p = el
while (p && p.tagName != "ARTICLE") { while (p && p.tagName != "ARTICLE") {
p = p.parentElement p = p.parentElement
@ -57,16 +56,13 @@ function zonkit(el, xid) {
function ackit(el, xid) { function ackit(el, xid) {
el.innerHTML = "acked" el.innerHTML = "acked"
el.disabled = true 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) { function deackit(el, xid) {
el.innerHTML = "deacked" el.innerHTML = "deacked"
el.disabled = true 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) { function fillinhonks(xhr) {
var doc = xhr.responseXML var doc = xhr.responseXML
topxid[thispagename] = doc.children[0].children[1].children[0].innerText topxid[thispagename] = doc.children[0].children[1].children[0].innerText

18
web.go
View File

@ -73,8 +73,9 @@ func getuserstyle(u *login.UserInfo) template.CSS {
func getInfo(r *http.Request) map[string]interface{} { func getInfo(r *http.Request) map[string]interface{} {
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
templinfo := make(map[string]interface{}) templinfo := make(map[string]interface{})
templinfo["StyleParam"] = getstyleparam("views/style.css") templinfo["StyleParam"] = getassetparam("views/style.css")
templinfo["LocalStyleParam"] = getstyleparam("views/local.css") templinfo["LocalStyleParam"] = getassetparam("views/local.css")
templinfo["JSParam"] = getassetparam("views/honkpage.js")
templinfo["UserStyle"] = getuserstyle(u) templinfo["UserStyle"] = getuserstyle(u)
templinfo["ServerName"] = serverName templinfo["ServerName"] = serverName
templinfo["IconName"] = iconName 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.Header().Set("Content-Type", "text/css; charset=utf-8")
w.Write([]byte(s)) 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) { func servehtml(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r) templinfo := getInfo(r)
err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo) err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo)
@ -1449,10 +1454,10 @@ func serve() {
"views/honkpage.js", "views/honkpage.js",
) )
if !debug { if !debug {
s := "views/style.css" assets := []string{"views/style.css", "views/local.css", "views/honkpage.js"}
savedstyleparams[s] = getstyleparam(s) for _, s := range assets {
s = "views/local.css" savedassetparams[s] = getassetparam(s)
savedstyleparams[s] = getstyleparam(s) }
} }
bitethethumbs() bitethethumbs()
@ -1485,6 +1490,7 @@ func serve() {
getters.HandleFunc("/style.css", servecss) getters.HandleFunc("/style.css", servecss)
getters.HandleFunc("/local.css", servecss) getters.HandleFunc("/local.css", servecss)
getters.HandleFunc("/honkpage.js", serveasset)
getters.HandleFunc("/about", servehtml) getters.HandleFunc("/about", servehtml)
getters.HandleFunc("/login", servehtml) getters.HandleFunc("/login", servehtml)
posters.HandleFunc("/dologin", login.LoginFunc) posters.HandleFunc("/dologin", login.LoginFunc)