writeonce/docs/examples/writeonce-framework/router/router.wo
shoney.arickathil 3d33b7bf58 feat(framework): v1 polish — helpers, 405+Allow, HEAD, Logging, set_header
- App.get/post/put/delete_(pattern, take h: Handler) — the take-interface
  shape probe-proven release + ASan before landing; retires plan-16
  deviation 1; delete_ because delete is the query keyword
- dispatch matches path-first: wrong method on a known path answers 405
  with Allow in registration order; unknown path stays 404
- HEAD routed as GET, body suppressed, Content-Length names the body a
  GET would carry (serialize gains head_only)
- Logging middleware (request line to stderr) ships in router/
- set_header(mut r, name, value) — the builder escape hatch
- web-app registers through the helpers (dogfood); README documents all
- gate grows 14 -> 16: 405+Allow, HEAD-vs-GET content-length equality
- all gates green: web-app 16/0, woc-test 540/0, oop-e2e 89/0,
  deps-accept 8/0, log-watcher 7/0, employee 8/0
- board/story: iteration 17 parked (spec+plan ready on library-internal),
  16 carries the v1-polish landing, order list updated

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 03:04:57 +02:00

68 lines
2.2 KiB
Text

-- router/router.wo — the handler model and the route table.
--
-- No function values in this language, by doctrine — so a handler is a
-- CLASS satisfying the Handler interface (its fields are the closure
-- substitute), dispatched structurally (ICALL). A class missing `handle`
-- is a compile error (WO-E205). Middleware is its own interface because
-- the signature differs: `before` returns `?Resp` — nil means continue,
-- a Resp short-circuits (401 gates, redirects).
pub interface Handler {
fn handle(req: Req) -> Resp
}
pub interface Middleware {
fn before(req: Req) -> ?Resp
}
-- One route: method + pattern + the handler value. Built with a ctor
-- literal at the registration site (`Route { method: "GET", pattern:
-- "/products/:id", h: ProductShow {} }`) — the exact ownership shape the
-- conformance corpus pins (run/container-owned-move).
pub class Route {
method: Text
pattern: Text
h: Handler
}
-- Middleware wrapper record, same shape as Route for the same reason.
pub class Mw {
m: Middleware
}
-- Request-line logging, the one middleware every framework ships: method +
-- path to stderr, never short-circuits. `pad` is the record-class ctor
-- convention (every stateless handler carries one Int field).
pub class Logging {
pad: Int
fn before(req: Req) -> ?Resp {
print_err("${req.method} ${req.path}");
return nil;
}
}
-- Does `pattern` match `path`? Fills `params` with :name captures.
-- Segments split on '/', empties dropped (so "/a//b" == "/a/b" and the
-- root "/" is the empty segment list). First mismatch wins; a :segment
-- captures anything non-empty.
pub fn route_match(pattern: Text, path: Text, mut params: map<Text, Text>) -> Bool {
let ps = split(pattern, "/");
let xs = split(path, "/");
let psegs: multi Text = [];
for s in ps { if s != "" { push(psegs, s); } }
let xsegs: multi Text = [];
for s in xs { if s != "" { push(xsegs, s); } }
if len(psegs) != len(xsegs) { return false; }
let i = 0;
while i < len(psegs) {
let p = psegs[i];
let x = xsegs[i];
if starts_with(p, ":") {
params[substr(p, 1, len(p) - 1)] = x;
} else {
if p != x { return false; }
}
i = i + 1;
}
return true;
}