internal/web/web.go
57 lines · 1315 bytes
1// Package web holds the server-rendered templates and static assets for the
2// read-only UI. No JavaScript, no build step.
3package web
4
5import (
6 "embed"
7 "html/template"
8 "io"
9 "runtime/debug"
10 "sync"
11)
12
13//go:embed templates/*.html
14var templateFS embed.FS
15
16//go:embed static/style.css
17var StyleCSS []byte
18
19//go:embed static/favicon.svg
20var FaviconSVG []byte
21
22// version returns the short VCS revision baked into the binary, or "" when
23// built outside a checkout. Used by the layout footer.
24var version = sync.OnceValue(func() string {
25 info, ok := debug.ReadBuildInfo()
26 if !ok {
27 return ""
28 }
29 for _, s := range info.Settings {
30 if s.Key == "vcs.revision" && len(s.Value) >= 10 {
31 return s.Value[:10]
32 }
33 }
34 return ""
35})
36
37var funcs = template.FuncMap{
38 "gitbayVersion": func() string { return version() },
39 // short abbreviates a commit SHA for display.
40 "short": func(s string) string {
41 if len(s) > 10 {
42 return s[:10]
43 }
44 return s
45 },
46}
47
48// Render executes the named page template with the shared layout.
49func Render(w io.Writer, page string, data any) error {
50 t, err := template.Must(
51 template.New("layout.html").Funcs(funcs).ParseFS(templateFS, "templates/layout.html"),
52 ).ParseFS(templateFS, "templates/"+page)
53 if err != nil {
54 return err
55 }
56 return t.ExecuteTemplate(w, "layout", data)
57}