registry.tri (1970B)
1 !import prelude * 2 !import io * 3 !import http as H 4 !import consumers_structural as SCP 5 !import consumers_registry_path as Paths 6 !export * 7 -- A small reusable server-consumer library: inspectable reply and ReadFile 8 -- commands, explicit receiver identity/state, and an IO interpreter. No new 9 -- host action or request parsing is introduced. 10 11 reply status contentType body = 12 SCP.outFork (SCP.outLiteral 0) 13 (SCP.outFork (SCP.outLiteral status) 14 (SCP.outFork (SCP.outLiteral contentType) body)) 15 readBundle receiver root hash state = 16 SCP.outFork (SCP.outLiteral 1) 17 (SCP.outFork (SCP.outLiteral receiver) 18 (SCP.outFork root (SCP.outFork hash state))) 19 20 -- A program is (name, consumer); caller supplies the initial environment. 21 -- An Await contains its receiver name AND the environment to restore. This 22 -- bounded library supports same-program receivers only and checks the name. 23 routeStep_ self request program state = 24 lazyResult 25 (message _ : pure (H.errorResponse 500 message)) 26 (command _ : matchPair (tag payload : 27 lazyBool 28 (_ : matchPair (status more : matchPair (contentType body : 29 pure (H.response status contentType body)) more) payload) 30 (_ : lazyBool 31 (_ : matchPair (receiver more : matchPair (root rest : 32 matchPair (hash resumeState : 33 lazyBool 34 (_ : onResult_ (readFile (Paths.bundleObjectPath root hash)) 35 (problem : self (err problem t) program resumeState) 36 (bytes : self (ok bytes t) program resumeState)) 37 (_ : pure (H.errorResponse 500 "unknown receiver")) 38 (equal? receiver (fst program))) rest) more) payload) 39 (_ : pure (H.errorResponse 500 "unknown consumer command")) 40 (equal? tag 1)) 41 (equal? tag 0)) command) 42 (SCP.runConsumer (SCP.makeConsumer state (snd (snd program))) request) 43 routeRun program state request = y routeStep_ request program state