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>
impl<S> Router<S>
Sourcepub fn route<R>(self, path: &str, method_router: R) -> Selfwhere
R: Into<MethodRouter<S>>,
pub fn route<R>(self, path: &str, method_router: R) -> Selfwhere
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)));
Sourcepub fn route_service<Svc>(self, path: &str, service: Svc) -> Selfwhere
Svc: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
Svc::Response: IntoResponse,
Svc::Future: Send + 'static,
pub fn route_service<Svc>(self, path: &str, service: Svc) -> Selfwhere
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
Sourcepub fn nest<R>(self, path: &str, router: R) -> Self
pub fn nest<R>(self, path: &str, router: R) -> Self
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);
Sourcepub fn nest_service<Svc>(self, path: &str, svc: Svc) -> Selfwhere
Svc: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
Svc::Response: IntoResponse,
Svc::Future: Send + 'static,
pub fn nest_service<Svc>(self, path: &str, svc: Svc) -> Selfwhere
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
.
Sourcepub fn merge<R>(self, other: R) -> Self
pub fn merge<R>(self, other: R) -> Self
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);
Sourcepub 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,
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
.
Sourcepub fn route_layer<L>(self, layer: L) -> Selfwhere
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,
pub fn route_layer<L>(self, layer: L) -> Selfwhere
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
.
Sourcepub fn fallback<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn fallback<H, T>(self, handler: H) -> Selfwhere
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.
Sourcepub fn fallback_service<Svc>(self, svc: Svc) -> Selfwhere
Svc: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
Svc::Response: IntoResponse,
Svc::Future: Send + 'static,
pub fn fallback_service<Svc>(self, svc: Svc) -> Selfwhere
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.
Sourcepub fn with_state<S2>(self, state: S) -> Router<S2>
pub fn with_state<S2>(self, state: S) -> Router<S2>
Provide the state for the router.
For details see axum::Router::with_state
.
Sourcepub fn into_parts(self) -> (AxumRouter<S>, RoutesOperations)
pub fn into_parts(self) -> (AxumRouter<S>, RoutesOperations)
Separate router into axum::Router
and list of operations.
Sourcepub fn axum_router(&self) -> AxumRouter<S>
pub fn axum_router(&self) -> AxumRouter<S>
Get inner axum::Router
.
Sourcepub fn routes_operations(&self) -> RoutesOperations
pub fn routes_operations(&self) -> RoutesOperations
Get list of operations.
Sourcepub fn generate_openapi_builder(&self) -> OpenApiBuilder
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.
Sourcepub fn set_openapi_builder_template(
&mut self,
builder: OpenApiBuilder,
) -> &mut Self
pub fn set_openapi_builder_template( &mut self, builder: OpenApiBuilder, ) -> &mut Self
Set OpenApiBuilder
template for this router.
By default OpenApiBuilder::default()
is used.
Sourcepub fn update_openapi_builder_template<F>(&mut self, f: F) -> &mut Selfwhere
F: FnOnce(&mut OpenApiBuilder),
pub fn update_openapi_builder_template<F>(&mut self, f: F) -> &mut Selfwhere
F: FnOnce(&mut OpenApiBuilder),
Update OpenApiBuilder
template of this router.
By default OpenApiBuilder::default()
is used.
Sourcepub fn openapi_builder_template_mut(&mut self) -> &mut OpenApiBuilder
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.
Sourcepub fn finish_openapi<'a>(
self,
serve_path: impl Into<Option<&'a str>>,
title: impl Into<String>,
version: impl Into<String>,
) -> Result<AxumRouter<S>, Error>
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");