mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-28 00:00:20 +02:00
Merge branch 'main' into separate-nesting-opaque-services
This commit is contained in:
@@ -7,10 +7,13 @@ and this project adheres to [Semantic Versioning].
|
||||
|
||||
# Unreleased
|
||||
|
||||
- **added:** Add `RouterExt::route_with_tsr` for adding routes with an
|
||||
additional "trailing slash redirect" route ([#1119])
|
||||
- **breaking:** `Resource::nest` and `Resource::nest_collection` now only
|
||||
accepts `Router`s ([#1086])
|
||||
|
||||
[#1086]: https://github.com/tokio-rs/axum/pull/1086
|
||||
[#1119]: https://github.com/tokio-rs/axum/pull/1119
|
||||
|
||||
# 0.3.5 (27. June, 2022)
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
//! Additional types for defining routes.
|
||||
|
||||
use axum::{handler::Handler, Router};
|
||||
use axum::{
|
||||
handler::Handler,
|
||||
http::Request,
|
||||
response::{Redirect, Response},
|
||||
Router,
|
||||
};
|
||||
use std::{convert::Infallible, future::ready};
|
||||
use tower_service::Service;
|
||||
|
||||
mod resource;
|
||||
|
||||
@@ -126,6 +133,37 @@ pub trait RouterExt<B>: sealed::Sealed {
|
||||
H: Handler<T, B>,
|
||||
T: FirstElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add another route to the router with an additional "trailing slash redirect" route.
|
||||
///
|
||||
/// If you add a route _without_ a trailing slash, such as `/foo`, this method will also add a
|
||||
/// route for `/foo/` that redirects to `/foo`.
|
||||
///
|
||||
/// If you add a route _with_ a trailing slash, such as `/bar/`, this method will also add a
|
||||
/// route for `/bar` that redirects to `/bar/`.
|
||||
///
|
||||
/// This is similar to what axum 0.5.x did by default, except this explicitly adds another
|
||||
/// route, so trying to add a `/foo/` route after calling `.route_with_tsr("/foo", /* ... */)`
|
||||
/// will result in a panic due to route overlap.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use axum::{Router, routing::get};
|
||||
/// use axum_extra::routing::RouterExt;
|
||||
///
|
||||
/// let app = Router::new()
|
||||
/// // `/foo/` will redirect to `/foo`
|
||||
/// .route_with_tsr("/foo", get(|| async {}))
|
||||
/// // `/bar` will redirect to `/bar/`
|
||||
/// .route_with_tsr("/bar/", get(|| async {}));
|
||||
/// # let _: Router = app;
|
||||
/// ```
|
||||
fn route_with_tsr<T>(self, path: &str, service: T) -> Self
|
||||
where
|
||||
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
|
||||
T::Future: Send + 'static,
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl<B> RouterExt<B> for Router<B>
|
||||
@@ -211,9 +249,62 @@ where
|
||||
{
|
||||
self.route(P::PATH, axum::routing::trace(handler))
|
||||
}
|
||||
|
||||
fn route_with_tsr<T>(mut self, path: &str, service: T) -> Self
|
||||
where
|
||||
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
|
||||
T::Future: Send + 'static,
|
||||
Self: Sized,
|
||||
{
|
||||
self = self.route(path, service);
|
||||
|
||||
let redirect = Redirect::permanent(path);
|
||||
|
||||
if let Some(path_without_trailing_slash) = path.strip_suffix('/') {
|
||||
self.route(
|
||||
path_without_trailing_slash,
|
||||
(move || ready(redirect.clone())).into_service(),
|
||||
)
|
||||
} else {
|
||||
self.route(
|
||||
&format!("{}/", path),
|
||||
(move || ready(redirect.clone())).into_service(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
impl<B> Sealed for axum::Router<B> {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_helpers::*;
|
||||
use axum::{http::StatusCode, routing::get};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tsr() {
|
||||
let app = Router::new()
|
||||
.route_with_tsr("/foo", get(|| async {}))
|
||||
.route_with_tsr("/bar/", get(|| async {}));
|
||||
|
||||
let client = TestClient::new(app);
|
||||
|
||||
let res = client.get("/foo").send().await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
let res = client.get("/foo/").send().await;
|
||||
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
|
||||
assert_eq!(res.headers()["location"], "/foo");
|
||||
|
||||
let res = client.get("/bar/").send().await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
let res = client.get("/bar").send().await;
|
||||
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
|
||||
assert_eq!(res.headers()["location"], "/bar/");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user