oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "html/template"
5
6 "amadan.net/oficina/estilo.git/sky"
7 "bytes"
8 "embed"
9 "net/http"
10 "regexp"
11 "strconv"
12 "sync"
13
14 "amadan.net/oficina/estilo.git/theme"
15 "amadan.net/oficina/home/internal/home/wisp"
16 "github.com/go-chi/chi/v5"
17)
18
19//go:embed static/site
20var siteFS embed.FS
21
22// The front page is a real page now, not the skeleton's bare fact. It is
23// authored as a file rather than a Go string because it is long enough
24// that a const would hide its diffs, and it has no dynamic content and no
25// user input to escape — so it is served as bytes, with no template step
26// standing between the file and the response.
27//
28// It is drawn in Calma, the suite's look: estilo's theme in the system
29// UI face (no webfont; estilo AGENTS.md row 9), the sky behind the hero,
30// and each app's own colour from estilo's ring (appsCSS). Everything it
31// links is on this origin; SecurityPolicy's `default-src 'self'` already
32// admits the pictures, and the stylesheets are covered by
33// `style-src 'self'`.
34//
35// Everything the page points at has to exist. The app links go to the six
36// live app hosts; there is no sign-up form, because there is no route that
37// would accept one (provisioning is still `oficina provision`, an operator
38// CLI). A form posting into a 404 is worse than saying so plainly, and the
39// page says so plainly.
40var indexPage = siteAsset("index.html")
41
42func siteAsset(name string) []byte {
43 b, err := siteFS.ReadFile("static/site/" + name)
44 if err != nil {
45 panic(err) // Embedded above; unreachable unless the build is broken.
46 }
47 return b
48}
49
50// appsCSS gives each app on the page its own accent: estilo's per-app
51// accent blocks, scoped from :root to [data-app="<slug>"], so a card for
52// Sheets tints green from the same ring the Sheets app itself uses, with
53// no colour copied here to drift.
54var appsCSS = func() []byte {
55 var b bytes.Buffer
56 b.WriteString("/* Each app's accent, from estilo's colour ring (theme.AccentCSS). */\n")
57 for _, a := range theme.Apps() {
58 block, _ := theme.AccentCSS(a.Slug)
59 b.Write(bytes.Replace(block, []byte(":root {"), []byte(`[data-app="`+a.Slug+`"] {`), 1))
60 }
61 return b.Bytes()
62}()
63
64// Public on both Home origins, and a closed list for the same reason
65// mountBrand keeps one: a request path must never be treated as a file
66// name.
67func mountSite(r chi.Router) {
68 mountCSS(r, "/static/site/home.css", siteAsset("home.css"))
69 mountCSS(r, "/static/site/apps.css", appsCSS)
70}
71
72// The wisps (package wisp): <!-- wisp:sprite --> is where the drawing
73// goes, once, at the end of the body — the front page is no-store, so a
74// visitor reads it every time, and there it never stands between them and
75// the first words; <!-- wisp:N --> is where a heading's
76// wisp goes, inside the heading, which also carries data-wisp="N" for
77// home.css to time it by.
78var (
79 wispSpriteMarker = []byte("<!-- wisp:sprite -->")
80 wispMarker = regexp.MustCompile(`<!-- wisp:(\d+) -->`)
81)
82
83func withWisps(page []byte) []byte {
84 page = bytes.Replace(page, wispSpriteMarker, wisp.Sprite(), 1)
85 return wispMarker.ReplaceAllFunc(page, func(m []byte) []byte {
86 n, _ := strconv.Atoi(string(wispMarker.FindSubmatch(m)[1]))
87 return []byte(wisp.Markup(n))
88 })
89}
90
91// skyMarker is where the front page's sky goes: rendered from estilo's
92// own template, so the page carries exactly the layers (and the inline
93// placeholder) the pinned estilo draws, never a hand-copied set of URLs.
94const skyMarker = "<!-- estilo-sky: the suite's sky, rendered from estilo's template on first serve (page.go) -->"
95
96func withSky(page []byte) []byte {
97 t := template.Must(template.ParseFS(sky.Templates(), "templates/*.html"))
98 var b bytes.Buffer
99 if err := t.ExecuteTemplate(&b, "estilo-sky", sky.For(sky.SuiteSeed)); err != nil {
100 panic(err) // estilo's own template over its own data; a broken pin
101 }
102 return bytes.Replace(page, []byte(skyMarker), b.Bytes(), 1)
103}
104
105// servedIndex is the front page with every stylesheet and picture linked
106// by its content version. Built on first serve, once the routers have
107// mounted (and so versioned) everything the page links.
108var (
109 servedIndexOnce sync.Once
110 servedIndex []byte
111)
112
113// handleIndex serves the front page.
114func (a *App) handleIndex(w http.ResponseWriter, _ *http.Request) {
115 servedIndexOnce.Do(func() { servedIndex = versionStaticRefs(withWisps(withSky(indexPage))) })
116 w.Header().Set("Content-Type", "text/html; charset=utf-8")
117 w.Write(servedIndex)
118}
119