parent:
797c7a407b8fa943b19b8241e470451b21fde72d
nmyk <nick@nmyk.io>
2026-03-01T17:43:15-05:00
parse templates once instead of per request clean up breadcrumbs
diff --git a/internal/handlers/formatting.go b/internal/handlers/formatting.go
index e73959e58f88047130a8ed5efae9b1508745f823..5aa5005d282a9c516264fccd85dfd6d5cad7d255 100644
--- a/internal/handlers/formatting.go
+++ b/internal/handlers/formatting.go
@@ -10,14 +10,12 @@ return name + " <" + email + ">"
}
type crumb struct {
- Name string
- URL string
- IsLast bool
+ Name string
+ URL string
}
func breadcrumbs(repoName string, refLabel string, p string) []crumb {
var crumbs []crumb
- last := true
cur := p
for {
base := path.Base(cur)
@@ -25,11 +23,9 @@ if base == "." || base == "/" {
break
}
crumbs = append(crumbs, crumb{
- Name: base,
- URL: path.Join(repoName, "tree", refLabel, cur),
- IsLast: last,
+ Name: base,
+ URL: path.Join(repoName, "tree", refLabel, cur),
})
- last = false
next := path.Dir(cur)
if next == cur {
break
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 49ec5f8365ac11617947b660587767000d4d2848..c53dac248e3f8d1b983650c63dd78b0d153d2df3 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -38,12 +38,32 @@ }
if c.Hostname == "" {
c.Hostname = "repos"
}
- return Server{Config: c, CSS: css}
+ tmpl := template.Must(template.New("").
+ Funcs(template.FuncMap{
+ "css": func() template.CSS {
+ return template.CSS(css)
+ },
+ "humanize": func(t time.Time) string {
+ return humanize.Time(t)
+ },
+ "rfc3339": func(t time.Time) string {
+ return t.Format(time.RFC3339)
+ },
+ "sub": func(a int, b int) int {
+ return a - b
+ },
+ }).
+ ParseFS(
+ Templates,
+ "templates/*.tmpl",
+ ),
+ )
+ return Server{Config: c, templates: tmpl}
}
type Server struct {
*Config
- CSS []byte
+ templates *template.Template
}
func (s Server) Routes() *http.ServeMux {
@@ -73,26 +93,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.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>