package main // The zoo: every promise in ZOO.md as an exhibit, working live, with a light. Odin on the wasm // runtime: one fresh instance per request and per job run. // // / every exhibit and its light // /api// an exhibit's JSON (check: run its check now; seen: "this worked for me") // /how/ the exhibit's source, the bytes this very program was built from (exhibit 19) // /lights.json every light, for tests and anyone curious // /index.txt one line per exhibit (promise and building block), for a model's brief // /how/.txt exhibit n's files as plain text // /platform.md what an app gets: root's PLATFORM.md, the running version, for anyone // // nginx also asks whether a browser may listen on an `auth` channel (channel_ok); and the host runs // the jobs of ratcloud.conf's `cron` (job): tick, selfcheck, nightly, workbench. import "core:encoding/json" import "core:fmt" import "core:strings" import "core:time" import "ratcloud:rc" main :: proc() { rc.serve(handle, channel_ok, job) } SELF_SRC :: Src{"main.odin", #load("main.odin", string)} channel_ok :: proc(r: ^rc.Request, channel, id: string) -> bool { u, signed := rc.me() me := &u if signed else nil for &e in exhibits() { if e.channel == nil do continue if allow, mine := e.channel(channel, id, me); mine do return allow } return false } g_req: ^rc.Request handle :: proc(r: ^rc.Request, w: ^rc.Response) { g_req = r defer robot_done() u, signed := rc.me() me := &u if signed else nil path := r.path if strings.has_prefix(path, "/api/") { api(r, w, me) return } if strings.has_prefix(path, "/how/") { rest := path[5:] if strings.has_suffix(rest, ".txt") {how_txt(w, int(atoi(rest[:len(rest) - 4]))); return} how_page(r, w, int(atoi(rest)), me) return } switch path { case "/": page(w, me); return case "/lights.json": lights_json(w); return case "/index.txt": // A model's brief lists the exhibits, one line each. rc.set_header(w, "Content-Type", "text/plain; charset=utf-8") rc.set_header(w, "Cache-Control", "max-age=300") for e in exhibits() do rc.write(w, fmt.tprintf("%d. %s (%s%s): %s\n", e.n, e.title, e.wing, ", not built yet" if e.kind == "waiting" else "", e.block)) return case "/platform.md": // Root publishes PLATFORM.md into every app's facts; the zoo, being the documentation, serves // its copy so a model working in a clone (no facts there) reads the version that is running. doc, ok := rc.fact("PLATFORM.md") rc.status(w, 200 if ok else 404) rc.set_header(w, "Content-Type", "text/markdown; charset=utf-8") rc.set_header(w, "Cache-Control", "max-age=300") rc.write(w, doc if ok else "PLATFORM.md is not published here yet.\n") return } rc.status(w, 404) rc.set_header(w, "Cache-Control", "no-store") rc.render(w, NOT_FOUND_HTML, {{"head", rc.raw(rc_head())}, {"header", rc.raw(rc_header("Zoo", me, "/api/3/signout", false))}}) } NOT_FOUND_HTML :: `Not found ยท Zoo{{head}}{{header}}
Nothing here

The zoo has no page at this address.

