oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package web
2
3import (
4 "bytes"
5 "embed"
6 "html/template"
7 "io/fs"
8 "net/http"
9 "testing/fstest"
10 "time"
11
12 "amadan.net/oficina/estilo.git/base"
13 "amadan.net/oficina/estilo.git/brand"
14 "amadan.net/oficina/estilo.git/busy"
15 "amadan.net/oficina/estilo.git/chrome"
16 estilomenu "amadan.net/oficina/estilo.git/menu"
17 "amadan.net/oficina/estilo.git/theme"
18 "amadan.net/rastrillo/rastrillo"
19 "amadan.net/rastrillo/rastrillo/sessions"
20 "amadan.net/rastrillo/rastrillo/ui"
21)
22
23//go:embed templates static
24var appFS embed.FS
25
26const assetPrefix = "/static/"
27
28// The stylesheets in the suite's order: rastrillo's tokens, estilo's
29// theme (Calma), then estilo's base, menu and chrome, then Alta's own.
30// The brand mark is estilo's Oficina mark: Alta is not an app in the
31// switcher and has no mark of its own.
32var assets = rastrillo.NewAssets(func() fs.FS {
33 files := fstest.MapFS{
34 "static/tokens.css": {Data: ui.TokensCSS()},
35 "static/theme.css": {Data: theme.CSS()},
36 "static/estilo/base.css": {Data: base.CSS()},
37 "static/estilo/menu.css": {Data: estilomenu.CSS()},
38 "static/estilo/chrome.css": {Data: chrome.CSS()},
39 "static/oficina.svg": {Data: brand.SVG("oficina")},
40 }
41 b, err := appFS.ReadFile("static/alta.css")
42 if err != nil {
43 panic(err)
44 }
45 files["static/alta.css"] = &fstest.MapFile{Data: b}
46 return files
47}())
48
49func assetHandler() http.Handler {
50 busyH, files := busy.Handler(), assets.Handler()
51 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52 if len(r.URL.Path) > len(busy.Prefix) && r.URL.Path[:len(busy.Prefix)] == busy.Prefix {
53 busyH.ServeHTTP(w, r)
54 return
55 }
56 files.ServeHTTP(w, r)
57 })
58}
59
60var pageNames = []string{"index", "team_new", "team", "run", "retire", "initialize", "notfound", "signedout"}
61
62var pages = map[string]*template.Template{}
63
64func init() {
65 for _, name := range pageNames {
66 t := template.New("layout").Funcs(ui.Funcs()).Funcs(template.FuncMap{
67 "asset": assets.Path,
68 "busyJS": func() string { return busy.Prefix + "busy.js" },
69 "busyCSS": func() string { return busy.Prefix + "busy.css" },
70 "when": func(t time.Time) string { return t.UTC().Format("2 Jan 15:04 UTC") },
71 "unix": func(n int64) string { return time.Unix(n, 0).UTC().Format("2 Jan 15:04 UTC") },
72 "describe": describe,
73 })
74 t = template.Must(t.ParseFS(ui.Templates(), "*.html"))
75 t = template.Must(t.ParseFS(chrome.Templates(), "templates/*.html"))
76 t = template.Must(t.ParseFS(estilomenu.Templates(), "templates/*.html"))
77 pages[name] = template.Must(t.ParseFS(appFS, "templates/layout.html", "templates/"+name+".html"))
78 }
79}
80
81// page is every template's data.
82type page struct {
83 Title, Context string
84 Error, Notice string
85 Bare bool
86 Data map[string]any
87 Frame frame
88}
89
90type frame struct {
91 Chrome chrome.Bar
92 Theme string
93}
94
95func (a *App) frame(r *http.Request, p page) frame {
96 bar := chrome.Bar{
97 App: chrome.App{Name: "Alta", Slug: "alta", Home: "/", Icon: assets.Path("static/oficina.svg")},
98 Context: p.Context,
99 Labels: chrome.BarLabels{Skip: "Skip to content", Menu: "Menu"},
100 }
101 if s, ok := sessions.Current(r); ok && s.Subject == a.cfg.OperatorSubject && !p.Bare {
102 bar.Nav = []chrome.Link{{Href: "/", Label: "Teams", Current: r.URL.Path == "/"}, {Href: "/teams/new", Label: "New team", Current: r.URL.Path == "/teams/new"}}
103 bar.NavLabel = "Alta"
104 bar.Account = &chrome.Account{
105 TriggerLabel: "Account",
106 Initials: "P",
107 Address: "Paul",
108 Labels: chrome.AccountLabels{SignedInAs: "Signed in as", Appearance: "Appearance", Help: "Help"},
109 SignOut: chrome.SignOut{Action: "/signout", Label: "Sign out"},
110 }
111 } else {
112 bar.App.Home = ""
113 }
114 return frame{Chrome: bar, Theme: chrome.ThemeAttr(chrome.Appearance(r))}
115}
116
117func (a *App) render(w http.ResponseWriter, r *http.Request, status int, name string, p page) {
118 p.Frame = a.frame(r, p)
119 if p.Data == nil {
120 p.Data = map[string]any{}
121 }
122 var buf bytes.Buffer
123 if err := pages[name].ExecuteTemplate(&buf, "layout", p); err != nil {
124 a.cfg.Logger.Error("alta render", "page", name, "err", err)
125 a.plain(w, http.StatusInternalServerError, "Something went wrong.")
126 return
127 }
128 w.Header().Set("Content-Type", "text/html; charset=utf-8")
129 w.Header().Set("Cache-Control", "no-store")
130 w.WriteHeader(status)
131 _, _ = buf.WriteTo(w)
132}
133
134func (a *App) notFound(w http.ResponseWriter, r *http.Request) {
135 a.render(w, r, http.StatusNotFound, "notfound", page{Title: "Not found", Bare: true})
136}
137
138func (a *App) plain(w http.ResponseWriter, status int, msg string) {
139 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
140 w.Header().Set("Cache-Control", "no-store")
141 w.WriteHeader(status)
142 _, _ = w.Write([]byte(msg + "\n"))
143}
144