logo
pub struct RouteDomain { /* private fields */ }
Expand description

Routing object for HOST header

Errors

Example

use poem::{
    endpoint::make_sync,
    handler,
    http::header,
    test::{TestClient, TestRequestBuilder},
    Endpoint, Request, RouteDomain,
};

let app = RouteDomain::new()
    .at("example.com", make_sync(|_| "1"))
    .at("www.+.com", make_sync(|_| "2"))
    .at("*.example.com", make_sync(|_| "3"))
    .at("*", make_sync(|_| "4"));

async fn check(app: impl Endpoint, domain: Option<&str>, res: &str) {
    let cli = TestClient::new(app);
    let mut req = cli.get("/");
    if let Some(domain) = domain {
        req = req.header(header::HOST, domain);
    }
    let resp = req.send().await;
    resp.assert_status_is_ok();
    resp.assert_text(res).await;
}

check(&app, Some("example.com"), "1").await;
check(&app, Some("www.abc.com"), "2").await;
check(&app, Some("a.b.example.com"), "3").await;
check(&app, Some("rust-lang.org"), "4").await;
check(&app, None, "4").await;

Implementations

Create a RouteDomain object.

Add an Endpoint to the specified domain pattern.

Panics

Panic when there are duplicates in the routing table.

Attempts to add an Endpoint to the specified domain pattern.

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

Returns the argument unchanged.

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

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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