#[macro_export]
#[doc(hidden)]
macro_rules! __to_method {
(GET) => {
$crate::axum::routing::get
};
(POST) => {
$crate::axum::routing::post
};
(PUT) => {
$crate::axum::routing::put
};
(DELETE) => {
$crate::axum::routing::delete
};
(PATCH) => {
$crate::axum::routing::patch
};
(HEAD) => {
$crate::axum::routing::head
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_handler {
($router:expr, $method:ident, $path:literal, [$($params:tt)*], | { $($body:tt)* } , $($rest:tt)*) => {
$crate::__add_routes!(
$router.route(
$path,
$crate::__to_method!($method)(| $($params)* | async move { $($body)* })
),
$($rest)*
)
};
($router:expr, $method:ident, $path:literal, [$($params:tt)*], | { $($body:tt)* }) => {
$router.route(
$path,
$crate::__to_method!($method)(| $($params)* | async move { $($body)* })
)
};
($router:expr, $method:ident, $path:literal, [$($params:tt)*], $tok:tt $($rest:tt)*) => {
$crate::__parse_handler!($router, $method, $path, [$($params)* $tok], $($rest)*)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __add_routes {
($router:expr ,) => { $router };
($router:expr) => { $router };
($router:expr, $method:ident $path:literal => { $($body:tt)* } , $($rest:tt)*) => {
$crate::__add_routes!(
$router.route($path, $crate::__to_method!($method)(|| async { $($body)* })),
$($rest)*
)
};
($router:expr, $method:ident $path:literal => { $($body:tt)* }) => {
$router.route($path, $crate::__to_method!($method)(|| async { $($body)* }))
};
($router:expr, $method:ident $path:literal => | $($rest:tt)*) => {
$crate::__parse_handler!($router, $method, $path, [], $($rest)*)
};
}
#[macro_export]
macro_rules! app {
(
Config => {
port: $port:expr,
host: $host:expr,
state: $state_type:ident $state_init:tt
},
$($routes:tt)*
) => {
pub fn create_app() -> $crate::Router {
#[allow(unused_imports)]
use $crate::extract::Path;
let state = $state_type $state_init;
let router = $crate::axum::Router::new();
$crate::__add_routes!(router, $($routes)*).with_state(state)
}
#[cfg(not(test))]
#[$crate::tokio::main]
async fn main() {
let addr = format!("{}:{}", $host, $port);
println!("🪦 Grave listening on {}", addr);
let listener = $crate::tokio::net::TcpListener::bind(&addr)
.await
.unwrap();
$crate::axum::serve(listener, create_app()).await.unwrap();
}
};
(
Config => {
port: $port:expr,
host: $host:expr
},
$($routes:tt)*
) => {
pub fn create_app() -> $crate::Router {
#[allow(unused_imports)]
use $crate::extract::Path;
let router = $crate::axum::Router::new();
$crate::__add_routes!(router, $($routes)*)
}
#[cfg(not(test))]
#[$crate::tokio::main]
async fn main() {
let addr = format!("{}:{}", $host, $port);
println!("🪦 Grave listening on {}", addr);
let listener = $crate::tokio::net::TcpListener::bind(&addr)
.await
.unwrap();
$crate::axum::serve(listener, create_app()).await.unwrap();
}
};
}