Router

Struct Router 

Source
pub struct Router<S = ()> { /* private fields */ }
Expand description

Drop-in replacement for axum::Router, which supports OpenAPI operations.

This replacement cannot be used as Service instead require explicit convertion of this type to axum::Router. This is done to ensure that OpenAPI specification generated and mounted.

Implementations§

Source§

impl<S> Router<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn new() -> Self

Create new router.

Source

pub fn route<R>(self, path: &str, method_router: R) -> Self
where R: Into<MethodRouter<S>>,

Add another route to the router.

This method works for both MethodRouter and one from axum.

For details see axum::Router::route.

§Example
#[openapi]
async fn handler() {}

let app = Router::new().route("/", get(openapi_handler!(handler)));
Source

pub fn route_service<Svc>(self, path: &str, service: Svc) -> Self
where Svc: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static, Svc::Response: IntoResponse, Svc::Future: Send + 'static,

Add another route to the router that calls a Service.

For details see axum::Router::route_service.

§Example

TODO

Source

pub fn nest<R>(self, path: &str, router: R) -> Self
where R: Into<Router<S>>,

Nest a router at some path.

This method works for both Router and one from axum.

For details see axum::Router::nest.

§Example
#[openapi]
async fn handler() {}
let handler_router = Router::new().route("/", get(openapi_handler!(handler)));
let app = Router::new().nest("/handle", handler_router);
Source

pub fn nest_service<Svc>(self, path: &str, svc: Svc) -> Self
where Svc: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static, Svc::Response: IntoResponse, Svc::Future: Send + 'static,

Like nest, but accepts an arbitrary Service.

For details see axum::Router::nest_service.

Source

pub fn merge<R>(self, other: R) -> Self
where R: Into<Router<S>>,

Merge two routers into one.

This method works for both Router and one from axum.

For details see axum::Router::merge.

§Example
#[openapi]
async fn handler() {}
let handler_router = Router::new().route("/another_handler", get(openapi_handler!(handler)));
let app = Router::new().route("/", get(openapi_handler!(handler))).merge(handler_router);
Source

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

Apply a tower::Layer to the router.

For details see axum::Router::layer.

Source

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

Apply a tower::Layer to the router that will only run if the request matches a route.

For details see axum::Router::route_layer.

Source

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

Add a fallback Service to the router.

For details see axum::Router::fallback_service.

§Note

This method doesn’t add anything to OpenaAPI spec.

Source

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

Add a fallback Service to the router.

For details see axum::Router::fallback_service.

§Note

This method doesn’t add anything to OpenaAPI spec.

Source

pub fn with_state<S2>(self, state: S) -> Router<S2>

Provide the state for the router.

For details see axum::Router::with_state.

Source

pub fn into_parts(self) -> (AxumRouter<S>, RoutesOperations)

Separate router into axum::Router and list of operations.

Source

pub fn axum_router(&self) -> AxumRouter<S>

Get inner axum::Router.

Source

pub fn routes_operations(&self) -> RoutesOperations

Get list of operations.

Source

pub fn generate_openapi_builder(&self) -> OpenApiBuilder

Generate OpenApiBuilder from current router.

Generated builder will be based on current builder template, have all routes and types, present in this router.

If template was not set, then OpenApiBuilder::default() is used.

Source

pub fn set_openapi_builder_template( &mut self, builder: OpenApiBuilder, ) -> &mut Self

Set OpenApiBuilder template for this router.

By default OpenApiBuilder::default() is used.

Source

pub fn update_openapi_builder_template<F>(&mut self, f: F) -> &mut Self
where F: FnOnce(&mut OpenApiBuilder),

Update OpenApiBuilder template of this router.

By default OpenApiBuilder::default() is used.

Source

pub fn openapi_builder_template_mut(&mut self) -> &mut OpenApiBuilder

Get mutable reference to OpenApiBuilder template of this router.

By default OpenApiBuilder::default() is set.

Source

pub fn finish_openapi<'a>( self, serve_path: impl Into<Option<&'a str>>, title: impl Into<String>, version: impl Into<String>, ) -> Result<AxumRouter<S>, Error>

Generate OpenAPI specification, mount it to inner router and return inner axum::Router.

Specification is based on OpenApiBuilder template, if one was set previously. If template was not set, then OpenApiBuilder::default() is used.

Note that passed title and version will override same values in OpenAPI builder template.

By default specification served at DEFAULT_OPENAPI_PATH (/openapi).

§Example
#[openapi]
async fn handler() {}

let app = Router::new().route("/", get(openapi_handler!(handler)));
let app = app.finish_openapi("/openapi", "Demo", "1.0.0").expect("ok");

Trait Implementations§

Source§

impl<S> Clone for Router<S>
where S: Clone + Send + Sync + 'static,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Debug for Router<S>
where S: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S> Default for Router<S>
where S: Clone + Send + Sync + 'static,

Source§

fn default() -> Self

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

impl<S> From<Router<S>> for Router<S>

Source§

fn from(value: AxumRouter<S>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<S> Freeze for Router<S>

§

impl<S = ()> !RefUnwindSafe for Router<S>

§

impl<S = ()> !Send for Router<S>

§

impl<S = ()> !Sync for Router<S>

§

impl<S> Unpin for Router<S>

§

impl<S = ()> !UnwindSafe for Router<S>

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

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