Skip to main content

Router

Struct Router 

Source
#[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

Source

pub fn new() -> Self

Creates a new empty Router with no routes configured.

Source

pub fn route<T>(self, path: &str, method_router: T) -> Self
where 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.

Source

pub fn ws<H, T>(self, path: &str, handler: H) -> Self
where T: 'static, H: Handler<T, ()>,

Registers a WebSocket upgrade route at the given path. Internally maps to GET path since WebSocket upgrades happen over HTTP GET.

Source

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.

Source

pub fn as_axum(&self) -> &AxumRouter

Returns a reference to the inner axum::Router.

Source

pub fn as_axum_mut(&mut self) -> &mut AxumRouter

Returns a mutable reference to the inner axum::Router.

Source

pub fn fallback<H, T>(self, handler: H) -> Self
where H: Handler<T, ()>, T: 'static,

Adds a fallback handler for requests that do not match any route.

Source

pub fn fallback_service<S>(self, service: S) -> Self
where S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + Sync + 'static, S::Future: Send + 'static,

Adds a fallback Tower service for requests that do not match any route.

Source

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/....

Source

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.

Source

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.

Source

pub fn layer<L>(self, layer: L) -> Self
where L: Layer<Route> + Clone + Send + Sync + 'static, L::Service: Service<Request, Response = Response, Error = Infallible> + Clone + Send + Sync + 'static, <L::Service as Service<Request>>::Future: Send + 'static,

Applies a Tower middleware layer to all routes in this router. The layer is cloned once per request and must be Send + Sync + 'static.

Methods from Deref<Target = AxumRouter>§

Source

pub fn has_routes(&self) -> bool

True if the router currently has at least one route added.

Source

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.

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Deref for Router

Source§

type Target = Router

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Router

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<Router> for Router

Source§

fn from(inner: AxumRouter) -> Self

Converts to this type from the input type.
Source§

impl From<Router> for Router

Source§

fn from(router: Router) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more