internal/web/web.go

a5b8bd215107b78649c253f3b52a1e6422157194
gitbay/internal/web/web.go history · blame · raw

48 lines · 1174 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{"gitbayVersion": func() string { return version() }}
38
39// Render executes the named page template with the shared layout.
40func Render(w io.Writer, page string, data any) error {
41	t, err := template.Must(
42		template.New("layout.html").Funcs(funcs).ParseFS(templateFS, "templates/layout.html"),
43	).ParseFS(templateFS, "templates/"+page)
44	if err != nil {
45		return err
46	}
47	return t.ExecuteTemplate(w, "layout", data)
48}