writeonce/docs/examples/shop/main.wo
shoney.arickathil ef37b8ffa2 feat(serve+view): file serving, downloads, supported systems; rename
- rename the two libraries: writeonce-framework -> writeonce-serve
  (`use serve`), wo-html -> writeonce-view (`use view`). Names say the
  ROLE now; every sample, script, gate and live doc follows
- stories/specs/plans keep the old names: they are dated records, and
  both library READMEs carry a "renamed 2026-08-25" note
- serve/http/files.wo: StaticFiles { dir, max_bytes } — traversal
  refused not normalised, extension content types, attachment
  disposition for archives. Lifted out of the shop, which had said in
  a comment that it belonged in the framework
- shop drops its private copy and mounts the framework's
- site: /dl/*path over $WO_DIST (default ./dist), 16 MiB ceiling
- /install gains supported systems — Linux x86-64, glibc >= 2.38,
  not musl — read off `file` and the binaries' GLIBC_ symbol
  versions, not off a wish list; plus GitHub release as primary,
  /dl as mirror, and the sha256 verify step
- site-accept: 17 -> 21 checks (supported systems, gzip download with
  a binary-safe probe, checksum, /dl traversal 404)

Verified on 192.168.0.165: the real 960,820-byte tarball downloads
as application/gzip and its sha256 matches the published digest.

Gates: oop-accept MET, site 21/0, web-app 46/0, fibers 10/0,
db-actor 8/0; shop rebuilt and its /assets served by the framework.

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

51 lines
1.7 KiB
Text

-- shop/main.wo — the bootstrap (app-routing.module's analog): seed the
-- store on first boot, wire routes to the feature controllers, serve.
-- No rendering and no queries here beyond the seed.
--
-- WO_DATA=./data ./target/shop 8080 (run from the shop directory:
-- /assets/* serves from ./assets)
use serve
use serve/http
use serve/router
use product_list
use product_page
use orders
fn seed_if_empty() {
let n = 0;
for p in from x in Product take 1 select x {
n = n + 1;
}
if n > 0 {
return;
}
insert Product { sku: "keyb-75", name: "75% mechanical keyboard", price: 89.0, stock: 12 };
insert Product { sku: "mug-wal", name: "WAL-backed coffee mug", price: 14.5, stock: 40 };
insert Product { sku: "tee-own", name: "Ownership-checked t-shirt", price: 24.9, stock: 25 };
insert Product { sku: "desk-pad", name: "Deskmat (one binary edition)", price: 19.0, stock: 0 };
}
fn main(args: multi Text) -> Int {
if len(args) < 1 {
print_err("usage: shop <port> (WO_DATA=<dir> makes the store durable)");
return 2;
}
let port = parse_int(args[0]);
if port == nil {
print_err("shop: <port> must be a number");
return 2;
}
seed_if_empty();
let app = App { middleware: [], routes: [] };
app.use_mw(Mw { m: Logging {} });
app.get("/", ListProducts {});
app.get("/p/:sku", ShowProduct {});
app.post("/orders/:sku", CreateOrder {});
app.get("/orders", ListOrders {});
-- the framework's file server (serve/http): 2 MiB ceiling is well
-- above this template's stylesheet.
app.get("/assets/*path", StaticFiles { dir: "assets", max_bytes: 2097152 });
return app.serve("127.0.0.1", port);
}