allow overriding css with local.css. from qbit@bsd.network

This commit is contained in:
Ted Unangst 2019-04-12 13:12:18 -04:00
parent dea10a4bd5
commit 75cd95d751
3 changed files with 19 additions and 7 deletions

View File

@ -116,7 +116,8 @@ func keymatch(keyname string, actor string) bool {
func getInfo(r *http.Request) map[string]interface{} {
templinfo := make(map[string]interface{})
templinfo["StyleParam"] = getstyleparam()
templinfo["StyleParam"] = getstyleparam("views/style.css")
templinfo["LocalStyleParam"] = getstyleparam("views/local.css")
templinfo["ServerName"] = serverName
templinfo["IconName"] = iconName
templinfo["UserInfo"] = GetUserInfo(r)
@ -1123,7 +1124,10 @@ func serve() {
"views/header.html",
)
if !debug {
savedstyleparam = getstyleparam()
s := "views/style.css"
savedstyleparams[s] = getstyleparam(s)
s = "views/local.css"
savedstyleparams[s] = getstyleparam(s)
}
mux := mux.NewRouter()
@ -1145,6 +1149,7 @@ func serve() {
getters.HandleFunc("/.well-known/webfinger", fingerlicker)
getters.HandleFunc("/style.css", servecss)
getters.HandleFunc("/local.css", servecss)
getters.HandleFunc("/login", servehtml)
posters.HandleFunc("/dologin", dologin)
getters.HandleFunc("/logout", dologout)

14
util.go
View File

@ -49,15 +49,19 @@ import (
_ "humungus.tedunangst.com/r/go-sqlite3"
)
var savedstyleparam string
var savedstyleparams = make(map[string]string)
func getstyleparam() string {
if savedstyleparam != "" {
return savedstyleparam
func getstyleparam(file string) string {
if p, ok := savedstyleparams[file]; ok {
return p
}
data, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
data, _ := ioutil.ReadFile("views/style.css")
hasher := sha512.New()
hasher.Write(data)
return fmt.Sprintf("?v=%.8x", hasher.Sum(nil))
}

View File

@ -3,6 +3,9 @@
<head>
<title>honk</title>
<link href="/style.css{{ .StyleParam }}" rel="stylesheet">
{{ if .LocalStyleParam }}
<link href="/local.css{{ .LocalStyleParam }}" rel="stylesheet">
{{ end }}
<link href="/icon.png" rel="icon">
</head>
<body>