use axum::Router as ax_router;
use axum::{handler::Handler, routing};
use std::sync::Arc;
#[derive(Clone)]
pub struct Router<S = ()> {
inner: ax_router<S>,
prefix: String,
layers: Vec<Arc<dyn Fn(ax_router<S>) -> ax_router<S> + Send + Sync>>,
}
impl<S> Router<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new() -> Self {
Self {
inner: ax_router::new(),
prefix: String::new(),
layers: Vec::new(),
}
}
pub fn from_axum(inner: ax_router<S>) -> Self {
Self {
inner,
prefix: String::new(),
layers: Vec::new(),
}
}
pub fn prefix(mut self, prefix: &str) -> Self {
self.prefix = prefix.to_string();
self
}
fn normalize_path(&self, path: &str) -> String {
if self.prefix.is_empty() {
return path.to_string();
}
format!(
"{}/{}",
self.prefix.trim_end_matches('/'),
path.trim_start_matches('/')
)
}
pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
let full_path = self.normalize_path(path);
self.inner = self.inner.route(&full_path, routing::get(handler));
self
}
pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
let full_path = self.normalize_path(path);
self.inner = self.inner.route(&full_path, routing::post(handler));
self
}
pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
let full_path = self.normalize_path(path);
self.inner = self.inner.route(&full_path, routing::put(handler));
self
}
pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
let full_path = self.normalize_path(path);
self.inner = self.inner.route(&full_path, routing::delete(handler));
self
}
pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
let full_path = self.normalize_path(path);
self.inner = self.inner.route(&full_path, routing::patch(handler));
self
}
pub fn group<F>(mut self, prefix: &str, f: F) -> Self
where
F: FnOnce(Router<S>) -> Router<S>,
{
let sub_router = Router::new();
let configured_sub = f(sub_router);
self.inner = self.inner.nest(prefix, configured_sub.into_axum());
self
}
pub fn merge(mut self, other: Router<S>) -> Self {
self.inner = self.inner.merge(other.into_axum());
self
}
pub fn nest(mut self, prefix: &str, other: Router<S>) -> Self {
self.inner = self.inner.nest(prefix, other.into_axum());
self
}
pub fn layer<L>(mut self, layer: L) -> Self
where
L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
{
self.layers
.push(Arc::new(move |router| router.layer(layer.clone())));
self
}
pub fn middleware<L>(self, layer: L) -> Self
where
L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
{
self.layer(layer)
}
pub fn route_layer<L>(mut self, layer: L) -> Self
where
L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
{
self.layers
.push(Arc::new(move |router| router.route_layer(layer.clone())));
self
}
pub fn route_middleware<L>(self, layer: L) -> Self
where
L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
{
self.route_layer(layer)
}
pub fn into_axum(self) -> ax_router<S> {
let mut inner = self.inner;
for layer_fn in self.layers {
inner = layer_fn(inner);
}
inner
}
}
impl<S> Default for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn default() -> Self {
Self::new()
}
}