move templates code into another project

This commit is contained in:
Ted Unangst 2019-04-29 13:15:14 -04:00
parent bf505478ce
commit 8aed2d59ab
4 changed files with 11 additions and 91 deletions

2
go.mod
View File

@ -6,5 +6,5 @@ require (
golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
humungus.tedunangst.com/r/go-sqlite3 v1.1.2 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
) )

4
go.sum
View File

@ -12,5 +12,5 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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 h1:bRAXNRZ4VNFRFhhG4tdudK4Lv4ktHQAHEppKlDANUFg=
humungus.tedunangst.com/r/go-sqlite3 v1.1.2/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= 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.2.0 h1:K0PJK4ZFNB+B0vGbs6huY5br35Z9xR4T6XpcFHmwXgQ=
humungus.tedunangst.com/r/webs v0.1.0/go.mod h1:6yLLDXBaE4pKURa/3/bxoQPod37uAqc/Kq8J0IopWW0= humungus.tedunangst.com/r/webs v0.2.0/go.mod h1:6yLLDXBaE4pKURa/3/bxoQPod37uAqc/Kq8J0IopWW0=

15
honk.go
View File

@ -37,6 +37,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"humungus.tedunangst.com/r/webs/login" "humungus.tedunangst.com/r/webs/login"
"humungus.tedunangst.com/r/webs/templates"
) )
type WhatAbout struct { type WhatAbout struct {
@ -87,7 +88,7 @@ type Honker struct {
var serverName string var serverName string
var iconName = "icon.png" var iconName = "icon.png"
var readviews *Template var readviews *templates.Template
func getInfo(r *http.Request) map[string]interface{} { func getInfo(r *http.Request) map[string]interface{} {
templinfo := make(map[string]interface{}) templinfo := make(map[string]interface{})
@ -145,7 +146,7 @@ func homepage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=0") w.Header().Set("Cache-Control", "max-age=0")
} }
w.Header().Set("Last-Modified", modtime.Format(http.TimeFormat)) 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 { if err != nil {
log.Print(err) log.Print(err)
} }
@ -566,7 +567,7 @@ func honkpage(w http.ResponseWriter, r *http.Request, u *login.UserInfo, user *W
} }
templinfo["Honks"] = honks templinfo["Honks"] = honks
templinfo["ServerMessage"] = infomsg templinfo["ServerMessage"] = infomsg
err := readviews.ExecuteTemplate(w, "honkpage.html", templinfo) err := readviews.Execute(w, "honkpage.html", templinfo)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -968,7 +969,7 @@ func viewhonkers(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r) templinfo := getInfo(r)
templinfo["Honkers"] = gethonkers(userinfo.UserID) templinfo["Honkers"] = gethonkers(userinfo.UserID)
templinfo["HonkerCSRF"] = login.GetCSRF("savehonker", r) templinfo["HonkerCSRF"] = login.GetCSRF("savehonker", r)
err := readviews.ExecuteTemplate(w, "honkers.html", templinfo) err := readviews.Execute(w, "honkers.html", templinfo)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -1107,7 +1108,7 @@ func killzone(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r) templinfo := getInfo(r)
templinfo["Zonkers"] = zonkers templinfo["Zonkers"] = zonkers
templinfo["KillCSRF"] = login.GetCSRF("killitwithfire", r) templinfo["KillCSRF"] = login.GetCSRF("killitwithfire", r)
err = readviews.ExecuteTemplate(w, "zonkers.html", templinfo) err = readviews.Execute(w, "zonkers.html", templinfo)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -1165,7 +1166,7 @@ func servecss(w http.ResponseWriter, r *http.Request) {
} }
func servehtml(w http.ResponseWriter, r *http.Request) { func servehtml(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r) 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 { if err != nil {
log.Print(err) log.Print(err)
} }
@ -1205,7 +1206,7 @@ func serve() {
debug := false debug := false
getconfig("debug", &debug) getconfig("debug", &debug)
readviews = ParseTemplates(debug, readviews = templates.Load(debug,
"views/honkpage.html", "views/honkpage.html",
"views/honkers.html", "views/honkers.html",
"views/zonkers.html", "views/zonkers.html",

View File

@ -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
}