use axum::Router;
use axum::routing;
pub use axum::Json;
pub use axum::extract::{Path, Query, State};
pub struct Route {
router: Router,
}
impl Route {
pub fn new() -> Self {
Self {
router: Router::new(),
}
}
pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
where
H: axum::handler::Handler<T, ()>,
T: 'static,
{
self.router = self.router.route(path, routing::get(handler));
self
}
pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
where
H: axum::handler::Handler<T, ()>,
T: 'static,
{
self.router = self.router.route(path, routing::post(handler));
self
}
pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
where
H: axum::handler::Handler<T, ()>,
T: 'static,
{
self.router = self.router.route(path, routing::put(handler));
self
}
pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
where
H: axum::handler::Handler<T, ()>,
T: 'static,
{
self.router = self.router.route(path, routing::delete(handler));
self
}
pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
where
H: axum::handler::Handler<T, ()>,
T: 'static,
{
self.router = self.router.route(path, routing::patch(handler));
self
}
pub fn merge(mut self, prefix: &str, router: Router) -> Self {
self.router = self.router.nest(prefix, router);
self
}
pub fn group(mut self, prefix: &str, f: impl FnOnce(Route) -> Route) -> Self {
let group_routes = f(Route::new());
self.router = self.router.nest(prefix, group_routes.into_router());
self
}
pub fn with(mut self, f: impl FnOnce(Router) -> Router) -> Self {
self.router = f(self.router);
self
}
pub fn controller<C: crate::controller::Controller>(
self,
ctrl: C,
f: impl FnOnce(Route, &C) -> Route,
) -> Self {
f(self, &ctrl)
}
pub fn middleware<F, M>(self, f: F) -> Self
where
F: Fn(axum::extract::Request, axum::middleware::Next) -> M + Clone + Send + Sync + 'static,
M: std::future::Future<Output = axum::response::Response> + Send + 'static,
{
self.with(|r| r.layer(axum::middleware::from_fn(f)))
}
pub fn layer<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 + Send,
<L::Service as tower::Service<axum::extract::Request>>::Error:
Into<std::convert::Infallible> + 'static,
<L::Service as tower::Service<axum::extract::Request>>::Future: Send,
{
self.with(|r| r.layer(layer))
}
pub fn build(self) -> Router {
self.router
}
pub fn into_router(self) -> Router {
self.router
}
pub fn inner(&self) -> &Router {
&self.router
}
}
impl Default for Route {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::http::StatusCode;
use axum::response::IntoResponse;
async fn index() -> &'static str {
"Hello, Ravel!"
}
async fn user_show(Path(id): Path<String>) -> String {
format!("User {id}")
}
async fn user_create() -> impl IntoResponse {
(StatusCode::CREATED, "created")
}
#[test]
fn test_basic_routes_compile() {
let router = Route::new()
.get("/", index)
.get("/users/{id}", user_show)
.post("/users", user_create)
.put("/users/{id}", user_show)
.delete("/users/{id}", user_show)
.patch("/users/{id}", user_show)
.build();
let _ = router;
}
#[test]
fn test_route_group() {
let router = Route::new()
.get("/", index)
.group("/api", |api| {
api.get("/status", index).post("/users", user_create)
})
.build();
let _ = router;
}
#[test]
fn test_merge_sub_router() {
let sub = Route::new().get("/ping", index).into_router();
let router = Route::new().merge("/v1", sub).build();
let _ = router;
}
#[test]
fn test_default_creates_empty_router() {
let route = Route::default();
let _router = route.build();
}
#[test]
fn test_middleware_compiles() {
async fn my_middleware(
req: axum::extract::Request,
next: axum::middleware::Next,
) -> axum::response::Response {
next.run(req).await
}
let _router = Route::new()
.get("/", index)
.middleware(my_middleware)
.build();
}
}