collect-prefixes.tri (1446B)
1 !import consumers_source_worker as Source 2 !import consumers_lexical_worker as L 3 !import prelude * 4 !import io * 5 !export * 6 -- Compile public SCP consumers once, then collect and choose route suffixes. 7 -- This is a readable composition example, not a deployment benchmark: it 8 -- reads .scp sources when launched. Pinned CAS applications avoid that cost. 9 10 loadPath path initial = Source.loadSource path initial 11 12 load name initial = loadPath (append "lib/consumer/" (append name ".scp")) initial 13 select collector chooser routes fallback = 14 bindResult (L.lRunChecked collector routes) (suffixes _ : 15 bindResult (L.lRunChecked chooser (pair suffixes fallback)) (first _ : 16 ok (pair (length suffixes) first) t)) 17 18 run = do onOk_ 19 prefix <- load "stripPrefix" "/api/" 20 collect <- load "mapMaybe" prefix 21 nonempty <- load "nonempty" t 22 find <- load "find" nonempty 23 choose <- loadPath "demos/consumers/collect-prefixes/choose-found.scp" find 24 pure (bindResult (select collect choose 25 ["/api/" "/other" "/api/item" "/api/health"] "none") (found _ : 26 bindResult (select collect choose ["/other"] "none") (missing _ : 27 ok (pair found (snd missing)) t))) 28 29 main = io (onResult_ run 30 (problem : putStrLn "collect-prefixes-failed") 31 (outcome : do bind 32 _ <- putStrLn (append "matches=" (showNumber (fst (fst outcome)))) 33 _ <- putStrLn (append "first=" (snd (fst outcome))) 34 putStrLn (append "unmatched-first=" (snd outcome))))