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},
test::TestClient,
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));
let cli = TestClient::new(app);
// /a/b
cli.get("/a/b").send().await.assert_status_is_ok();
// /b/:group/:name
cli.get("/b/foo/bar").send().await.assert_status_is_ok();
// /c/*path
cli.get("/c/d/e").send().await.assert_status_is_ok();
// /d/<\\d>
cli.get("/d/123").send().await.assert_status_is_ok();
// /e/:name<\\d>
cli.get("/e/123").send().await.assert_status_is_ok();
Nested
use poem::{
handler,
http::{StatusCode, Uri},
test::TestClient,
Endpoint, Request, Route,
};
#[handler]
fn index() -> &'static str {
"hello"
}
let app = Route::new().nest("/foo", Route::new().at("/bar", index));
let cli = TestClient::new(app);
let resp = cli.get("/foo/bar").send().await;
resp.assert_status_is_ok();
resp.assert_text("hello").await;
Nested no strip
use poem::{
handler,
http::{StatusCode, Uri},
test::TestClient,
Endpoint, Request, Route,
};
#[handler]
fn index() -> &'static str {
"hello"
}
let app = Route::new().nest_no_strip("/foo", Route::new().at("/foo/bar", index));
let cli = TestClient::new(app);
let resp = cli.get("/foo/bar").send().await;
resp.assert_status_is_ok();
resp.assert_text("hello").await;
Implementations§
source§impl Route
impl Route
sourcepub fn at<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn at<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere E: IntoEndpoint, E::Endpoint: 'static,
sourcepub fn try_at<E>(self, path: impl AsRef<str>, ep: E) -> Result<Self, RouteError>where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn try_at<E>(self, path: impl AsRef<str>, ep: E) -> Result<Self, RouteError>where E: IntoEndpoint, E::Endpoint: 'static,
Attempts to add an Endpoint to the specified path.
sourcepub fn just_at<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn just_at<E>(self, ep: E) -> Selfwhere E: IntoEndpoint, E::Endpoint: 'static,
sourcepub fn nest<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn nest<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere E: IntoEndpoint, E::Endpoint: 'static,
Nest a Endpoint
to the specified path and strip the prefix.
Panics
Panic when there are duplicates in the routing table.
sourcepub fn try_nest<E>(
self,
path: impl AsRef<str>,
ep: E
) -> Result<Self, RouteError>where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn try_nest<E>( self, path: impl AsRef<str>, ep: E ) -> Result<Self, RouteError>where E: IntoEndpoint, E::Endpoint: 'static,
Attempts to nest a Endpoint
to the specified path and strip the
prefix.
sourcepub fn nest_no_strip<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn nest_no_strip<E>(self, path: impl AsRef<str>, ep: E) -> Selfwhere E: IntoEndpoint, E::Endpoint: 'static,
Nest a Endpoint
to the specified path, but do not strip the prefix.
Panics
Panic when there are duplicates in the routing table.
sourcepub fn try_nest_no_strip<E>(
self,
path: impl AsRef<str>,
ep: E
) -> Result<Self, RouteError>where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn try_nest_no_strip<E>( self, path: impl AsRef<str>, ep: E ) -> Result<Self, RouteError>where E: IntoEndpoint, E::Endpoint: 'static,
Attempts to nest a Endpoint
to the specified path, but do not strip
the prefix.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Route
impl Send for Route
impl Sync for Route
impl Unpin for Route
impl !UnwindSafe for Route
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> EndpointExt for Twhere
T: IntoEndpoint,
impl<T> EndpointExt for Twhere T: IntoEndpoint,
source§fn boxed<'a>(self) -> BoxEndpoint<'a, <Self::Endpoint as Endpoint>::Output>where
Self: Sized + 'a,
fn boxed<'a>(self) -> BoxEndpoint<'a, <Self::Endpoint as Endpoint>::Output>where Self: Sized + 'a,
Wrap the endpoint in a Box.
source§fn with<T>(self, middleware: T) -> T::Outputwhere
T: Middleware<Self::Endpoint>,
Self: Sized,
fn with<T>(self, middleware: T) -> T::Outputwhere T: Middleware<Self::Endpoint>, Self: Sized,
Use middleware to transform this endpoint. Read more
source§fn with_if<T>(
self,
enable: bool,
middleware: T
) -> EitherEndpoint<Self, T::Output>where
T: Middleware<Self::Endpoint>,
Self: Sized,
fn with_if<T>( self, enable: bool, middleware: T ) -> EitherEndpoint<Self, T::Output>where T: Middleware<Self::Endpoint>, Self: Sized,
source§fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>where
T: Clone + Send + Sync + 'static,
Self: Sized,
fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>where T: Clone + Send + Sync + 'static, Self: Sized,
Attach a state data to the endpoint, similar to
with(AddData(T))
. Read moresource§fn data_opt<T>(
self,
data: Option<T>
) -> EitherEndpoint<AddDataEndpoint<Self::Endpoint, T>, Self>where
T: Clone + Send + Sync + 'static,
Self: Sized,
fn data_opt<T>( self, data: Option<T> ) -> EitherEndpoint<AddDataEndpoint<Self::Endpoint, T>, Self>where T: Clone + Send + Sync + 'static, Self: Sized,
if
data
is Some(T)
then attach the value to the endpoint.source§fn before<F, Fut>(self, f: F) -> Before<Self, F>where
F: Fn(Request) -> Fut + Send + Sync,
Fut: Future<Output = Result<Request>> + Send,
Self: Sized,
fn before<F, Fut>(self, f: F) -> Before<Self, F>where F: Fn(Request) -> Fut + Send + Sync, Fut: Future<Output = Result<Request>> + Send, Self: Sized,
Maps the request of this endpoint. Read more
source§fn after<F, Fut, T>(self, f: F) -> After<Self::Endpoint, F>where
F: Fn(Result<<Self::Endpoint as Endpoint>::Output>) -> Fut + Send + Sync,
Fut: Future<Output = Result<T>> + Send,
T: IntoResponse,
Self: Sized,
fn after<F, Fut, T>(self, f: F) -> After<Self::Endpoint, F>where F: Fn(Result<<Self::Endpoint as Endpoint>::Output>) -> Fut + Send + Sync, Fut: Future<Output = Result<T>> + Send, T: IntoResponse, Self: Sized,
Maps the output of this endpoint. Read more
source§fn around<F, Fut, R>(self, f: F) -> Around<Self::Endpoint, F>where
F: Fn(Arc<Self::Endpoint>, Request) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R>> + Send + 'static,
R: IntoResponse,
Self: Sized,
fn around<F, Fut, R>(self, f: F) -> Around<Self::Endpoint, F>where F: Fn(Arc<Self::Endpoint>, Request) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R>> + Send + 'static, R: IntoResponse, Self: Sized,
Maps the request and response of this endpoint. Read more
source§fn map_to_response(self) -> MapToResponse<Self::Endpoint>where
Self: Sized,
fn map_to_response(self) -> MapToResponse<Self::Endpoint>where Self: Sized,
source§fn to_response(self) -> ToResponse<Self::Endpoint>where
Self: Sized,
fn to_response(self) -> ToResponse<Self::Endpoint>where Self: Sized,
source§fn map<F, Fut, R, R2>(self, f: F) -> Map<Self::Endpoint, F>where
F: Fn(R) -> Fut + Send + Sync,
Fut: Future<Output = R2> + Send,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = R> + Sized,
fn map<F, Fut, R, R2>(self, f: F) -> Map<Self::Endpoint, F>where F: Fn(R) -> Fut + Send + Sync, Fut: Future<Output = R2> + Send, R: IntoResponse, R2: IntoResponse, Self: Sized, Self::Endpoint: Endpoint<Output = R> + Sized,
Maps the response of this endpoint. Read more
source§fn and_then<F, Fut, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F>where
F: Fn(R) -> Fut + Send + Sync,
Fut: Future<Output = Result<R2>> + Send,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = R> + Sized,
fn and_then<F, Fut, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F>where F: Fn(R) -> Fut + Send + Sync, Fut: Future<Output = Result<R2>> + Send, R: IntoResponse, R2: IntoResponse, Self: Sized, Self::Endpoint: Endpoint<Output = R> + Sized,
source§fn catch_all_error<F, Fut, R>(self, f: F) -> CatchAllError<Self, F, R>where
F: Fn(Error) -> Fut + Send + Sync,
Fut: Future<Output = R> + Send,
R: IntoResponse + Send,
Self: Sized + Sync,
fn catch_all_error<F, Fut, R>(self, f: F) -> CatchAllError<Self, F, R>where F: Fn(Error) -> Fut + Send + Sync, Fut: Future<Output = R> + Send, R: IntoResponse + Send, Self: Sized + Sync,
Catch all errors and convert it into a response. Read more
source§fn catch_error<F, Fut, R, ErrType>(
self,
f: F
) -> CatchError<Self, F, R, ErrType>where
F: Fn(ErrType) -> Fut + Send + Sync,
Fut: Future<Output = R> + Send,
R: IntoResponse + Send + Sync,
ErrType: Error + Send + Sync + 'static,
Self: Sized,
fn catch_error<F, Fut, R, ErrType>( self, f: F ) -> CatchError<Self, F, R, ErrType>where F: Fn(ErrType) -> Fut + Send + Sync, Fut: Future<Output = R> + Send, R: IntoResponse + Send + Sync, ErrType: Error + Send + Sync + 'static, Self: Sized,
Catch the specified type of error and convert it into a response. Read more
source§fn inspect_all_err<F>(self, f: F) -> InspectAllError<Self, F>where
F: Fn(&Error) + Send + Sync,
Self: Sized,
fn inspect_all_err<F>(self, f: F) -> InspectAllError<Self, F>where F: Fn(&Error) + Send + Sync, Self: Sized,
Does something with each error. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEndpoint for Twhere
T: Endpoint,
impl<T> IntoEndpoint for Twhere T: Endpoint,
source§fn into_endpoint(self) -> <T as IntoEndpoint>::Endpoint
fn into_endpoint(self) -> <T as IntoEndpoint>::Endpoint
Converts this object into endpoint.
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: HttpBody + Send + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<Body>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where ResBody: HttpBody + Send + 'static, ResBody::Data: Into<Bytes> + Send + 'static, ResBody::Error: StdError + Send + Sync + 'static, Err: Into<Error>, Self: Service<Request<Body>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static, Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.