{{5}}
{{2}}
See it working
`, {{"1", rc.raw(light_html(lights[i]))}, {"2", x.promise}}) if x.show != nil do rc.render_to(&wings, `{{1}}
package main
// The front page (the row of lights, then every exhibit by wing) and "how" (exhibit 19: an
// exhibit's source, the very bytes this program was built from).
import "core:crypto/hash"
import "core:fmt"
import "core:strings"
import "ratcloud:rc"
PAGE_HTML :: #load("web/page.html", string)
HOW_HTML :: #load("web/how.html", string)
ZOO_CSS :: #load("public/zoo.css", string) // served by nginx from public/; here only for its version
ZOO_JS :: #load("public/zoo.js", string)
// The first 8 hex of a file's sha256: its version in a URL, so a new file is a new address.
version_of :: proc(s: string) -> string {
out: [32]u8
hash.hash_string_to_buffer(.SHA256, s, out[:])
return hex(out[:4])
}
light_html :: proc(l: Light) -> string {
when_ := cat(" ", ago(l.at)) if l.at > 0 else ""
// A red light says what is wrong, then when it last worked, or that it never has.
text: string
if l.prefix != "" {
pre := cat(h(l.prefix), when_) if when_ != "" else "never worked yet"
text = fmt.tprintf(`%s (%s)`, h(l.text), pre)
} else {
text = cat(h(l.text), when_)
}
return fmt.tprintf(`%s`, l.color, text)
}
KIND_WORDS :: proc(kind: string) -> string {
switch kind {
case "self": return "checks itself"
case "human": return "a person checks this"
}
return "not built yet"
}
page :: proc(w: ^rc.Response, me: ^rc.User) {
all := exhibits()
lights := make([]Light, len(all))
count: map[string]int
for &e, i in all {lights[i] = light(&e); count[lights[i].color] += 1}
e := env()
deploy := jget(fact("deploy.json"), e.env)
dots := strings.builder_make()
tries := strings.builder_make()
scripts := strings.builder_make()
for &x, i in all {
rc.render_to(&dots, `{{4}}`, {{"1", x.n}, {"2", lights[i].color}, {"3", fmt.tprintf("%d. %s: %s", x.n, x.title, lights[i].text)}, {"4", x.n}})
if x.try_ != "" && me != nil {
if strings.builder_len(tries) > 0 do strings.write_string(&tries, " · ")
rc.render_to(&tries, `{{2}}`, {{"1", x.n}, {"2", x.try_}})
}
}
wings := strings.builder_make()
for wing in WINGS {
rc.render_to(&wings, " {{2}} {{1}}{{1}}
\n {{5}}
See it working
`, {{"1", rc.raw(light_html(lights[i]))}, {"2", x.promise}})
if x.show != nil do rc.render_to(&wings, `
Things to try: %s.
`, strings.to_string(tries)) if me != nil && strings.builder_len(tries) > 0 else "" legend := `A red light is a promise broken right now. The zoo shows it rather than hiding it, and the card says what is wrong; the green ones still work.
` if count["red"] > 0 else "" rc.set_header(w, "Content-Type", "text/html; charset=utf-8") rc.set_header(w, "Cache-Control", "no-store") rc.render(w, PAGE_HTML, { {"env_title", "" if e.env == "live" else fmt.tprintf(" (%s)", e.env)}, {"head", rc.raw(rc_head())}, {"css_v", version_of(ZOO_CSS)}, {"js_v", version_of(ZOO_JS)}, {"me_id", itoa(me.id) if me != nil else ""}, {"header", rc.raw(rc_header("Zoo", me, "/api/3/signout", false))}, {"home", home_url()}, {"checked", rc.raw(ago(meta_int("checked_at")))}, {"green", count["green"]}, {"red", count["red"]}, {"amber", count["amber"]}, {"grey", count["grey"]}, {"legend", rc.raw(legend)}, {"tries", rc.raw(tries_html)}, {"dots", rc.raw(strings.to_string(dots))}, {"alerts", rc.raw(alerts_card(me))}, {"wings", rc.raw(strings.to_string(wings))}, {"commit", short(jstr(deploy, "commit") or_else "unknown", 10)}, {"env", e.env}, {"by", jstr(deploy, "by") or_else "?"}, {"arrived", rc.raw(ago(parse_time(jstr(deploy, "at"))))}, {"other_host", other_env_host()}, {"other_name", "the staging zoo" if e.env == "live" else "the live zoo"}, {"apps", rc.app_url("apps")}, {"scripts", rc.raw(strings.to_string(scripts))}, }) } // ---- how (exhibit 19) ---- // // The switch: where a card's "how" link opens. false: /how/{{1}} sha256 {{2}} · raw`, {{"1", f.path}, {"2", hex(sum[:6])}, {"3", n}, {"4", rc.percent_encode(f.path)}})
if how_on_apps() do rc.render_to(&b, ` · on apps`, {{"1", how_apps_url("files", f.path)}})
strings.write_string(&b, "")
lines := strings.split(strings.trim_right(f.text, "\n"), "\n")
for line, i in lines do rc.render_to(&b, "{{1}}{{2}}\n", {{"1", i + 1}, {"2", line}})
strings.write_string(&b, "