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

Routing object for HOST header

Errors

Example

use poem::{endpoint::make_sync, handler, http::header, 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"));

fn make_request(host: &str) -> Request {
    Request::builder().header(header::HOST, host).finish()
}

async fn do_request(app: &RouteDomain, req: Request) -> String {
    app.call(req)
        .await
        .unwrap()
        .into_body()
        .into_string()
        .await
        .unwrap()
}

assert_eq!(do_request(&app, make_request("example.com")).await, "1");
assert_eq!(do_request(&app, make_request("www.abc.com")).await, "2");
assert_eq!(do_request(&app, make_request("a.b.example.com")).await, "3");
assert_eq!(do_request(&app, make_request("rust-lang.org")).await, "4");
assert_eq!(do_request(&app, Request::default()).await, "4");

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

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