#[non_exhaustive]pub struct Router { /* private fields */ }Expand description
A wrapper around Axum’s Router to provide clean integration with Rullst features
Implementations§
Source§impl Router
impl Router
Sourcepub fn route<T>(self, path: &str, method_router: T) -> Selfwhere
T: Into<MethodRouter>,
pub fn route<T>(self, path: &str, method_router: T) -> Selfwhere
T: Into<MethodRouter>,
Registers an HTTP route at the given path with the given method router.
method_router is typically the result of get(handler), post(handler), etc.
Sourcepub fn ws<H, T>(self, path: &str, handler: H) -> Self
pub fn ws<H, T>(self, path: &str, handler: H) -> Self
Registers a WebSocket upgrade route at the given path.
Internally maps to GET path since WebSocket upgrades happen over HTTP GET.
Sourcepub fn into_axum(self) -> AxumRouter
pub fn into_axum(self) -> AxumRouter
Unwraps the inner axum::Router. Use this to hand the router off to axum::serve
or to interoperate with Tower middleware that requires a raw Axum router.
Sourcepub fn as_axum(&self) -> &AxumRouter
pub fn as_axum(&self) -> &AxumRouter
Returns a reference to the inner axum::Router.
Sourcepub fn as_axum_mut(&mut self) -> &mut AxumRouter
pub fn as_axum_mut(&mut self) -> &mut AxumRouter
Returns a mutable reference to the inner axum::Router.
Sourcepub fn fallback<H, T>(self, handler: H) -> Self
pub fn fallback<H, T>(self, handler: H) -> Self
Adds a fallback handler for requests that do not match any route.
Sourcepub fn fallback_service<S>(self, service: S) -> Self
pub fn fallback_service<S>(self, service: S) -> Self
Adds a fallback Tower service for requests that do not match any route.
Sourcepub fn nest(self, path: &str, router: Router) -> Self
pub fn nest(self, path: &str, router: Router) -> Self
Nests another Rullst Router under a path prefix.
All routes defined in router will be reachable under path/....
Sourcepub fn nest_axum(self, path: &str, router: AxumRouter) -> Self
pub fn nest_axum(self, path: &str, router: AxumRouter) -> Self
Nests a raw axum::Router under a path prefix.
Useful for integrating third-party Axum routers (e.g. from utoipa, aide) without wrapping.
Sourcepub fn merge_axum(self, router: AxumRouter) -> Self
pub fn merge_axum(self, router: AxumRouter) -> Self
Merges a raw axum::Router into this router at the root.
Equivalent to Router::merge on the inner axum::Router. This is the
recommended way to compose a fully-built Axum router (for example, one
produced by utoipa_axum::OpenApiRouter::split_for_parts) into a Rullst
Router, since axum 0.8+ no longer allows nesting at the root path.
Methods from Deref<Target = AxumRouter>§
Sourcepub fn has_routes(&self) -> bool
pub fn has_routes(&self) -> bool
True if the router currently has at least one route added.
Sourcepub fn as_service<B>(&mut self) -> RouterAsService<'_, B, S>
pub fn as_service<B>(&mut self) -> RouterAsService<'_, B, S>
Convert the router into a borrowed Service with a fixed request body type, to aid type
inference.
In some cases when calling methods from tower::ServiceExt on a Router you might get
type inference errors along the lines of
let response = router.ready().await?.call(request).await?;
^^^^^ cannot infer type for type parameter `B`This happens because Router implements Service with impl<B> Service<Request<B>> for Router<()>.
For example:
use axum::{
Router,
routing::get,
http::Request,
body::Body,
};
use tower::{Service, ServiceExt};
let mut router = Router::new().route("/", get(|| async {}));
let request = Request::new(Body::empty());
let response = router.ready().await?.call(request).await?;Calling Router::as_service fixes that:
use axum::{
Router,
routing::get,
http::Request,
body::Body,
};
use tower::{Service, ServiceExt};
let mut router = Router::new().route("/", get(|| async {}));
let request = Request::new(Body::empty());
let response = router.as_service().ready().await?.call(request).await?;This is mainly used when calling Router in tests. It shouldn’t be necessary when running
the Router normally via Router::into_make_service.