all repos — honk @ 33eddd2dd9c9ed854a8549a8fda45d6c2ea91bc6

my fork of honk

move templates code into another project
Ted Unangst tedu@tedunangst.com
Mon, 29 Apr 2019 13:15:14 -0400
commit

33eddd2dd9c9ed854a8549a8fda45d6c2ea91bc6

parent

d23f5ab00e020cd65d2519fe2b1531e57e13d565

4 files changed, 11 insertions(+), 91 deletions(-)

jump to
M go.modgo.mod

@@ -6,5 +6,5 @@ github.com/mattn/go-runewidth v0.0.4

golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 humungus.tedunangst.com/r/go-sqlite3 v1.1.2 - humungus.tedunangst.com/r/webs v0.1.0 + humungus.tedunangst.com/r/webs v0.2.0 )
M go.sumgo.sum

@@ -12,5 +12,5 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= humungus.tedunangst.com/r/go-sqlite3 v1.1.2 h1:bRAXNRZ4VNFRFhhG4tdudK4Lv4ktHQAHEppKlDANUFg= humungus.tedunangst.com/r/go-sqlite3 v1.1.2/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= -humungus.tedunangst.com/r/webs v0.1.0 h1:TaJBDhgWWL66oK+6aldgn5BdSwTD+9epqhWHoKFc0iI= -humungus.tedunangst.com/r/webs v0.1.0/go.mod h1:6yLLDXBaE4pKURa/3/bxoQPod37uAqc/Kq8J0IopWW0= +humungus.tedunangst.com/r/webs v0.2.0 h1:K0PJK4ZFNB+B0vGbs6huY5br35Z9xR4T6XpcFHmwXgQ= +humungus.tedunangst.com/r/webs v0.2.0/go.mod h1:6yLLDXBaE4pKURa/3/bxoQPod37uAqc/Kq8J0IopWW0=
M honk.gohonk.go

@@ -37,6 +37,7 @@ "time"

"github.com/gorilla/mux" "humungus.tedunangst.com/r/webs/login" + "humungus.tedunangst.com/r/webs/templates" ) type WhatAbout struct {

@@ -87,7 +88,7 @@

var serverName string var iconName = "icon.png" -var readviews *Template +var readviews *templates.Template func getInfo(r *http.Request) map[string]interface{} { templinfo := make(map[string]interface{})

@@ -145,7 +146,7 @@ } else {

w.Header().Set("Cache-Control", "max-age=0") } w.Header().Set("Last-Modified", modtime.Format(http.TimeFormat)) - err := readviews.ExecuteTemplate(w, "honkpage.html", templinfo) + err := readviews.Execute(w, "honkpage.html", templinfo) if err != nil { log.Print(err) }

@@ -566,7 +567,7 @@ templinfo["WhatAbout"] = cleanstring(whatabout)

} templinfo["Honks"] = honks templinfo["ServerMessage"] = infomsg - err := readviews.ExecuteTemplate(w, "honkpage.html", templinfo) + err := readviews.Execute(w, "honkpage.html", templinfo) if err != nil { log.Print(err) }

@@ -968,7 +969,7 @@ userinfo := login.GetUserInfo(r)

templinfo := getInfo(r) templinfo["Honkers"] = gethonkers(userinfo.UserID) templinfo["HonkerCSRF"] = login.GetCSRF("savehonker", r) - err := readviews.ExecuteTemplate(w, "honkers.html", templinfo) + err := readviews.Execute(w, "honkers.html", templinfo) if err != nil { log.Print(err) }

@@ -1107,7 +1108,7 @@ }

templinfo := getInfo(r) templinfo["Zonkers"] = zonkers templinfo["KillCSRF"] = login.GetCSRF("killitwithfire", r) - err = readviews.ExecuteTemplate(w, "zonkers.html", templinfo) + err = readviews.Execute(w, "zonkers.html", templinfo) if err != nil { log.Print(err) }

@@ -1165,7 +1166,7 @@ http.ServeFile(w, r, "views"+r.URL.Path)

} func servehtml(w http.ResponseWriter, r *http.Request) { templinfo := getInfo(r) - err := readviews.ExecuteTemplate(w, r.URL.Path[1:]+".html", templinfo) + err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo) if err != nil { log.Print(err) }

@@ -1205,7 +1206,7 @@ go redeliverator()

debug := false getconfig("debug", &debug) - readviews = ParseTemplates(debug, + readviews = templates.Load(debug, "views/honkpage.html", "views/honkers.html", "views/zonkers.html",
D template.go

@@ -1,81 +0,0 @@

-// -// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com> -// -// Permission to use, copy, modify, and distribute this software for any -// purpose with or without fee is hereby granted, provided that the above -// copyright notice and this permission notice appear in all copies. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -package main - -import ( - "errors" - "html/template" - "io" - "log" -) - -type Template struct { - names []string - templates *template.Template - reload bool -} - -func mapmaker(values ...interface{}) (map[string]interface{}, error) { - if len(values)%2 != 0 { - return nil, errors.New("need arguments in pairs") - } - dict := make(map[string]interface{}, len(values)/2) - for i := 0; i < len(values); i += 2 { - key, ok := values[i].(string) - if !ok { - return nil, errors.New("key must be string") - } - dict[key] = values[i+1] - } - return dict, nil -} - -func loadtemplates(filenames ...string) (*template.Template, error) { - templates := template.New("") - templates.Funcs(template.FuncMap{ - "map": mapmaker, - }) - templates, err := templates.ParseFiles(filenames...) - if err != nil { - return nil, err - } - return templates, nil -} - -func (t *Template) ExecuteTemplate(w io.Writer, name string, data interface{}) error { - if t.reload { - templates, err := loadtemplates(t.names...) - if err != nil { - return err - } - return templates.ExecuteTemplate(w, name, data) - } - return t.templates.ExecuteTemplate(w, name, data) -} - -func ParseTemplates(reload bool, filenames ...string) *Template { - t := new(Template) - t.names = filenames - t.reload = reload - templates, err := loadtemplates(filenames...) - if err != nil { - log.Panic(err) - } - if !reload { - t.templates = templates - } - return t -}