logo

Struct poem::Route

source · []
pub struct Route { /* private fields */ }
Expand description

Routing object

You can match the full path or wildcard path, and use the Path extractor to get the path parameters.

Errors

Example

use poem::{
    get, handler,
    http::{StatusCode, Uri},
    web::Path,
    Endpoint, Request, Route,
};

#[handler]
async fn a() {}

#[handler]
async fn b(Path((group, name)): Path<(String, String)>) {
    assert_eq!(group, "foo");
    assert_eq!(name, "bar");
}

#[handler]
async fn c(Path(path): Path<String>) {
    assert_eq!(path, "d/e");
}

let app = Route::new()
    // full path
    .at("/a/b", get(a))
    // capture parameters
    .at("/b/:group/:name", get(b))
    // capture tail path
    .at("/c/*path", get(c))
    // match regex
    .at("/d/<\\d+>", get(a))
    // capture with regex
    .at("/e/:name<\\d+>", get(a));

// /a/b
let resp = app
    .call(Request::builder().uri(Uri::from_static("/a/b")).finish())
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);

// /b/:group/:name
let resp = app
    .call(
        Request::builder()
            .uri(Uri::from_static("/b/foo/bar"))
            .finish(),
    )
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);

// /c/*path
let resp = app
    .call(Request::builder().uri(Uri::from_static("/c/d/e")).finish())
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);

// /d/<\\d>
let resp = app
    .call(Request::builder().uri(Uri::from_static("/d/123")).finish())
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);

// /e/:name<\\d>
let resp = app
    .call(Request::builder().uri(Uri::from_static("/e/123")).finish())
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);

Nested

use poem::{
    handler,
    http::{StatusCode, Uri},
    Endpoint, Request, Route,
};

#[handler]
fn index() -> &'static str {
    "hello"
}

let app = Route::new().nest("/foo", Route::new().at("/bar", index));

let resp = app
    .call(
        Request::builder()
            .uri(Uri::from_static("/foo/bar"))
            .finish(),
    )
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "hello");

Nested no strip

use poem::{
    handler,
    http::{StatusCode, Uri},
    Endpoint, Request, Route,
};

#[handler]
fn index() -> &'static str {
    "hello"
}

let app = Route::new().nest_no_strip("/foo", Route::new().at("/foo/bar", index));

let resp = app
    .call(
        Request::builder()
            .uri(Uri::from_static("/foo/bar"))
            .finish(),
    )
    .await
    .unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "hello");

Implementations

Create a new routing object.

Add an Endpoint to the specified path.

Panics

Panic when there are duplicates in the routing table.

Attempts to add an Endpoint to the specified path.

Nest a Endpoint to the specified path and strip the prefix.

Panics

Panic when there are duplicates in the routing table.

Attempts to nest a Endpoint to the specified path and strip the prefix.

Nest a Endpoint to the specified path, but do not strip the prefix.

Panics

Panic when there are duplicates in the routing table.

Attempts to nest a Endpoint to the specified path, but do not strip the prefix.

Trait Implementations

Returns the “default value” for a type. Read more

Represents the response of the endpoint.

Get the response to the request.

Get the response to the request and return a Response. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more

Attaches the current Context to this type, returning a WithContext wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more