Templates should use go:embed instead of filesystem parsing at request time #7

Closed
opened 2026-03-01 19:07:26 +01:00 by clawbot · 0 comments
Collaborator

Per GO_HTTP_SERVER_CONVENTIONS §12, templates should be embedded using //go:embed and parsed once via template.Must(template.ParseFS(...)). Currently, internal/handlers/handlers.go calls template.ParseFiles() on every request in renderTemplate(), which:

  1. Reads from the filesystem at request time (not embedded)
  2. Re-parses templates on every request (performance cost)
  3. Does not work correctly in Docker containers if the templates directory is not in the expected location relative to the working directory

The static/ package already correctly uses //go:embed. Templates should follow the same pattern:

// templates/templates.go
package templates

import (
    "embed"
    "text/template"
)

//go:embed *.html
var TemplatesRaw embed.FS
var TemplatesParsed *template.Template

func GetParsed() *template.Template {
    if TemplatesParsed == nil {
        TemplatesParsed = template.Must(template.ParseFS(TemplatesRaw, "*"))
    }
    return TemplatesParsed
}
Per GO_HTTP_SERVER_CONVENTIONS §12, templates should be embedded using `//go:embed` and parsed once via `template.Must(template.ParseFS(...))`. Currently, `internal/handlers/handlers.go` calls `template.ParseFiles()` on every request in `renderTemplate()`, which: 1. Reads from the filesystem at request time (not embedded) 2. Re-parses templates on every request (performance cost) 3. Does not work correctly in Docker containers if the templates directory is not in the expected location relative to the working directory The `static/` package already correctly uses `//go:embed`. Templates should follow the same pattern: ```go // templates/templates.go package templates import ( "embed" "text/template" ) //go:embed *.html var TemplatesRaw embed.FS var TemplatesParsed *template.Template func GetParsed() *template.Template { if TemplatesParsed == nil { TemplatesParsed = template.Must(template.ParseFS(TemplatesRaw, "*")) } return TemplatesParsed } ```
clawbot added the
bot
label 2026-03-01 19:07:26 +01:00
sneak closed this issue 2026-03-04 01:19:43 +01:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sneak/webhooker#7
No description provided.