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, "
\n

{{1}}

\n
\n", {{"1", wing}}) for &x, i in all { if x.wing != wing do continue rc.render_to(&wings, `
{{4}}

{{5}}

`, {{"1", lights[i].color}, {"2", x.n}, {"3", rc.raw(" data-try" if x.try_ != "" else "")}, {"4", x.n}, {"5", x.title}}) rc.render_to(&wings, `{{1}}

{{2}}

See it working`, {{"1", rc.raw(light_html(lights[i]))}, {"2", x.promise}}) if x.show != nil do rc.render_to(&wings, `
{{1}}
`, {{"1", rc.raw(x.show(me))}}) rc.render_to(&wings, `

{{1}}

` + "\n", {{"1", KIND_WORDS(x.kind)}}) if x.script.text != "" {strings.write_string(&scripts, x.script.text); strings.write_byte(&scripts, '\n')} } strings.write_string(&wings, "
\n
\n") } tries_html := fmt.tprintf(`

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/, the zoo's own page. true: apps's file // view of the commit this zoo runs (https://apps./zoo/files//), from root's // mirror of the zoo's repo. apps shows an app's source to everyone only under `source = public` in // its ratcloud.conf, a key root does not know yet. Flip a line once it does. how_on_apps :: proc() -> bool { switch env().env { case "live": return false case "staging": return false } return false } how_commit :: proc() -> string {return jstr(fact("deploy.json"), env().env, "commit")} how_apps_url :: proc(kind, file: string) -> string { apps := rc.app_url("apps") if i := strings.index(apps, "-staging."); i >= 0 do apps = cat(apps[:i], apps[i + 8:]) parts := strings.split(file, "/") for &p in parts do p = rc.percent_encode(p) return fmt.tprintf("%s/%s/%s/%s/%s", apps, env().app, kind, how_commit(), strings.join(parts, "/")) } // A card's "how" link: apps's view of the exhibit's own file when the switch is on, else /how/. how_url :: proc(e: ^Exhibit) -> string { if how_on_apps() && how_commit() != "" do return how_apps_url("files", e.src.path) return fmt.tprintf("/how/%d", e.n) } how_page :: proc(r: ^rc.Request, w: ^rc.Response, n: int, me: ^rc.User) { e, ok := exhibit(n) if !ok {rc.status(w, 404); rc.write(w, "no such exhibit\n"); return} files := how_files(e) rc.set_header(w, "Cache-Control", "no-store") if raw, has := rc.query(r)["raw"]; has { // Only the files this exhibit names: never anything else. for f in files { if f.path != raw do continue rc.set_header(w, "Content-Type", "text/plain; charset=utf-8") rc.write(w, f.text) return } rc.status(w, 404) rc.write(w, "not one of this exhibit's files\n") return } b := strings.builder_make() for f in files { sum: [32]u8 hash.hash_string_to_buffer(.SHA256, f.text, sum[:]) rc.render_to(&b, `

{{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, "

\n
")
		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, "
\n") } rc.set_header(w, "Content-Type", "text/html; charset=utf-8") rc.render(w, HOW_HTML, { {"title", e.title}, {"n", n}, {"promise", e.promise}, {"block", e.block}, {"head", rc.raw(rc_head())}, {"css_v", version_of(ZOO_CSS)}, {"header", rc.raw(rc_header("Zoo", me, "/api/3/signout", false))}, {"commit", short(how_commit() if how_commit() != "" else "unknown", 10)}, {"env", env().env}, {"files", rc.raw(strings.to_string(b))}, }) } how_txt :: proc(w: ^rc.Response, n: int) { rc.set_header(w, "Content-Type", "text/plain; charset=utf-8") rc.set_header(w, "Cache-Control", "no-store") e, ok := exhibit(n) if !ok {rc.status(w, 404); rc.write(w, "no such exhibit\n"); return} rc.write(w, fmt.tprintf("Exhibit %d: %s\nPromise: %s\nBlock: %s\n", e.n, e.title, e.promise, e.block)) for f in how_files(e) { rc.write(w, fmt.tprintf("\n=== %s ===\n", f.path)) rc.write(w, f.text) } }