parent:
2b9e13786d6dff5c324880d0a26bd000123e9156
nmyk <nick@nmyk.io>
2026-03-02T18:07:08-05:00
parse templates once instead of per request clean up breadcrumbs
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 49ec5f8365ac11617947b660587767000d4d2848..c6ed0bbeb82c99410b8332515a2b9b2109971c75 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -38,12 +38,43 @@ }
if c.Hostname == "" {
c.Hostname = "repos"
}
- return Server{Config: c, CSS: css}
+ entries, err := Templates.ReadDir("templates")
+ if err != nil {
+ panic(err)
+ }
+ templates := make(map[string]*template.Template)
+ for _, t := range entries {
+ if t.Name() == "base.tmpl" {
+ continue
+ }
+ tmpl := template.Must(template.New("").
+ Funcs(template.FuncMap{
+ "css": func() template.CSS {
+ return template.CSS(css)
+ },
+ "humanize": humanize.Time,
+ "rfc3339": func(t time.Time) string {
+ return t.Format(time.RFC3339)
+ },
+ "sub": func(a int, b int) int {
+ return a - b
+ },
+ }).
+ ParseFS(
+ Templates,
+ path.Join("templates", "base.tmpl"),
+ path.Join("templates", t.Name()),
+ ),
+ )
+ key := strings.TrimSuffix(path.Base(t.Name()), ".tmpl")
+ templates[key] = tmpl
+ }
+ return Server{Config: c, templates: templates}
}
type Server struct {
*Config
- CSS []byte
+ templates map[string]*template.Template
}
func (s Server) Routes() *http.ServeMux {
@@ -73,26 +104,8 @@ http.Error(w, err.Error(), http.StatusInternalServerError)
}
func (s Server) Serve(w http.ResponseWriter, tmplName string, data any) {
- tmpl := template.Must(template.New("").
- Funcs(template.FuncMap{
- "css": func() template.CSS {
- return template.CSS(s.CSS)
- },
- "humanize": func(t time.Time) string {
- return humanize.Time(t)
- },
- "rfc3339": func(t time.Time) string {
- return t.Format(time.RFC3339)
- },
- }).
- ParseFS(
- Templates,
- "templates/base.tmpl",
- "templates/"+tmplName+".tmpl",
- ),
- )
var buf bytes.Buffer
- if err := tmpl.ExecuteTemplate(&buf, tmplName, data); err != nil {
+ if err := s.templates[tmplName].ExecuteTemplate(&buf, tmplName, data); err != nil {
s.error(w, err)
return
}
diff --git a/internal/handlers/templates/blob.tmpl b/internal/handlers/templates/blob.tmpl
index 4df727dda838c9627ec21bb3f878c47065f51de5..b72158039d362822b4322feb3e707c3acd1beddb 100644
--- a/internal/handlers/templates/blob.tmpl
+++ b/internal/handlers/templates/blob.tmpl
@@ -3,7 +3,7 @@ {{define "content"}}
<h1>{{if eq .Path ""}}{{.Repo}}{{else}}<a href="/{{.Repo}}/tree/{{.RefLabel}}">{{.Repo}}</a> /{{end}}
{{range $i, $c := .Crumbs}}
{{if $i}} / {{end}}
- {{if $c.IsLast}}
+ {{if eq $i (sub (len $.Crumbs) 1)}}
{{$c.Name}}
{{else}}
<a href="/{{$c.URL}}">{{$c.Name}}</a>
diff --git a/internal/handlers/templates/tree.tmpl b/internal/handlers/templates/tree.tmpl
index 9c80c605eaa27d453d85e66db589f96674d1bce6..d76cd6e7425643861de33ab53fde523065084a1f 100644
--- a/internal/handlers/templates/tree.tmpl
+++ b/internal/handlers/templates/tree.tmpl
@@ -6,7 +6,7 @@ <a href="/">{{.Hostname}}</a> / <a href="/{{.Repo}}/tree/{{.HeadRef}}">{{.Repo}}</a>
{{end}}
{{range $i, $c := .Crumbs}}
{{if $i}} / {{end}}
- {{if $c.IsLast}}
+ {{if eq $i (sub (len $.Crumbs) 1)}}
{{$c.Name}}
{{else}}
<a href="/{{$c.URL}}">{{$c.Name}}</a>