Remove unused template init (fix startup crash)
The global template.Must parse at init time was left over from the basic auth version. web.RenderTemplate handles parsing per-request. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
24
handler.go
24
handler.go
@@ -4,9 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -29,33 +26,12 @@ var templateFiles embed.FS
|
|||||||
//go:embed static
|
//go:embed static
|
||||||
var staticFiles embed.FS
|
var staticFiles embed.FS
|
||||||
|
|
||||||
var templates = template.Must(template.ParseFS(templateFiles, "templates/*.html"))
|
|
||||||
|
|
||||||
type page struct {
|
type page struct {
|
||||||
Username string
|
Username string
|
||||||
Short string
|
Short string
|
||||||
URLs []*links.URL
|
URLs []*links.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *server) servePage(w http.ResponseWriter, p page, tpl string, funcs ...template.FuncMap) {
|
|
||||||
t := templates
|
|
||||||
if len(funcs) > 0 {
|
|
||||||
t = template.Must(t.Clone())
|
|
||||||
for _, fm := range funcs {
|
|
||||||
t.Funcs(fm)
|
|
||||||
}
|
|
||||||
// Re-parse to pick up the funcs.
|
|
||||||
t = template.Must(template.New("").Funcs(funcs[0]).ParseFS(templateFiles, "templates/*.html"))
|
|
||||||
}
|
|
||||||
err := t.ExecuteTemplate(w, tpl, p)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error executing template: %s", err)
|
|
||||||
http.Error(w, fmt.Sprintf("template execution failed: %s", err.Error()),
|
|
||||||
http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (srv *server) handleLoginPage(w http.ResponseWriter, r *http.Request) {
|
func (srv *server) handleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||||
web.RenderTemplate(w, templateFiles, "login.html", page{}, srv.csrf.TemplateFunc(w))
|
web.RenderTemplate(w, templateFiles, "login.html", page{}, srv.csrf.TemplateFunc(w))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user