Skip to content
CryoCryo home
Stdlibnet::http

router

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

Types

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()); }
    }
}

Properties

PropertyType
routesArray<Route>

Constructors

static new() -> Router;

Create an empty router with no registered routes and built-in fallback handlers (plain-text 404 / 405).

Methods

not_found_handler: Option<(Request) -> Response>;
method_not_allowed_handler: Option<(Request) -> Response>;
get(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a GET exact route.

get(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a GET route with explicit kind; pass RouteKind::Prefix to match any path that begins with pattern.

post(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a POST exact route.

post(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a POST route with explicit kind.

put(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a PUT exact route.

put(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a PUT route with explicit kind.

del(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a DELETE exact route.

del(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a DELETE route with explicit kind.

patch(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a PATCH exact route.

patch(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a PATCH route with explicit kind.

head(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register a HEAD exact route.

head(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a HEAD route with explicit kind.

options(mut &this, pattern: Str, handler: (Request) -> Response) -> &Router;

Register an OPTIONS exact route.

options(mut &this, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register an OPTIONS route with explicit kind.

route(mut &this, method: Method, pattern: Str, handler: (Request) -> Response) -> &Router;

Register an exact route for an arbitrary Method.

route(mut &this, method: Method, pattern: Str, handler: (Request) -> Response, kind: RouteKind) -> &Router;

Register a route for an arbitrary Method with explicit kind.

on_not_found(mut &this, handler: (Request) -> Response) -> &Router;

Set a custom 404 handler. Called when no registered route matches the request path at all. The handler receives the original request with an empty params map.

on_method_not_allowed(mut &this, handler: (Request) -> Response) -> &Router;

Set a custom 405 handler. Called when the request path matches at least one registered route but none of those routes accept the request method. The handler receives the original request with an empty params map.

dispatch(&this, req: Request) -> Response;

Dispatch req to the first matching route.

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).

Route
Properties
PropertyType
methodMethod
patternStr
kindRouteKind
Methods

####### handler: (Request) -> Response;

RouteKind
Variants
VariantNotes
ExactAll path segments must match; no trailing segments allowed.
PrefixThe pattern must be a prefix of the path; any extra trailing segments on the request path are accepted and ignored.

Trait implementations

Router
implement trait Drop for struct Router
Route
implement trait Drop for struct Route