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
sourceimpl Route
impl Route
sourcepub fn at<E>(self, path: impl AsRef<str>, ep: E) -> Self where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn at<E>(self, path: impl AsRef<str>, ep: E) -> Self where
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 nest<E>(self, path: impl AsRef<str>, ep: E) -> Self where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn nest<E>(self, path: impl AsRef<str>, ep: E) -> Self where
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) -> Self where
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn nest_no_strip<E>(self, path: impl AsRef<str>, ep: E) -> Self where
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more