compile templates only once on app start instead of on every page request

This commit is contained in:
Iris Lightshard 2023-02-02 22:05:17 -07:00
parent 907dcc9eeb
commit 068bda4915
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
4 changed files with 19 additions and 39 deletions

View file

@ -3,8 +3,10 @@ package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"git.icyphox.sh/legit/config"
"git.icyphox.sh/legit/routes"
@ -29,7 +31,10 @@ func main() {
log.Fatalf("unveil: %s", err)
}
mux := routes.Handlers(c)
tpath := filepath.Join(c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
mux := routes.Handlers(c, t)
addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
log.Println("starting server on", addr)
log.Fatal(http.ListenAndServe(addr, mux))

View file

@ -2,6 +2,7 @@ package routes
import (
"net/http"
"html/template"
"git.icyphox.sh/legit/config"
"github.com/alexedwards/flow"
@ -29,9 +30,9 @@ func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
}
}
func Handlers(c *config.Config) *flow.Mux {
func Handlers(c *config.Config, t *template.Template) *flow.Mux {
mux := flow.New()
d := deps{c}
d := deps{c, t}
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)

View file

@ -21,6 +21,7 @@ import (
type deps struct {
c *config.Config
t *template.Template
}
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
@ -70,14 +71,11 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
return infos[j].d.Before(infos[i].d)
})
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
data["info"] = infos
if err := t.ExecuteTemplate(w, "index", data); err != nil {
if err := d.t.ExecuteTemplate(w, "index", data); err != nil {
log.Println(err)
return
}
@ -138,9 +136,6 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
log.Printf("no readme found for %s", name)
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
if len(commits) >= 3 {
commits = commits[:3]
}
@ -154,7 +149,7 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
data["servername"] = d.c.Server.Name
data["gomod"] = isGoModule(gr)
if err := t.ExecuteTemplate(w, "repo", data); err != nil {
if err := d.t.ExecuteTemplate(w, "repo", data); err != nil {
log.Println(err)
return
}
@ -247,9 +242,6 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
return
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["commits"] = commits
data["meta"] = d.c.Meta
@ -258,7 +250,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
data["desc"] = getDescription(path)
data["dotdot"] = filepath.Dir(path)
if err := t.ExecuteTemplate(w, "log", data); err != nil {
if err := d.t.ExecuteTemplate(w, "log", data); err != nil {
log.Println(err)
return
}
@ -286,9 +278,6 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
return
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["commit"] = diff.Commit
@ -299,7 +288,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
data["ref"] = ref
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "commit", data); err != nil {
if err := d.t.ExecuteTemplate(w, "commit", data); err != nil {
log.Println(err)
return
}
@ -332,9 +321,6 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
return
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
@ -343,7 +329,7 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
data["tags"] = tags
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "refs", data); err != nil {
if err := d.t.ExecuteTemplate(w, "refs", data); err != nil {
log.Println(err)
return
}

View file

@ -2,42 +2,33 @@ package routes
import (
"bytes"
"html/template"
"io"
"log"
"net/http"
"path/filepath"
"strings"
"git.icyphox.sh/legit/git"
)
func (d *deps) Write404(w http.ResponseWriter) {
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
w.WriteHeader(404)
if err := t.ExecuteTemplate(w, "404", nil); err != nil {
if err := d.t.ExecuteTemplate(w, "404", nil); err != nil {
log.Printf("404 template: %s", err)
}
}
func (d *deps) Write500(w http.ResponseWriter) {
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
w.WriteHeader(500)
if err := t.ExecuteTemplate(w, "500", nil); err != nil {
if err := d.t.ExecuteTemplate(w, "500", nil); err != nil {
log.Printf("500 template: %s", err)
}
}
func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data["files"] = files
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "tree", data); err != nil {
if err := d.t.ExecuteTemplate(w, "tree", data); err != nil {
log.Println(err)
return
}
@ -70,9 +61,6 @@ func countLines(r io.Reader) (int, error) {
}
func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) {
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
lc, err := countLines(strings.NewReader(content))
if err != nil {
// Non-fatal, we'll just skip showing line numbers in the template.
@ -90,7 +78,7 @@ func (d *deps) showFile(content string, data map[string]any, w http.ResponseWrit
data["content"] = content
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "file", data); err != nil {
if err := d.t.ExecuteTemplate(w, "file", data); err != nil {
log.Println(err)
return
}