Routes under prefix that share middleware, which runs after the app's own. The prefix is part of the group's type, so
a handler's Path(...) is checked against the whole route. The group belongs to the app and is released with it: do not
free it. Groups nest with group(parent, "/more"), and the middleware add up.
guard :: (request: *Request, next: Next) -> Response {
_, found := header(request, "Authorization");
if !found return error_response(request, unauthorized("a token is required"));
return next(request);
}
stats :: () -> string {
return "stats";
}
main :: () {
app: App;
defer free_app(*app);
admin := group(*app, "/admin", guard);
get(admin, "/stats", stats); // GET /admin/stats
api := test_client(*app);
refused, _ := get(api, "/admin/stats");
assert(refused.status == 401);
close(*refused);
token := Header_Field.[.{ name = "Authorization", value = "secret" }];
allowed, _ := get(api, "/admin/stats", token);
assert(allowed.status == 200);
close(*allowed);
}Doesn't run in the browser (skipped on wasm).
group :: (
app: *App,
$prefix: string,
middleware: ..Middleware
) -> *Group(prefix)
Routes under prefix that share middleware, which runs after the app's own. The prefix is part of the group's type, so
a handler's Path(...) is checked against the whole route. The group belongs to the app and is released with it: do not
free it. Groups nest with group(parent, "/more"), and the middleware add up.
Parameters
- app: *App
- $prefix: string
- middleware: ..Middleware
Returns
- *Group(prefix)
guard :: (request: *Request, next: Next) -> Response {
_, found := header(request, "Authorization");
if !found return error_response(request, unauthorized("a token is required"));
return next(request);
}
stats :: () -> string {
return "stats";
}
main :: () {
app: App;
defer free_app(*app);
admin := group(*app, "/admin", guard);
get(admin, "/stats", stats); // GET /admin/stats
api := test_client(*app);
refused, _ := get(api, "/admin/stats");
assert(refused.status == 401);
close(*refused);
token := Header_Field.[.{ name = "Authorization", value = "secret" }];
allowed, _ := get(api, "/admin/stats", token);
assert(allowed.status == 200);
close(*allowed);
}Doesn't run in the browser (skipped on wasm).
group :: (
parent: *Group($P),
$prefix: string,
middleware: ..Middleware
) -> *Group(#run join_template(P, prefix))
A group nested in parent: its prefix is the parent's followed by prefix, and its middleware are the parent's followed by
middleware. Like any group it belongs to the app and is not freed by the caller.
Parameters
- parent: *Group($P)
- $prefix: string
- middleware: ..Middleware
Returns
- *Group(#run join_template(P, prefix))