Skip to content
CryoCryo home
Stdlibnet::http

router

import std::net::http::router; · source

Router

type struct Router {
    routes: Array<Route>;

    not_found_handler: Option<(Request) -> Response>;
    method_not_allowed_handler: Option<(Request) -> Response>;
    static new() -> Router;
    get(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    get(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    post(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    post(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    put(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    put(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    del(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    del(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    patch(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    patch(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    head(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    head(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    options(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;
    options(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    route(mut &this, method: Method, pattern: Str, handler: (Request) -> Response) -> &Router;
    route(mut &this, method: Method, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;
    on_not_found(mut &this, handler: (Request) -> Response) -> &Router;
    on_method_not_allowed(mut &this, handler: (Request) -> Response) -> &Router;
    dispatch(&this, req: Request) -> Response;
}

Register routes with the per-method helpers, then hand a pointer to the server. Routes are evaluated in registration order; the first route where both method and path match wins.

/                     exact root
/users                exact match
/users/:id            `:id` captures any non-empty segment
/files/:dir/:name     two named captures

Each per-method helper has an overload taking a RouteKind, which is how you register a prefix route — one that matches as long as the pattern is a prefix of the request path, accepting trailing segments. route(method, pattern, handler) is the generic form. Every registration returns &Router, so calls chain.

on_not_found(handler) and on_method_not_allowed(handler) override the fallbacks. dispatch(req) is what the server calls.

Captured segments come back through Request::param:

function show_user(req: Request) -> Response {
    match (req.param(Str::new("id"))) {
        Option::Some(id) => { return Response::text(StatusCode::ok(), id.as_str()); }
        Option::None     => { return Response::new(StatusCode::bad_request()); }
    }
}

Route and RouteKind

type struct Route {
    method:  Method;
    pattern: Str;

    handler: (Request) -> Response;

    kind: RouteKind;
}
type enum RouteKind {
    Exact;
    Prefix;
}

One registered route: the method, the pattern, the handler, and whether the pattern must match the whole path (Exact) or only lead it (Prefix).

Trait implementations

implement trait Drop for struct Router

implement trait Drop for struct Route