writeonce/docs/examples/writeonce-framework/app.wo
shoney.arickathil d24c705860 feat: iterations 19 + 17 — Float/Bytes scalars (.wob v5), library kind + internal/
- Float full stack: literals (fraction/exponent; `0..10` still a range), f64
  opcodes 34-41, @table column, WAL bit-exact replay, json fractions in and
  shortest-round-trip out. IEEE-quiet — FDIV never traps where DIV does.
- Bytes: a wo_str with its own class id, so alloc/free/copy are shared but no
  Text builtin accepts one; len/at/slice/eq/concat, base64 both ways, json
  boundary as base64; TEXT_COPY preserves the kind.
- No implicit Int/Float mixing (WO-E201 in the typechecker, not the emitter,
  which picks the opcode from one side and would misread the other).
- One IEEE deviation: float_cmp total order (NaN last, -0.0 == +0.0) for
  indexes and order-by, keys canonicalized to match. `?Float` nil is a
  reserved quiet NaN — the zero word is +0.0, WO_NIL_SCALAR's bits are -2.0.
- Renderer prefers fixed over exponential in 1e-6..1e21: pure shortest makes
  a price of 900.0 read `9e+02`. One renderer for interp/json/float_to_text.
- Fixed en route: lexer double-counted the leading digit; is_scalar_shaped
  took Float/Bytes as Int-shaped; Bytes ownership needed a shared heap-scalar
  predicate or temps never dropped; order-by bit-compared negatives backwards.
- Iteration 17: `kind = "library"` (absent = program; bad value = WO-E109),
  entry-less check mode retiring the `--emit` workaround, Go's `internal/` as
  WO-E108 at the consumer's `use`. Driver-only; VM/.wob/GC untouched.
- Framework reorg: internal/{parse,serve}.wo; http/form.wo split out to keep
  media_type/form_values public (parse.wo had grown public surface).
- Docs: link audit (97 -> 88 broken, conflict markers resolved, 2 duplicate
  stories removed), 00-code-review verified 26/27, iterations re-sequenced.
- Also carries the pre-staged pub(read)/using/#if work from the index.
- Gates: corpus 103/0, test_wal 156/0, web-app 26/0, oop-accept ALL MET.

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

75 lines
2.5 KiB
Text

-- app.wo — the assembly: an App holds the middleware chain and the route
-- table, satisfies internal'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
-- iteration 17: the serve loop is library-internal now (internal/serve.wo).
-- Legal here: the `internal/` boundary refuses CONSUMERS, not the library.
use internal
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 internal.serve(host, port, self);
}
}