pub struct Router<S = ()> { /* private fields */ }Expand description
A drop-in replacement for axum::Router that adds OpenAPI documentation support.
This Router works seamlessly with handlers decorated with #[rovo] and provides
a fluent API for building documented APIs with Swagger UI integration.
§Example
use rovo::{Router, rovo, routing::get, aide::axum::IntoApiResponse};
use rovo::aide::openapi::OpenApi;
use rovo::response::Json;
#[rovo]
async fn documented_handler() -> impl IntoApiResponse {
Json(())
}
let mut api = OpenApi::default();
api.info.title = "My API".to_string();
let app = Router::new()
.route("/documented", get(documented_handler))
.with_oas(api);Implementations§
Source§impl<S> Router<S>
impl<S> Router<S>
Sourcepub fn route<M>(self, path: &str, method_router: M) -> Selfwhere
M: Into<ApiMethodRouter<S>>,
pub fn route<M>(self, path: &str, method_router: M) -> Selfwhere
M: Into<ApiMethodRouter<S>>,
Add a route to the router
Sourcepub fn with_oas(self, api: OpenApi) -> Self
pub fn with_oas(self, api: OpenApi) -> Self
Configure OpenAPI spec with default routes (/api.json and /api.yaml)
This automatically sets up endpoints for both JSON and YAML formats.
§Memory Efficiency
The OpenAPI spec is serialized to JSON and YAML at startup, then the
original struct is dropped to minimize memory usage. Only the pre-serialized
strings are kept in memory.
If you need runtime access to the OpenApi struct (e.g., in handlers via
Extension<Arc<OpenApi>>), use finish_api_with_extension instead.
Sourcepub fn with_oas_route(self, api: OpenApi, route: impl Into<String>) -> Self
pub fn with_oas_route(self, api: OpenApi, route: impl Into<String>) -> Self
Configure OpenAPI spec with custom base route
This sets up endpoints with the specified base route. For example, “/openapi” creates:
- /openapi.json
- /openapi.yaml
§Memory Efficiency
The OpenAPI spec is serialized to JSON and YAML at startup, then the
original struct is dropped to minimize memory usage. Only the pre-serialized
strings are kept in memory.
If you need runtime access to the OpenApi struct (e.g., in handlers via
Extension<Arc<OpenApi>>), use finish_api_with_extension instead.
Sourcepub fn with_state(self, state: S) -> Router
pub fn with_state(self, state: S) -> Router
Add state to the router and finalize the API
Sourcepub fn finish_api(self, api: &mut OpenApi) -> Router<S>
pub fn finish_api(self, api: &mut OpenApi) -> Router<S>
Finish building the API and return an axum Router for further configuration
Sourcepub fn finish_api_with_extension(self, api: OpenApi) -> Router<S>
pub fn finish_api_with_extension(self, api: OpenApi) -> Router<S>
Finish the API with OpenAPI spec embedded via Extension layer
The spec is wrapped in Arc<OpenApi> for efficient sharing. Use this method
when you need runtime access to the OpenAPI spec in your handlers.
§Example
use std::sync::Arc;
use axum::Extension;
use aide::openapi::OpenApi;
async fn handler(Extension(api): Extension<Arc<OpenApi>>) {
// Access the OpenAPI spec at runtime
println!("API title: {}", api.info.title);
}§Note
This keeps the full OpenApi struct in memory. For large APIs where you don’t
need runtime access, prefer using with_oas which only keeps
pre-serialized strings in memory.
Sourcepub fn into_inner(self) -> AideApiRouter<S>
pub fn into_inner(self) -> AideApiRouter<S>
Convert into the underlying aide ApiRouter