| 1 | // Package look is Base's face: the layout, the stylesheet and the fonts. |
| 2 | // |
| 3 | // Each consumer (Base's own web package, and later the apps Base ships) |
| 4 | // supplies its page templates as an fs.FS and renders them through a |
| 5 | // Renderer, so every page shares the same atmosphere. |
| 6 | package look |
| 7 | |
| 8 | import ( |
| 9 | "bytes" |
| 10 | "embed" |
| 11 | "fmt" |
| 12 | "html/template" |
| 13 | "io/fs" |
| 14 | "net/http" |
| 15 | "strings" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo" |
| 19 | ) |
| 20 | |
| 21 | // all: keeps the .keep file that holds the git-ignored timeless directory open. |
| 22 | // |
| 23 | //go:embed all:static templates |
| 24 | var embedded embed.FS |
| 25 | |
| 26 | // Assets fingerprints the embedded static tree: {{asset "static/app.css"}} |
| 27 | // in a template yields a content-hashed URL. Mount Assets.Handler() at |
| 28 | // GET /static/. |
| 29 | var Assets = rastrillo.NewAssets(embedded) |
| 30 | |
| 31 | const fontPrefix = "/static/fonts/" |
| 32 | |
| 33 | // FontHandler serves the embedded fonts. Mount it at GET /static/fonts/, |
| 34 | // ahead of the generic static route. Font paths carry their version, so |
| 35 | // files are immutable for a year. A font that is not there (Timeless Sans |
| 36 | // until hack/fonts.sh has run) answers 404 and the page falls back to DM Sans. |
| 37 | func FontHandler() http.Handler { |
| 38 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 39 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 40 | w.Header().Set("Allow", "GET, HEAD") |
| 41 | http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) |
| 42 | return |
| 43 | } |
| 44 | name := strings.TrimPrefix(r.URL.Path, "/") |
| 45 | if !strings.HasPrefix(r.URL.Path, fontPrefix) || !fs.ValidPath(name) || !strings.HasSuffix(name, ".woff2") { |
| 46 | http.NotFound(w, r) |
| 47 | return |
| 48 | } |
| 49 | body, err := embedded.ReadFile(name) |
| 50 | if err != nil { |
| 51 | http.NotFound(w, r) |
| 52 | return |
| 53 | } |
| 54 | w.Header().Set("Content-Type", "font/woff2") |
| 55 | w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") |
| 56 | http.ServeContent(w, r, name, time.Time{}, bytes.NewReader(body)) |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | // Renderer renders page templates inside the shared layout. |
| 61 | type Renderer struct { |
| 62 | // pages holds each page once per theme ("" follows the device), so |
| 63 | // the layout draws a member's Appearance without a script. |
| 64 | pages map[string]map[string]*template.Template |
| 65 | } |
| 66 | |
| 67 | // themes are the Appearances a page is drawn in: "" follows the device's |
| 68 | // light or dark setting; "light" and "dark" hold that one. |
| 69 | var themes = []string{"", "light", "dark"} |
| 70 | |
| 71 | // NewRenderer parses every top-level *.html in pageFS as a page named after |
| 72 | // the file (without .html). A page defines the layout's "title" and "main" |
| 73 | // blocks. Templates in partials/*.html are not pages: every page can use |
| 74 | // what they define. funcs are extra template functions; "asset" is always |
| 75 | // provided, and "theme" (the layout's: the Appearance it draws). |
| 76 | func NewRenderer(pageFS fs.FS, funcs template.FuncMap) (*Renderer, error) { |
| 77 | files, err := fs.Glob(pageFS, "*.html") |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | partials, err := fs.Glob(pageFS, "partials/*.html") |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | all := template.FuncMap{"asset": Assets.Path} |
| 86 | for k, v := range funcs { |
| 87 | all[k] = v |
| 88 | } |
| 89 | r := &Renderer{pages: make(map[string]map[string]*template.Template, len(themes))} |
| 90 | for _, theme := range themes { |
| 91 | r.pages[theme] = make(map[string]*template.Template, len(files)) |
| 92 | all["theme"] = func() string { return theme } |
| 93 | for _, f := range files { |
| 94 | t, err := template.New("layout").Funcs(all).ParseFS(embedded, "templates/layout.html") |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | if len(partials) > 0 { |
| 99 | if t, err = t.ParseFS(pageFS, partials...); err != nil { |
| 100 | return nil, fmt.Errorf("look: partials: %w", err) |
| 101 | } |
| 102 | } |
| 103 | if t, err = t.ParseFS(pageFS, f); err != nil { |
| 104 | return nil, fmt.Errorf("look: page %s: %w", f, err) |
| 105 | } |
| 106 | r.pages[theme][strings.TrimSuffix(f, ".html")] = t |
| 107 | } |
| 108 | } |
| 109 | return r, nil |
| 110 | } |
| 111 | |
| 112 | // Page renders the named page in layout.html with status 200. It executes |
| 113 | // into a buffer first, so a template error writes nothing to w. |
| 114 | func (r *Renderer) Page(w http.ResponseWriter, name string, data any) error { |
| 115 | return r.PageStatus(w, http.StatusOK, name, data) |
| 116 | } |
| 117 | |
| 118 | // PageStatus is Page with another status, such as 409 for a refused save |
| 119 | // shown again with what the person typed. The page follows the device's |
| 120 | // light or dark setting. |
| 121 | func (r *Renderer) PageStatus(w http.ResponseWriter, status int, name string, data any) error { |
| 122 | return r.PageTheme(w, status, name, data, "") |
| 123 | } |
| 124 | |
| 125 | // PageTheme is PageStatus in a member's Appearance: "light" or "dark" holds |
| 126 | // that theme; anything else follows the device. |
| 127 | func (r *Renderer) PageTheme(w http.ResponseWriter, status int, name string, data any, theme string) error { |
| 128 | byName, ok := r.pages[theme] |
| 129 | if !ok { |
| 130 | byName = r.pages[""] |
| 131 | } |
| 132 | t, ok := byName[name] |
| 133 | if !ok { |
| 134 | return fmt.Errorf("look: no page %q", name) |
| 135 | } |
| 136 | var buf bytes.Buffer |
| 137 | if err := t.ExecuteTemplate(&buf, "layout", data); err != nil { |
| 138 | return fmt.Errorf("look: render %s: %w", name, err) |
| 139 | } |
| 140 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 141 | w.WriteHeader(status) |
| 142 | _, err := buf.WriteTo(w) |
| 143 | return err |
| 144 | } |
| 145 | |