compile templates only once on app start instead of on every page request
This commit is contained in:
parent
907dcc9eeb
commit
068bda4915
4 changed files with 19 additions and 39 deletions
7
main.go
7
main.go
|
@ -3,8 +3,10 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"git.icyphox.sh/legit/config"
|
"git.icyphox.sh/legit/config"
|
||||||
"git.icyphox.sh/legit/routes"
|
"git.icyphox.sh/legit/routes"
|
||||||
|
@ -29,7 +31,10 @@ func main() {
|
||||||
log.Fatalf("unveil: %s", err)
|
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)
|
addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
|
||||||
log.Println("starting server on", addr)
|
log.Println("starting server on", addr)
|
||||||
log.Fatal(http.ListenAndServe(addr, mux))
|
log.Fatal(http.ListenAndServe(addr, mux))
|
||||||
|
|
|
@ -2,6 +2,7 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"html/template"
|
||||||
|
|
||||||
"git.icyphox.sh/legit/config"
|
"git.icyphox.sh/legit/config"
|
||||||
"github.com/alexedwards/flow"
|
"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()
|
mux := flow.New()
|
||||||
d := deps{c}
|
d := deps{c, t}
|
||||||
|
|
||||||
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
d.Write404(w)
|
d.Write404(w)
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
|
|
||||||
type deps struct {
|
type deps struct {
|
||||||
c *config.Config
|
c *config.Config
|
||||||
|
t *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
|
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)
|
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 := make(map[string]interface{})
|
||||||
data["meta"] = d.c.Meta
|
data["meta"] = d.c.Meta
|
||||||
data["info"] = infos
|
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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -138,9 +136,6 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("no readme found for %s", name)
|
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 {
|
if len(commits) >= 3 {
|
||||||
commits = 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["servername"] = d.c.Server.Name
|
||||||
data["gomod"] = isGoModule(gr)
|
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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -247,9 +242,6 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
|
||||||
t := template.Must(template.ParseGlob(tpath))
|
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
data["commits"] = commits
|
data["commits"] = commits
|
||||||
data["meta"] = d.c.Meta
|
data["meta"] = d.c.Meta
|
||||||
|
@ -258,7 +250,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
|
||||||
data["desc"] = getDescription(path)
|
data["desc"] = getDescription(path)
|
||||||
data["dotdot"] = filepath.Dir(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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -286,9 +278,6 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
|
||||||
t := template.Must(template.ParseGlob(tpath))
|
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
|
|
||||||
data["commit"] = diff.Commit
|
data["commit"] = diff.Commit
|
||||||
|
@ -299,7 +288,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
|
||||||
data["ref"] = ref
|
data["ref"] = ref
|
||||||
data["desc"] = getDescription(path)
|
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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -332,9 +321,6 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
|
||||||
t := template.Must(template.ParseGlob(tpath))
|
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
|
|
||||||
data["meta"] = d.c.Meta
|
data["meta"] = d.c.Meta
|
||||||
|
@ -343,7 +329,7 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
|
||||||
data["tags"] = tags
|
data["tags"] = tags
|
||||||
data["desc"] = getDescription(path)
|
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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,42 +2,33 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.icyphox.sh/legit/git"
|
"git.icyphox.sh/legit/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *deps) Write404(w http.ResponseWriter) {
|
func (d *deps) Write404(w http.ResponseWriter) {
|
||||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
|
||||||
t := template.Must(template.ParseGlob(tpath))
|
|
||||||
w.WriteHeader(404)
|
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)
|
log.Printf("404 template: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *deps) Write500(w http.ResponseWriter) {
|
func (d *deps) Write500(w http.ResponseWriter) {
|
||||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
|
||||||
t := template.Must(template.ParseGlob(tpath))
|
|
||||||
w.WriteHeader(500)
|
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)
|
log.Printf("500 template: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
|
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["files"] = files
|
||||||
data["meta"] = d.c.Meta
|
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)
|
log.Println(err)
|
||||||
return
|
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) {
|
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))
|
lc, err := countLines(strings.NewReader(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Non-fatal, we'll just skip showing line numbers in the template.
|
// 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["content"] = content
|
||||||
data["meta"] = d.c.Meta
|
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)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue