pub use rovo_macros::rovo;
pub use aide;
use ::axum::Extension;
use aide::axum::ApiRouter as AideApiRouter;
pub struct Router<S = ()> {
inner: AideApiRouter<S>,
}
impl<S> Router<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new() -> Self {
Self {
inner: AideApiRouter::new(),
}
}
pub fn route<M>(mut self, path: &str, method_router: M) -> Self
where
M: Into<aide::axum::routing::ApiMethodRouter<S>>,
{
self.inner = self.inner.api_route(path, method_router.into());
self
}
pub fn nest(mut self, path: &str, router: Router<S>) -> Self {
self.inner = self.inner.nest(path, router.inner);
self
}
#[cfg(feature = "swagger")]
pub fn with_swagger(mut self, swagger_path: &str, api_json_path: &str) -> Self
where
S: Clone + Send + Sync + 'static,
{
self.inner = self.inner.route(
swagger_path,
aide::swagger::Swagger::new(api_json_path).axum_route(),
);
self
}
#[cfg(feature = "redoc")]
pub fn with_redoc(mut self, redoc_path: &str, api_json_path: &str) -> Self
where
S: Clone + Send + Sync + 'static,
{
self.inner = self.inner.route(
redoc_path,
aide::redoc::Redoc::new(api_json_path).axum_route(),
);
self
}
#[cfg(feature = "scalar")]
pub fn with_scalar(mut self, scalar_path: &str, api_json_path: &str) -> Self
where
S: Clone + Send + Sync + 'static,
{
self.inner = self.inner.route(
scalar_path,
aide::scalar::Scalar::new(api_json_path).axum_route(),
);
self
}
pub fn with_api_json<H, T>(mut self, path: &str, handler: H) -> Self
where
H: ::axum::handler::Handler<T, S>,
S: Clone + Send + Sync + 'static,
T: 'static,
{
self.inner = self.inner.route(path, ::axum::routing::get(handler));
self
}
pub fn with_state<S2>(self, state: S) -> Router<S2>
where
S2: Clone + Send + Sync + 'static,
{
Router {
inner: self.inner.with_state(state),
}
}
pub fn finish_api(self, api: &mut aide::openapi::OpenApi) -> ::axum::Router<S> {
self.inner.finish_api(api)
}
pub fn finish_api_with_extension(self, api: aide::openapi::OpenApi) -> ::axum::Router<S>
where
S: Clone + Send + Sync + 'static,
{
let mut api_mut = api;
self.inner
.finish_api(&mut api_mut)
.layer(Extension(api_mut))
}
pub fn into_inner(self) -> AideApiRouter<S> {
self.inner
}
}
impl<S> Default for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn default() -> Self {
Self::new()
}
}
pub trait IntoApiMethodRouter<S = ()> {
fn into_get_route(self) -> aide::axum::routing::ApiMethodRouter<S>;
fn into_post_route(self) -> aide::axum::routing::ApiMethodRouter<S>;
fn into_patch_route(self) -> aide::axum::routing::ApiMethodRouter<S>;
fn into_delete_route(self) -> aide::axum::routing::ApiMethodRouter<S>;
fn into_put_route(self) -> aide::axum::routing::ApiMethodRouter<S>;
}
pub struct ApiMethodRouter<S = ()> {
inner: aide::axum::routing::ApiMethodRouter<S>,
}
impl<S> ApiMethodRouter<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new(inner: aide::axum::routing::ApiMethodRouter<S>) -> Self {
Self { inner }
}
pub fn post<H>(self, handler: H) -> Self
where
H: IntoApiMethodRouter<S>,
{
Self {
inner: self.inner.merge(handler.into_post_route()),
}
}
pub fn get<H>(self, handler: H) -> Self
where
H: IntoApiMethodRouter<S>,
{
Self {
inner: self.inner.merge(handler.into_get_route()),
}
}
pub fn patch<H>(self, handler: H) -> Self
where
H: IntoApiMethodRouter<S>,
{
Self {
inner: self.inner.merge(handler.into_patch_route()),
}
}
pub fn delete<H>(self, handler: H) -> Self
where
H: IntoApiMethodRouter<S>,
{
Self {
inner: self.inner.merge(handler.into_delete_route()),
}
}
pub fn put<H>(self, handler: H) -> Self
where
H: IntoApiMethodRouter<S>,
{
Self {
inner: self.inner.merge(handler.into_put_route()),
}
}
}
impl<S> From<ApiMethodRouter<S>> for aide::axum::routing::ApiMethodRouter<S> {
fn from(router: ApiMethodRouter<S>) -> Self {
router.inner
}
}
pub mod routing {
use super::*;
pub fn get<S, H>(handler: H) -> ApiMethodRouter<S>
where
H: IntoApiMethodRouter<S>,
S: Clone + Send + Sync + 'static,
{
ApiMethodRouter::new(handler.into_get_route())
}
pub fn post<S, H>(handler: H) -> ApiMethodRouter<S>
where
H: IntoApiMethodRouter<S>,
S: Clone + Send + Sync + 'static,
{
ApiMethodRouter::new(handler.into_post_route())
}
pub fn patch<S, H>(handler: H) -> ApiMethodRouter<S>
where
H: IntoApiMethodRouter<S>,
S: Clone + Send + Sync + 'static,
{
ApiMethodRouter::new(handler.into_patch_route())
}
pub fn delete<S, H>(handler: H) -> ApiMethodRouter<S>
where
H: IntoApiMethodRouter<S>,
S: Clone + Send + Sync + 'static,
{
ApiMethodRouter::new(handler.into_delete_route())
}
pub fn put<S, H>(handler: H) -> ApiMethodRouter<S>
where
H: IntoApiMethodRouter<S>,
S: Clone + Send + Sync + 'static,
{
ApiMethodRouter::new(handler.into_put_route())
}
}
pub mod axum {
pub use aide::axum::{ApiRouter, IntoApiResponse};
}