Go to the zoo
` // ---- /api// ---- Api :: struct { r: ^rc.Request, w: ^rc.Response, do_: string, me: ^rc.User, post: bool, in_: json.Value, // the POST's JSON body, or the query as an object of strings } reply :: proc(a: ^Api, v: any, status := 200) { rc.status(a.w, status) rc.set_header(a.w, "Cache-Control", "no-store") rc.set_header(a.w, "Content-Type", "application/json") rc.write(a.w, to_json(v)) rc.write(a.w, "\n") } reply_raw :: proc(a: ^Api, json_text: string, status := 200) { rc.status(a.w, status) rc.set_header(a.w, "Cache-Control", "no-store") rc.set_header(a.w, "Content-Type", "application/json") rc.write(a.w, json_text) rc.write(a.w, "\n") } reply_err :: proc(a: ^Api, status: int, msg: string) { reply_raw(a, fmt.tprintf(`{{"error":%s}}`, jq(msg)), status) } in_str :: proc(a: ^Api, k: string) -> string {return jstr(a.in_, k)} in_int :: proc(a: ^Api, k: string) -> i64 {return jint(a.in_, k)} api :: proc(r: ^rc.Request, w: ^rc.Response, me: ^rc.User) { a := Api{r = r, w = w, me = me, post = r.method == "POST"} rest := r.path[5:] slash := strings.index_byte(rest, '/') if slash < 0 {reply_err(&a, 404, "no such exhibit"); return} n := int(atoi(rest[:slash])) a.do_ = rest[slash + 1:] e, ok := exhibit(n) if !ok || itoa(i64(n)) != rest[:slash] {reply_err(&a, 404, "no such exhibit"); return} if !rc.csrf_ok(r) {reply_err(&a, 403, "refused: not from the zoo's own page"); return} if a.post { body, _ := rc.body(r) if v, err := json.parse(body, parse_integers = true); err == nil do a.in_ = v } else { o := make(json.Object) for k, v in rc.query(r) do o[k] = json.String(v) a.in_ = o } if a.do_ == "check" && e.check != nil { // POST runs it now, unless it ran (or started) in the last 15 seconds: anyone can press this, // and a run costs a robot sign-in, a model call, B2 and push. The slot is taken atomically, so // a burst runs it once. GET (an anywhere) only reads the light. if a.post && claim_check(e.n, 15) do run_check(e) s, found := stored(e.n) if !found { reply_raw(&a, fmt.tprintf(`{{"n":%d,"title":%s,"ok":false,"detail":"not checked yet","at":null,"data":null}}`, e.n, jq(e.title))) return } reply_raw(&a, fmt.tprintf(`{{"n":%d,"title":%s,"ok":%v,"detail":%s,"at":%s,"data":%s}}`, e.n, jq(e.title), s.ok, jq(s.detail), jq(iso(s.at)), s.data if s.data != "" else "null")) return } if a.do_ == "seen" && a.post && e.kind == "human" { if me == nil {reply_err(&a, 401, "sign in first, so the zoo can say who saw it work"); return} seen(e.n, me.id) l := light(e) reply_raw(&a, fmt.tprintf(`{{"ok":true,"light":{{"color":%s,"text":%s}}}}`, jq(l.color), jq(l.text))) return } if e.api != nil && e.api(&a) do return reply_err(&a, 404, fmt.tprintf("exhibit %d has no '%s'", e.n, a.do_)) } // ---- /lights.json ---- lights_json :: proc(w: ^rc.Response) { b := strings.builder_make() checked := meta_int("checked_at") fmt.sbprintf(&b, `{{"env":%s,"checked":%s,"lights":[`, jq(env().env), jq(iso(checked)) if checked > 0 else "null") for &e, i in exhibits() { l := light(&e) if i > 0 do strings.write_byte(&b, ',') fmt.sbprintf(&b, `{{"n":%d,"title":%s,"kind":%s,"color":%s,"text":%s,"at":%s}}`, e.n, jq(e.title), jq(e.kind), jq(l.color), jq(l.text), jq(iso(l.at)) if l.at > 0 else "null") } strings.write_string(&b, `],"alerts":[`) for x, i in alerts_recent(10) { if i > 0 do strings.write_byte(&b, ',') fmt.sbprintf(&b, `{{"at":%s,"kind":%s,"title":%s,"body":%s,"results":%s}}`, jq(iso(x.at)), jq(x.kind), jq(x.title), jq(x.body), x.results) } strings.write_string(&b, "]}\n") rc.set_header(w, "Content-Type", "application/json") rc.set_header(w, "Cache-Control", "no-store") rc.write(w, strings.to_string(b)) } // ---- the jobs (ratcloud.conf `cron`): the host runs each in a fresh instance on its schedule ---- job :: proc(name: string, w: ^rc.Response) { defer robot_done() switch name { case "tick": // Exhibit 21: write the time. That is all. meta_set("last_tick", itoa(now())) case "selfcheck": selfcheck(w) case "nightly": // Exhibit 8's upkeep: if a valid push replaced zoo-broken's deliberate typo, put it back. res := broken_reassert() res.at = now() meta_set("broken.nightly", to_json(res)) rc.write(w, fmt.tprintf("exhibit 8 upkeep (%s): %s: %s\n", res.branch, res.result, res.detail)) if res.result != "still refused" && res.result != "put back" do rc.status(w, 500) case "workbench": // Exhibit 25's run in the robot's workbench: at most once a day, live only. if said, did := e25_job(); did { rc.write(w, said) rc.write(w, "\n") if strings.contains(said, "FAILED") do rc.status(w, 500) } case: rc.status(w, 404) rc.write(w, fmt.tprintf("no job %q\n", name)) } } // Exhibit 22's job: every five minutes, run every self-running exhibit's check and store the results // as lights. Exhibit 22 runs last, since it judges the others. selfcheck :: proc(w: ^rc.Response) { t0 := time.tick_now() g_in_job = true last: ^Exhibit for &e in exhibits() { if e.n == 22 {last = &e; continue} if e.check == nil || e.kind != "self" do continue r := run_check(&e) if !r.ok do rc.write(w, fmt.tprintf("exhibit %d: %s\n", r.n, r.detail)) } if last != nil { r := run_check(last) if !r.ok do rc.write(w, fmt.tprintf("exhibit %d: %s\n", r.n, r.detail)) } alerts_run(w) meta_set("checked_at", itoa(now())) meta_set("checked_ms", itoa(i64(time.duration_milliseconds(time.tick_since(t0))))) rc.write(w, fmt.tprintf("checked in %d ms, memory %d KiB\n", i64(time.duration_milliseconds(time.tick_since(t0))), rc.bump_top() / 1024)) } // The five-minute job, not a "check now" from the page (PHP's PHP_SAPI === "cli"): the rounds that // take minutes (18, 29, 30) start only here, where the job's own time allows them. g_in_job: bool