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 "bytes"
5 "embed"
6 "fmt"
7 "html/template"
8 "io/fs"
9 "net/http"
10 "path"
11
12 "amadan.net/oficina/estilo.git/chrome"
13 "amadan.net/oficina/estilo.git/credits"
14 "amadan.net/oficina/estilo.git/menu"
15 "amadan.net/oficina/estilo.git/sky"
16 "amadan.net/rastrillo/rastrillo/ui"
17)
18
19//go:embed templates/*.html
20var templatesFS embed.FS
21
22// layoutFile is the app's own shell. It defines "layout" and executes
23// {{template "content" .}}, which is the contract rastrillo's shipped
24// shells use, so swapping in one of those later is a file copy rather
25// than a rewrite.
26const layoutFile = "templates/layout.html"
27
28// renderer holds one parsed tree per page.
29//
30// Parsed once, at construction, so a broken template is a boot failure
31// rather than a 500 on the one page nobody visits in testing. Minimal on
32// purpose: no asset pipeline, no theme, no vendored static files. The
33// scriptless sweep and the assets land with the task that adds them.
34type renderer struct {
35 pages map[string]*template.Template
36}
37
38func newRenderer() (*renderer, error) {
39 files, err := fs.Glob(templatesFS, "templates/*.html")
40 if err != nil {
41 return nil, err
42 }
43 // The layout and the shared partials (rastrillo's, then estilo's)
44 // are parsed once and cloned for each page: they are most of the
45 // bytes, and parsing them once per page made every App construction
46 // cost a re-parse of rastrillo's partials per page (34 of them).
47 // ui.Funcs supplies the helpers rastrillo's partials call — "T"
48 // among them — so the tree must carry them before those partials
49 // are parsed, not after. "door" and "hop" are placeholders here, so
50 // the layout parses; each page's clone sets its own below, and a
51 // clone's functions are its own (template.Clone copies the map).
52 base, err := template.New("layout.html").Funcs(ui.Funcs()).Funcs(homeFuncs).Funcs(template.FuncMap{
53 "door": func() bool { return false },
54 "hop": func() bool { return false },
55 }).ParseFS(templatesFS, layoutFile)
56 if err != nil {
57 return nil, fmt.Errorf("parse %s: %w", layoutFile, err)
58 }
59 if base, err = base.ParseFS(ui.Templates(), "*.html"); err != nil {
60 return nil, fmt.Errorf("parse rastrillo partials: %w", err)
61 }
62 if base, err = base.ParseFS(credits.Templates(), "templates/*.html"); err != nil {
63 return nil, fmt.Errorf("parse credits partials: %w", err)
64 }
65 if base, err = base.ParseFS(sky.Templates(), "templates/*.html"); err != nil {
66 return nil, fmt.Errorf("parse sky partials: %w", err)
67 }
68 // The shared chrome: the bar, the account menu, the door and the
69 // error page (chrome.go).
70 if base, err = base.ParseFS(menu.Templates(), "templates/*.html"); err != nil {
71 return nil, fmt.Errorf("parse menu partials: %w", err)
72 }
73 if base, err = base.ParseFS(chrome.Templates(), "templates/*.html"); err != nil {
74 return nil, fmt.Errorf("parse chrome partials: %w", err)
75 }
76 r := &renderer{pages: make(map[string]*template.Template, len(files))}
77 for _, f := range files {
78 name := path.Base(f)
79 if f == layoutFile {
80 continue
81 }
82 // The layout first, then rastrillo's partials, then the page —
83 // so a page may use a partial, and a page redefining a block
84 // wins over the layout's default.
85 isDoor, isHop := doorPages[name], hopPages[name]
86 t, err := base.Clone()
87 if err != nil {
88 return nil, fmt.Errorf("clone for %s: %w", f, err)
89 }
90 t.Funcs(template.FuncMap{
91 "door": func() bool { return isDoor },
92 "hop": func() bool { return isHop },
93 })
94 if t, err = t.ParseFS(templatesFS, f); err != nil {
95 return nil, fmt.Errorf("parse %s: %w", f, err)
96 }
97 r.pages[name] = t
98 }
99 if len(r.pages) == 0 {
100 return nil, fmt.Errorf("no page templates found")
101 }
102 return r, nil
103}
104
105// render writes one page in the request's chrome (chrome.go).
106func (a *App) render(w http.ResponseWriter, r *http.Request, status int, page string, data any) {
107 a.renderPage(w, r, status, page, data)
108}
109
110// renderPage writes one page.
111//
112// It renders to a buffer first. A template that fails halfway through
113// would otherwise have already written a 200 and half a page, and the
114// error handler could only append to it — so the status code would say
115// the opposite of what happened. A page that fails is answered with the
116// 500 page; if that one fails too, with plain text.
117func (a *App) renderPage(w http.ResponseWriter, r *http.Request, status int, name string, data any) {
118 if a.onCredential(r) {
119 w.Header().Set("Cache-Control", "no-store")
120 }
121 t, ok := a.renderer.pages[name]
122 if !ok {
123 a.logger.Error("no such page template", "page", name)
124 a.renderFailed(w, r, name)
125 return
126 }
127 var buf bytes.Buffer
128 if err := t.ExecuteTemplate(&buf, "layout", a.pageFor(r, status, data, doorPages[name], hopPages[name])); err != nil {
129 a.logger.Error("render", "page", name, "err", err)
130 a.renderFailed(w, r, name)
131 return
132 }
133 w.Header().Set("Content-Type", "text/html; charset=utf-8")
134 w.WriteHeader(status)
135 if _, err := buf.WriteTo(w); err != nil {
136 a.logger.Warn("write response", "page", name, "err", err)
137 }
138}
139
140// renderFailed answers a page that could not be drawn: the 500 page,
141// unless the page that failed was the error page itself.
142func (a *App) renderFailed(w http.ResponseWriter, r *http.Request, name string) {
143 if name == "error.html" || name == "id_error.html" || r == nil {
144 http.Error(w, "internal error", http.StatusInternalServerError)
145 return
146 }
147 a.serverError(w, r)
148}
149