Expand support to TRACE, CONNECT & any method
This commit is contained in:
parent
902ef858ce
commit
ca7262d40e
3 changed files with 64 additions and 13 deletions
12
examples/advanced/api/ping/route.rs
Normal file
12
examples/advanced/api/ping/route.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use axum::response::Html;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>GET Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
// This tests that our macro generates the routes in the correct order
|
||||||
|
// as any is only allowable as a first route.
|
||||||
|
pub async fn any() -> impl IntoResponse {
|
||||||
|
Html("<h1>ANY Pong!</h1>").into_response()
|
||||||
|
}
|
41
src/lib.rs
41
src/lib.rs
|
@ -54,7 +54,7 @@
|
||||||
//!
|
//!
|
||||||
//! ### HTTP Methods
|
//! ### HTTP Methods
|
||||||
//!
|
//!
|
||||||
//! The macro supports all standard HTTP methods:
|
//! The macro supports all standard HTTP methods as defined in RFC9110.
|
||||||
//! - ```get```
|
//! - ```get```
|
||||||
//! - ```post```
|
//! - ```post```
|
||||||
//! - ```put```
|
//! - ```put```
|
||||||
|
@ -62,6 +62,11 @@
|
||||||
//! - ```patch```
|
//! - ```patch```
|
||||||
//! - ```head```
|
//! - ```head```
|
||||||
//! - ```options```
|
//! - ```options```
|
||||||
|
//! - ```trace```
|
||||||
|
//! - ```connect```
|
||||||
|
//!
|
||||||
|
//! And additionally
|
||||||
|
//! - ```any```, which maches all methods
|
||||||
//!
|
//!
|
||||||
//! ### Path Parameters
|
//! ### Path Parameters
|
||||||
//!
|
//!
|
||||||
|
@ -189,7 +194,6 @@ impl ModuleDir {
|
||||||
/// Cargo manifest directory
|
/// Cargo manifest directory
|
||||||
/// * `state_type` - The type name of your application state that will be shared
|
/// * `state_type` - The type name of your application state that will be shared
|
||||||
/// across all routes
|
/// across all routes
|
||||||
///
|
|
||||||
#[allow(clippy::missing_panics_doc)]
|
#[allow(clippy::missing_panics_doc)]
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
@ -298,30 +302,41 @@ fn methods_for_route(route_path: &PathBuf) -> Vec<&'static str> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Define HTTP methods we're looking for
|
// Define HTTP methods we're looking for
|
||||||
let methods = ["get", "post", "put", "delete", "patch", "head", "options"];
|
let allowed_methods = [
|
||||||
|
"any", "get", "post", "put", "delete", "patch", "head", "options", "trace", "connect",
|
||||||
|
];
|
||||||
let mut found_methods = Vec::new();
|
let mut found_methods = Vec::new();
|
||||||
|
|
||||||
// Examine each item in the file
|
// Collect all pub & async fn's
|
||||||
for item in &file.items {
|
for item in &file.items {
|
||||||
if let Item::Fn(fn_item) = item {
|
if let Item::Fn(fn_item) = item {
|
||||||
let fn_name = fn_item.sig.ident.to_string();
|
let fn_name = fn_item.sig.ident.to_string();
|
||||||
|
|
||||||
// Check if the function name is one of our HTTP methods
|
// Check if the function name is one of our HTTP methods
|
||||||
if let Some(&method) = methods.iter().find(|&&m| m == fn_name) {
|
// if let Some(&method) = methods.iter().find(|&&m| m == fn_name) {
|
||||||
// Check if the function is public
|
// Check if the function is public
|
||||||
let is_public = matches!(fn_item.vis, Visibility::Public(_));
|
let is_public = matches!(fn_item.vis, Visibility::Public(_));
|
||||||
|
|
||||||
// Check if the function is async
|
// Check if the function is async
|
||||||
let is_async = fn_item.sig.asyncness.is_some();
|
let is_async = fn_item.sig.asyncness.is_some();
|
||||||
|
|
||||||
if is_public && is_async {
|
if is_public && is_async {
|
||||||
found_methods.push(method);
|
found_methods.push(fn_name);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
found_methods
|
// Iterate through methods to ensure consistent order
|
||||||
|
allowed_methods
|
||||||
|
.into_iter()
|
||||||
|
.filter(|elem| {
|
||||||
|
found_methods
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.any(|method| method == *elem)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn route_registrations(
|
fn route_registrations(
|
||||||
|
|
|
@ -51,6 +51,20 @@ mod __folder_router__myfolderrouter__examples_advanced_api {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[path = "ping"]
|
||||||
|
pub mod ping {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::Html;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>GET Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
pub async fn any() -> impl IntoResponse {
|
||||||
|
Html("<h1>ANY Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
#[path = "users"]
|
#[path = "users"]
|
||||||
pub mod users {
|
pub mod users {
|
||||||
#[path = "route.rs"]
|
#[path = "route.rs"]
|
||||||
|
@ -99,6 +113,16 @@ impl MyFolderRouter {
|
||||||
__folder_router__myfolderrouter__examples_advanced_api::files::route::post,
|
__folder_router__myfolderrouter__examples_advanced_api::files::route::post,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/ping",
|
||||||
|
axum::routing::any(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::ping::route::any,
|
||||||
|
)
|
||||||
|
.get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::ping::route::get,
|
||||||
|
),
|
||||||
|
);
|
||||||
router = router
|
router = router
|
||||||
.route(
|
.route(
|
||||||
"/",
|
"/",
|
||||||
|
|
Loading…
Add table
Reference in a new issue