| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "embed" |
| 7 | "encoding/base64" |
| 8 | "fmt" |
| 9 | "html/template" |
| 10 | "net/http" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | // The pages' templates, and the one stylesheet and one script they carry |
| 17 | // inline. Nothing else is served: no font, image or script is fetched. |
| 18 | var ( |
| 19 | //go:embed templates/*.tmpl |
| 20 | templateFiles embed.FS |
| 21 | //go:embed assets/style.css |
| 22 | styleCSS string |
| 23 | //go:embed assets/plan.js |
| 24 | planJS string |
| 25 | ) |
| 26 | |
| 27 | // csp is every response's Content-Security-Policy. The inline stylesheet |
| 28 | // and script are admitted by their SHA-256 hashes alone, and every other |
| 29 | // fetch is held to the server's own origin. |
| 30 | var csp = "default-src 'self'; script-src " + inlineHash(planJS) + "; style-src " + inlineHash(styleCSS) + |
| 31 | "; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" |
| 32 | |
| 33 | func inlineHash(s string) string { |
| 34 | sum := sha256.Sum256([]byte(s)) |
| 35 | return "'sha256-" + base64.StdEncoding.EncodeToString(sum[:]) + "'" |
| 36 | } |
| 37 | |
| 38 | var pages = parsePages("plan", "knot", "tree", "blob", "log", "error") |
| 39 | |
| 40 | // parsePages parses each page's template over the shared layout. |
| 41 | func parsePages(names ...string) map[string]*template.Template { |
| 42 | funcs := template.FuncMap{"glyph": glyphClass, "label": stateLabel, "inc": func(i int) int { return i + 1 }} |
| 43 | base := template.Must(template.New("layout").Funcs(funcs).ParseFS(templateFiles, "templates/layout.tmpl")) |
| 44 | m := map[string]*template.Template{} |
| 45 | for _, name := range names { |
| 46 | m[name] = template.Must(template.Must(base.Clone()).ParseFS(templateFiles, "templates/"+name+".tmpl")) |
| 47 | } |
| 48 | return m |
| 49 | } |
| 50 | |
| 51 | // layout is what every page shares: its title, the inline stylesheet, the |
| 52 | // script on the plan page alone, the links to the plan and to each |
| 53 | // project's source, and the export's age. |
| 54 | type layout struct { |
| 55 | Title string |
| 56 | Style template.CSS |
| 57 | Script template.JS |
| 58 | Nav []link |
| 59 | Fresh *freshness |
| 60 | Content any |
| 61 | } |
| 62 | |
| 63 | // errorPage is a page that answers a request with no content of its own. |
| 64 | type errorPage struct { |
| 65 | Heading string |
| 66 | Text string |
| 67 | } |
| 68 | |
| 69 | // link is one link: its text and where it goes. |
| 70 | type link struct { |
| 71 | Text string |
| 72 | URL string |
| 73 | } |
| 74 | |
| 75 | // page renders the page called name with content, and writes it with |
| 76 | // status. A page is rendered whole before anything is written, so a |
| 77 | // failure to render answers 500 rather than half a page. |
| 78 | func (q *request) page(status int, name, title string, content any) { |
| 79 | data := layout{Title: title, Style: template.CSS(styleCSS), Content: content} |
| 80 | if name == "plan" { |
| 81 | data.Script = template.JS(planJS) |
| 82 | } |
| 83 | if q.e != nil { |
| 84 | data.Nav = append(data.Nav, link{"Plan", "/"}) |
| 85 | for _, name := range q.idx.public { |
| 86 | p := q.idx.projects[name] |
| 87 | data.Nav = append(data.Nav, link{name + " source", treeURL(name, p.SourceTip, nil)}) |
| 88 | } |
| 89 | data.Fresh = freshnessOf(q.e.Collected, q.now) |
| 90 | } |
| 91 | var b bytes.Buffer |
| 92 | if err := pages[name].ExecuteTemplate(&b, "layout", data); err != nil { |
| 93 | q.s.log(fmt.Errorf("rendering %s: %w", name, err)) |
| 94 | q.w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 95 | q.w.WriteHeader(http.StatusInternalServerError) |
| 96 | fmt.Fprintln(q.w, "The server could not render this page.") |
| 97 | return |
| 98 | } |
| 99 | h := q.w.Header() |
| 100 | h.Set("Content-Type", "text/html; charset=utf-8") |
| 101 | h.Set("Content-Length", strconv.Itoa(b.Len())) |
| 102 | q.w.WriteHeader(status) |
| 103 | q.w.Write(b.Bytes()) |
| 104 | } |
| 105 | |
| 106 | // freshness is the export's collection time and its age, as of the |
| 107 | // request. An export is stale once it is older than StaleAfter. |
| 108 | type freshness struct { |
| 109 | Collected string |
| 110 | Age string |
| 111 | Stale bool |
| 112 | } |
| 113 | |
| 114 | func freshnessOf(collected string, now time.Time) *freshness { |
| 115 | at, err := parseTime(collected) |
| 116 | if err != nil { |
| 117 | return &freshness{Collected: collected, Age: "of unknown age", Stale: true} |
| 118 | } |
| 119 | age := now.Sub(at) |
| 120 | return &freshness{Collected: stamp(at), Age: ago(age), Stale: age > StaleAfter} |
| 121 | } |
| 122 | |
| 123 | // stamp is a time as the pages show it, to the minute, in UTC. |
| 124 | func stamp(t time.Time) string { return t.UTC().Format("2006-01-02 15:04 UTC") } |
| 125 | |
| 126 | func parseTime(s string) (time.Time, error) { return time.Parse(time.RFC3339, s) } |
| 127 | |
| 128 | // stampOf is stamp of an RFC 3339 time, or the text as it is when it is |
| 129 | // not one. |
| 130 | func stampOf(s string) string { |
| 131 | t, err := parseTime(s) |
| 132 | if err != nil { |
| 133 | return s |
| 134 | } |
| 135 | return stamp(t) |
| 136 | } |
| 137 | |
| 138 | // ago is an age in words. A negative age, from a clock behind the |
| 139 | // publisher's, reads as the youngest. |
| 140 | func ago(d time.Duration) string { |
| 141 | switch { |
| 142 | case d < time.Minute: |
| 143 | return "less than a minute ago" |
| 144 | case d < time.Hour: |
| 145 | return count(int(d/time.Minute), "minute") + " ago" |
| 146 | case d < 48*time.Hour: |
| 147 | return count(int(d/time.Hour), "hour") + " ago" |
| 148 | } |
| 149 | return count(int(d/(24*time.Hour)), "day") + " ago" |
| 150 | } |
| 151 | |
| 152 | // trimEnd is text without the line ends that close it, which a |
| 153 | // preformatted block would show as blank lines. |
| 154 | func trimEnd(s string) string { return strings.TrimRight(s, "\r\n") } |
| 155 | |
| 156 | func count(n int, noun string) string { |
| 157 | if n == 1 { |
| 158 | return "1 " + noun |
| 159 | } |
| 160 | return strconv.Itoa(n) + " " + noun + "s" |
| 161 | } |
| 162 | |
| 163 | // The derived states valley-plan/v1 gives a knot, and the words the pages |
| 164 | // show for each. ready is shown as free to start. |
| 165 | var stateWords = map[string]string{ |
| 166 | "ready": "free to start", |
| 167 | "in-progress": "in progress", |
| 168 | "blocked": "blocked", |
| 169 | "active": "question or idea", |
| 170 | "done": "done", |
| 171 | "abandoned": "abandoned", |
| 172 | } |
| 173 | |
| 174 | // glyphClass is the class of a state's glyph. A state the pages do not |
| 175 | // know is drawn as active. |
| 176 | func glyphClass(state string) string { |
| 177 | if _, ok := stateWords[state]; ok { |
| 178 | return state |
| 179 | } |
| 180 | return "active" |
| 181 | } |
| 182 | |
| 183 | func stateLabel(state string) string { |
| 184 | if w, ok := stateWords[state]; ok { |
| 185 | return w |
| 186 | } |
| 187 | return state |
| 188 | } |