parent:
e8fb0cb9983fb3d6d17b77afaa74beabc7656ca5
nmyk <nick@nmyk.io>
2026-03-01T17:44:45-05:00
Revert "parse templates once instead of per request" This reverts commit e8fb0cb9983fb3d6d17b77afaa74beabc7656ca5.
diff --git a/internal/handlers/formatting.go b/internal/handlers/formatting.go
index 5aa5005d282a9c516264fccd85dfd6d5cad7d255..e73959e58f88047130a8ed5efae9b1508745f823 100644
--- a/internal/handlers/formatting.go
+++ b/internal/handlers/formatting.go
@@ -10,12 +10,14 @@ return name + " <" + email + ">"
}
type crumb struct {
- Name string
- URL string
+ Name string
+ URL string
+ IsLast bool
}
func breadcrumbs(repoName string, refLabel string, p string) []crumb {
var crumbs []crumb
+ last := true
cur := p
for {
base := path.Base(cur)
@@ -23,9 +25,11 @@ if base == "." || base == "/" {
break
}
crumbs = append(crumbs, crumb{
- Name: base,
- URL: path.Join(repoName, "tree", refLabel, cur),
+ Name: base,
+ URL: path.Join(repoName, "tree", refLabel, cur),
+ IsLast: last,
})
+ last = false
next := path.Dir(cur)
if next == cur {
break
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index c53dac248e3f8d1b983650c63dd78b0d153d2df3..49ec5f8365ac11617947b660587767000d4d2848 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -38,32 +38,12 @@ }
if c.Hostname == "" {
c.Hostname = "repos"
}
- 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}
+ return Server{Config: c, CSS: css}
}
type Server struct {
*Config
- templates *template.Template
+ CSS []byte
}
func (s Server) Routes() *http.ServeMux {
@@ -93,8 +73,26 @@ 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 := s.templates.ExecuteTemplate(&buf, tmplName, data); err != nil {
+ if err := tmpl.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 b72158039d362822b4322feb3e707c3acd1beddb..4df727dda838c9627ec21bb3f878c47065f51de5 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 eq $i (sub (len $.Crumbs) 1)}}
+ {{if $c.IsLast}}
{{$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 d76cd6e7425643861de33ab53fde523065084a1f..9c80c605eaa27d453d85e66db589f96674d1bce6 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 eq $i (sub (len $.Crumbs) 1)}}
+ {{if $c.IsLast}}
{{$c.Name}}
{{else}}
<a href="/{{$c.URL}}">{{$c.Name}}</a>