writeonce/docs/examples/writeonce-framework/app.wo
shoney.arickathil d84b72f0b4 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

72 lines
2.3 KiB
Text

-- app.wo — the assembly: an App holds the middleware chain and the route
-- table, satisfies http's Dispatcher interface, and serves.
--
-- let app = App { middleware: [], routes: [] };
-- app.use_mw(Mw { m: Auth { token: t } });
-- app.add(Route { method: "GET", pattern: "/products/:id", h: Show {} });
-- return app.serve("127.0.0.1", port);
--
-- Dispatch order: middleware in registration order (a Resp short-circuits),
-- then the first matching route (method + pattern), else the framework 404.
-- The serve loop wraps dispatch in `try`, so a trapping handler answers 500
-- and the server survives.
use http
use router
pub class App {
middleware: multi Mw
routes: multi Route
fn use_mw(take m: Mw) {
push(self.middleware, m);
}
fn add(take r: Route) {
push(self.routes, r);
}
-- Registration helpers — the ctor-literal-into-take shape, per method.
fn get(pattern: Text, take h: Handler) {
push(self.routes, Route { method: "GET", pattern: pattern, h: h });
}
fn post(pattern: Text, take h: Handler) {
push(self.routes, Route { method: "POST", pattern: pattern, h: h });
}
fn put(pattern: Text, take h: Handler) {
push(self.routes, Route { method: "PUT", pattern: pattern, h: h });
}
fn delete_(pattern: Text, take h: Handler) {
push(self.routes, Route { method: "DELETE", pattern: pattern, h: h });
}
fn dispatch(mut req: Req) -> Resp {
for mw in self.middleware {
let short = mw.m.before(req);
if short != nil { return short; }
}
-- Path-first matching so a wrong-method hit on a known path answers
-- 405 with the Allow header (registration order) instead of a 404.
let allow = "";
for r in self.routes {
let params: map<Text, Text> = {};
if route_match(r.pattern, req.path, params) {
if r.method == req.method {
-- captures land on the borrowed request itself (mut) — a failed
-- match above never touched it, since captures collect locally
for k, v in params { req.params[k] = v; }
return r.h.handle(req);
}
if allow == "" { allow = "${r.method}"; } else { allow = "${allow}, ${r.method}"; }
}
}
if allow != "" { return method_not_allowed(allow); }
return not_found();
}
fn serve(host: Text, port: Int) -> Int {
return http.serve(host, port, self);
}
}