use axum::Router as ax_router;
use axum::{handler::Handler, routing};
use std::sync::{Arc, Mutex};
pub use axum::middleware::{from_fn, from_fn_with_state, Next};
#[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>>,
routes_doc: Arc<Mutex<Vec<crate::core::openapi::RouteDoc>>>,
}
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(),
routes_doc: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn from_axum(inner: ax_router<S>) -> Self {
Self {
inner,
prefix: String::new(),
layers: Vec::new(),
routes_doc: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn prefix(mut self, prefix: &str) -> Self {
self.prefix = prefix.to_string();
self
}
fn normalize_path(&self, path: &str) -> String {
let combined = if self.prefix.is_empty() {
path.to_string()
} else {
format!(
"{}/{}",
self.prefix.trim_end_matches('/'),
path.trim_start_matches('/')
)
};
if combined.contains(':') {
combined
.split('/')
.map(|segment| {
if let Some(param) = segment.strip_prefix(':') {
format!("{{{}}}", param)
} else {
segment.to_string()
}
})
.collect::<Vec<_>>()
.join("/")
} else {
combined
}
}
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);
if let Ok(mut docs) = self.routes_doc.lock() {
docs.push(crate::core::openapi::RouteDoc::new("GET", &full_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);
if let Ok(mut docs) = self.routes_doc.lock() {
docs.push(crate::core::openapi::RouteDoc::new("POST", &full_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);
if let Ok(mut docs) = self.routes_doc.lock() {
docs.push(crate::core::openapi::RouteDoc::new("PUT", &full_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);
if let Ok(mut docs) = self.routes_doc.lock() {
docs.push(crate::core::openapi::RouteDoc::new("DELETE", &full_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);
if let Ok(mut docs) = self.routes_doc.lock() {
docs.push(crate::core::openapi::RouteDoc::new("PATCH", &full_path));
}
self.inner = self.inner.route(&full_path, routing::patch(handler));
self
}
pub fn summary(self, summary: impl Into<String>) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
last.summary = Some(summary.into());
}
}
self
}
pub fn description(self, description: impl Into<String>) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
last.description = Some(description.into());
}
}
self
}
pub fn tag(self, tag: impl Into<String>) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
last.tags.push(tag.into());
}
}
self
}
pub fn tags(self, tags: &[&str]) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
for t in tags {
last.tags.push(t.to_string());
}
}
}
self
}
pub fn deprecated(self) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
last.deprecated = true;
}
}
self
}
pub fn operation_id(self, id: impl Into<String>) -> Self {
if let Ok(mut docs) = self.routes_doc.lock() {
if let Some(last) = docs.last_mut() {
last.operation_id = Some(id.into());
}
}
self
}
pub fn fallback<H, T>(mut self, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
self.inner = self.inner.fallback(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);
if let Ok(sub_docs) = configured_sub.routes_doc.lock() {
if let Ok(mut my_docs) = self.routes_doc.lock() {
for mut doc in sub_docs.clone() {
doc.path = format!(
"{}/{}",
prefix.trim_end_matches('/'),
doc.path.trim_start_matches('/')
);
my_docs.push(doc);
}
}
}
self.inner = self.inner.nest(prefix, configured_sub.into_axum());
self
}
pub fn merge(mut self, other: Router<S>) -> Self {
if let Ok(other_docs) = other.routes_doc.lock() {
if let Ok(mut my_docs) = self.routes_doc.lock() {
my_docs.extend(other_docs.clone());
}
}
self.inner = self.inner.merge(other.into_axum());
self
}
pub fn nest(mut self, prefix: &str, other: Router<S>) -> Self {
if let Ok(other_docs) = other.routes_doc.lock() {
if let Ok(mut my_docs) = self.routes_doc.lock() {
for mut doc in other_docs.clone() {
doc.path = format!(
"{}/{}",
prefix.trim_end_matches('/'),
doc.path.trim_start_matches('/')
);
my_docs.push(doc);
}
}
}
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 + Sync + '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 + Sync + '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 guard<F, T>(self, f: F) -> Self
where
F: Clone + Send + Sync + 'static,
axum::middleware::FromFnLayer<F, (), T>: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service:
tower::Service<axum::extract::Request> + Clone + Send + Sync + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Future:
Send + 'static,
{
self.layer(axum::middleware::from_fn(f))
}
pub fn middleware_fn<F, T>(self, f: F) -> Self
where
F: Clone + Send + Sync + 'static,
axum::middleware::FromFnLayer<F, (), T>: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service:
tower::Service<axum::extract::Request> + Clone + Send + Sync + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Future:
Send + 'static,
{
self.guard(f)
}
pub fn route_guard<F, T>(self, f: F) -> Self
where
F: Clone + Send + Sync + 'static,
axum::middleware::FromFnLayer<F, (), T>: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service:
tower::Service<axum::extract::Request> + Clone + Send + Sync + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Response:
axum::response::IntoResponse + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<<axum::middleware::FromFnLayer<F, (), T> as tower::Layer<axum::routing::Route>>::Service as tower::Service<axum::extract::Request>>::Future:
Send + 'static,
{
self.route_layer(axum::middleware::from_fn(f))
}
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 + Sync + '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 + Sync + '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 can(self, permission: &'static str) -> Self {
self.route_layer(axum::middleware::from_fn(
move |req: axum::extract::Request, next: axum::middleware::Next| async move {
let user = req.extensions().get::<crate::core::auth::AuthUser>();
match user {
Some(u) if u.can(permission) => next.run(req).await,
Some(_) => crate::http::middleware::forbidden_json(&format!(
"Akses ditolak: Izin '{}' diperlukan.",
permission
)),
None => crate::http::middleware::unauthorized_json("Silakan login terlebih dahulu."),
}
},
))
}
pub fn has_role(self, role: &'static str) -> Self {
self.route_layer(axum::middleware::from_fn(
move |req: axum::extract::Request, next: axum::middleware::Next| async move {
let user = req.extensions().get::<crate::core::auth::AuthUser>();
match user {
Some(u) if u.has_role(role) => next.run(req).await,
Some(_) => crate::http::middleware::forbidden_json(&format!(
"Akses ditolak: Role '{}' diperlukan.",
role
)),
None => crate::http::middleware::unauthorized_json("Silakan login terlebih dahulu."),
}
},
))
}
pub fn panic_boundary(self) -> Self {
self.layer(axum::middleware::from_fn(
crate::core::middleware::zero_panic_boundary,
))
}
pub fn security_headers(self) -> Self {
self.layer(axum::middleware::from_fn(
crate::http::middleware::security_headers,
))
}
pub fn routes_doc(&self) -> Vec<crate::core::openapi::RouteDoc> {
self.routes_doc.lock().map(|d| d.clone()).unwrap_or_default()
}
pub fn openapi_spec(&self, title: &str, version: &str) -> crate::core::openapi::OpenApiSpec {
let docs = self.routes_doc();
crate::core::openapi::OpenApiSpec::from_routes(&docs, title, version)
}
pub fn openapi_json(&self, title: &str, version: &str) -> String {
self.openapi_spec(title, version).to_json_pretty().unwrap_or_default()
}
pub fn openapi_yaml(&self, title: &str, version: &str) -> String {
self.openapi_spec(title, version).to_yaml()
}
pub fn export_openapi_json(&self, path: impl AsRef<std::path::Path>, title: &str, version: &str) -> std::io::Result<()> {
let json = self.openapi_json(title, version);
std::fs::write(path, json)
}
pub fn export_openapi_yaml(&self, path: impl AsRef<std::path::Path>, title: &str, version: &str) -> std::io::Result<()> {
let yaml = self.openapi_yaml(title, version);
std::fs::write(path, yaml)
}
pub fn export_postman(&self, path: impl AsRef<std::path::Path>, collection_name: &str, version: &str) -> std::io::Result<()> {
let spec = self.openapi_spec(collection_name, version);
let postman = spec.to_postman_collection(collection_name);
let json = serde_json::to_string_pretty(&postman).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
std::fs::write(path, json)
}
pub fn swagger_ui(mut self, mount_path: &str, title: &str, version: &str) -> Self
where
S: Clone + Send + Sync + 'static,
{
let base_mount = mount_path.trim_end_matches('/').to_string();
let json_path = format!("{}/openapi.json", base_mount);
let title_owned = title.to_string();
let version_owned = version.to_string();
let swagger_html = crate::core::openapi::generate_swagger_ui_html(&json_path, title);
let routes_arc = self.routes_doc.clone();
let json_handler = move || {
let r_arc = routes_arc.clone();
let t = title_owned.clone();
let v = version_owned.clone();
async move {
let docs = r_arc.lock().map(|d| d.clone()).unwrap_or_default();
let spec = crate::core::openapi::OpenApiSpec::from_routes(&docs, &t, &v);
let json = spec.to_json_pretty().unwrap_or_default();
([(axum::http::header::CONTENT_TYPE, "application/json")], json)
}
};
let html_clone1 = swagger_html.clone();
let html_clone2 = swagger_html;
let swagger_route = format!("{}/swagger", base_mount);
let root_route = if base_mount.is_empty() { "/docs".to_string() } else { base_mount.clone() };
self.inner = self.inner
.route(&root_route, routing::get(move || {
let h = html_clone1.clone();
async move { axum::response::Html(h) }
}))
.route(&swagger_route, routing::get(move || {
let h = html_clone2.clone();
async move { axum::response::Html(h) }
}))
.route(&json_path, routing::get(json_handler));
self
}
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()
}
}