package web
import (
"bytes"
"embed"
"html/template"
"log"
"net/http"
"strings"
)
// tmplFS holds the page templates. Each page is parsed together with the shared
// layout into its own template set so that per-page "content" defines do not
// collide across pages.
//
//go:embed templates/*.html
var tmplFS embed.FS
// staticFS holds the built assets: the hashed stylesheet produced by `make css`
// and the logo. It is compiled into the binary, which is why `make css` alone
// does not restyle a running daemon — see the package doc.
//
//go:embed static
var staticFS embed.FS
// funcMap holds the template helpers shared by every page.
var funcMap = template.FuncMap{
// shortsha abbreviates an object id to 8 hex chars.
"shortsha": func(s string) string {
if len(s) > 8 {
return s[:8]
}
return s
},
// indent renders a tree depth as non-breaking space, so the space view's
// hierarchy reads as a hierarchy without a nested-list template recursion.
// A negative depth (a level-1 heading, once decremented) indents nothing.
"indent": func(depth int) template.HTML {
if depth <= 0 {
return ""
}
return template.HTML(strings.Repeat(" ", depth))
},
// dec turns a 1-based heading level into a 0-based indent depth.
"dec": func(n int) int { return n - 1 },
}
// pageNames are the content templates; each is parsed with layout.html.
var pageNames = []string{"index", "space", "document", "search", "error", "proposal"}
// pages maps a page name to its parsed template set (layout + that page).
var pages = func() map[string]*template.Template {
m := make(map[string]*template.Template, len(pageNames))
for _, name := range pageNames {
t := template.New("layout.html").Funcs(funcMap)
t = template.Must(t.ParseFS(tmplFS, "templates/layout.html", "templates/"+name+".html"))
m[name] = t
}
return m
}()
// render executes a page into a buffer first, so a template error yields a
// clean 500 rather than a half-written response. On success it writes the
// status and the buffered HTML.
func (s *Server) render(w http.ResponseWriter, status int, page string, vd viewData) {
t, ok := pages[page]
if !ok {
log.Printf("web: unknown template page %q", page)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "layout.html", vd); err != nil {
log.Printf("web: executing template %q: %v", page, err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
_, _ = buf.WriteTo(w)
}
// errorData is the payload of the error page.
type errorData struct {
Status int
StatusText string
Message string
}
// renderError renders the chrome-wrapped error page. It never recurses into
// render on failure (render falls back to http.Error itself).
func (s *Server) renderError(w http.ResponseWriter, r *http.Request, status int, message string) {
vd := s.chrome(r)
vd.Title = http.StatusText(status)
vd.Data = errorData{
Status: status,
StatusText: http.StatusText(status),
Message: message,
}
s.render(w, status, "error", vd)
}