Skip to content

Commit

Permalink
add method_not_allowed_fallback to router
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachstec committed Sep 8, 2024
1 parent 1ac617a commit 73ddf83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ where
.fallback_endpoint(Endpoint::Route(route))
}

pub fn method_not_allowed_fallback<H, T>(self, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static
{
self.tap_inner_mut(|this| {
this.path_router.method_not_allowed_fallback(handler.clone())
})
}

fn fallback_endpoint(self, endpoint: Endpoint<S>) -> Self {
self.tap_inner_mut(|this| {
this.fallback_router.set_fallback(endpoint);
Expand Down
14 changes: 13 additions & 1 deletion axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::extract::{nested_path::SetNestedPath, Request};
use crate::{extract::{nested_path::SetNestedPath, Request}, handler::Handler};
use axum_core::response::IntoResponse;
use matchit::MatchError;
use std::{borrow::Cow, collections::HashMap, convert::Infallible, fmt, sync::Arc};
Expand Down Expand Up @@ -79,6 +79,18 @@ where
Ok(())
}

pub(super) fn method_not_allowed_fallback<H, T>(&mut self, handler: H)
where
H: Handler<T, S>,
T: 'static
{
for (_, endpoint) in self.routes.iter_mut() {
if let Endpoint::MethodRouter(rt) = endpoint {
*rt = rt.clone().fallback(handler.clone());
}
}
}

pub(super) fn route_service<T>(
&mut self,
path: &str,
Expand Down

0 comments on commit 73ddf83

Please sign in to comment.