diff --git a/main.go b/main.go index 01b1e5d..4cc95b1 100644 --- a/main.go +++ b/main.go @@ -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" @@ -28,8 +30,11 @@ func main() { "r"); err != nil { log.Fatalf("unveil: %s", err) } + + tpath := filepath.Join(c.Dirs.Templates, "*") + t := template.Must(template.ParseGlob(tpath)) - mux := routes.Handlers(c) + 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)) diff --git a/routes/handler.go b/routes/handler.go index 52d6e4e..a5a8620 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -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) diff --git a/routes/routes.go b/routes/routes.go index c3544c1..e1765dc 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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 } diff --git a/routes/template.go b/routes/template.go index 408841a..cf354ca 100644 --- a/routes/template.go +++ b/routes/template.go @@ -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 }