From 4fbdd54582fe2a1f47f99552c0fe876ad6c2c125 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 17 Sep 2023 10:30:51 +0200 Subject: [PATCH] Use tuples instead of `ServiceBuilder` internally --- axum/src/routing/method_routing.rs | 13 ++--- axum/src/routing/route.rs | 16 +++--- axum/src/routing/tests/handle_error.rs | 50 +++++++------------ axum/src/routing/tests/merge.rs | 11 ++-- axum/src/routing/tests/mod.rs | 23 +++------ .../src/main.rs | 3 +- 6 files changed, 41 insertions(+), 75 deletions(-) diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index c379d194b8..ae1b2fdca7 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -1254,7 +1254,7 @@ mod tests { use axum_core::response::IntoResponse; use http::{header::ALLOW, HeaderMap}; use std::time::Duration; - use tower::{timeout::TimeoutLayer, Service, ServiceBuilder, ServiceExt}; + use tower::{timeout::TimeoutLayer, Service, ServiceExt}; use tower_http::{services::fs::ServeDir, validate_request::ValidateRequestHeaderLayer}; #[crate::test] @@ -1354,13 +1354,10 @@ mod tests { .merge(delete_service(ServeDir::new("."))) .fallback(|| async { StatusCode::NOT_FOUND }) .put(ok) - .layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(TimeoutLayer::new(Duration::from_secs(10))), - ), + .layer(( + HandleErrorLayer::new(|_| async { StatusCode::REQUEST_TIMEOUT }), + TimeoutLayer::new(Duration::from_secs(10)), + )), ); let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await.unwrap(); diff --git a/axum/src/routing/route.rs b/axum/src/routing/route.rs index 0a2713a8d2..fd63d0e8d0 100644 --- a/axum/src/routing/route.rs +++ b/axum/src/routing/route.rs @@ -17,8 +17,8 @@ use std::{ task::{Context, Poll}, }; use tower::{ - util::{BoxCloneService, MapResponseLayer, Oneshot}, - ServiceBuilder, ServiceExt, + util::{BoxCloneService, MapErrLayer, MapRequestLayer, MapResponseLayer, Oneshot}, + ServiceExt, }; use tower_layer::Layer; use tower_service::Service; @@ -57,12 +57,12 @@ impl Route { >::Future: Send + 'static, NewError: 'static, { - let layer = ServiceBuilder::new() - .map_request(|req: Request<_>| req.map(Body::new)) - .map_err(Into::into) - .layer(MapResponseLayer::new(IntoResponse::into_response)) - .layer(layer) - .into_inner(); + let layer = ( + MapRequestLayer::new(|req: Request<_>| req.map(Body::new)), + MapErrLayer::new(Into::into), + MapResponseLayer::new(IntoResponse::into_response), + layer, + ); Route::new(layer.layer(self)) } diff --git a/axum/src/routing/tests/handle_error.rs b/axum/src/routing/tests/handle_error.rs index 3781677d6b..a1af97af70 100644 --- a/axum/src/routing/tests/handle_error.rs +++ b/axum/src/routing/tests/handle_error.rs @@ -1,6 +1,6 @@ use super::*; use std::future::{pending, ready}; -use tower::{timeout::TimeoutLayer, ServiceBuilder}; +use tower::timeout::TimeoutLayer; async fn unit() {} @@ -33,13 +33,10 @@ impl Service for Svc { async fn handler() { let app = Router::new().route( "/", - get(forever.layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_: BoxError| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(timeout()), - )), + get(forever.layer(( + HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }), + timeout(), + ))), ); let client = TestClient::new(app); @@ -52,13 +49,10 @@ async fn handler() { async fn handler_multiple_methods_first() { let app = Router::new().route( "/", - get(forever.layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_: BoxError| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(timeout()), - )) + get(forever.layer(( + HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }), + timeout(), + ))) .post(unit), ); @@ -73,15 +67,10 @@ async fn handler_multiple_methods_middle() { let app = Router::new().route( "/", delete(unit) - .get( - forever.layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_: BoxError| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(timeout()), - ), - ) + .get(forever.layer(( + HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }), + timeout(), + ))) .post(unit), ); @@ -95,15 +84,10 @@ async fn handler_multiple_methods_middle() { async fn handler_multiple_methods_last() { let app = Router::new().route( "/", - delete(unit).get( - forever.layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_: BoxError| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(timeout()), - ), - ), + delete(unit).get(forever.layer(( + HandleErrorLayer::new(|_: BoxError| async { StatusCode::REQUEST_TIMEOUT }), + timeout(), + ))), ); let client = TestClient::new(app); diff --git a/axum/src/routing/tests/merge.rs b/axum/src/routing/tests/merge.rs index 31b42d8336..74eb24bea5 100644 --- a/axum/src/routing/tests/merge.rs +++ b/axum/src/routing/tests/merge.rs @@ -127,13 +127,10 @@ async fn layer_and_handle_error() { let one = Router::new().route("/foo", get(|| async {})); let two = Router::new() .route("/timeout", get(std::future::pending::<()>)) - .layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_| async { - StatusCode::REQUEST_TIMEOUT - })) - .layer(TimeoutLayer::new(Duration::from_millis(10))), - ); + .layer(( + HandleErrorLayer::new(|_| async { StatusCode::REQUEST_TIMEOUT }), + TimeoutLayer::new(Duration::from_millis(10)), + )); let app = one.merge(two); let client = TestClient::new(app); diff --git a/axum/src/routing/tests/mod.rs b/axum/src/routing/tests/mod.rs index 59047644fa..ab7c825b9f 100644 --- a/axum/src/routing/tests/mod.rs +++ b/axum/src/routing/tests/mod.rs @@ -30,9 +30,7 @@ use std::{ task::{Context, Poll}, time::Duration, }; -use tower::{ - service_fn, timeout::TimeoutLayer, util::MapResponseLayer, ServiceBuilder, ServiceExt, -}; +use tower::{service_fn, timeout::TimeoutLayer, util::MapResponseLayer, ServiceExt}; use tower_http::{limit::RequestBodyLimitLayer, validate_request::ValidateRequestHeaderLayer}; use tower_service::Service; @@ -179,7 +177,6 @@ async fn routing_between_services() { #[crate::test] async fn middleware_on_single_route() { - use tower::ServiceBuilder; use tower_http::{compression::CompressionLayer, trace::TraceLayer}; async fn handle(_: Request) -> &'static str { @@ -188,12 +185,7 @@ async fn middleware_on_single_route() { let app = Router::new().route( "/", - get(handle.layer( - ServiceBuilder::new() - .layer(TraceLayer::new_for_http()) - .layer(CompressionLayer::new()) - .into_inner(), - )), + get(handle.layer((TraceLayer::new_for_http(), CompressionLayer::new()))), ); let client = TestClient::new(app); @@ -309,13 +301,10 @@ async fn wildcard_sees_whole_url() { async fn middleware_applies_to_routes_above() { let app = Router::new() .route("/one", get(std::future::pending::<()>)) - .layer( - ServiceBuilder::new() - .layer(HandleErrorLayer::new(|_: BoxError| async move { - StatusCode::REQUEST_TIMEOUT - })) - .layer(TimeoutLayer::new(Duration::new(0, 0))), - ) + .layer(( + HandleErrorLayer::new(|_: BoxError| async move { StatusCode::REQUEST_TIMEOUT }), + TimeoutLayer::new(Duration::new(0, 0)), + )) .route("/two", get(|| async {})); let client = TestClient::new(app); diff --git a/examples/consume-body-in-extractor-or-middleware/src/main.rs b/examples/consume-body-in-extractor-or-middleware/src/main.rs index fd63fcb1ff..5c93bf23e9 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -14,7 +14,6 @@ use axum::{ routing::post, Router, }; -use tower::ServiceBuilder; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] @@ -29,7 +28,7 @@ async fn main() { let app = Router::new() .route("/", post(handler)) - .layer(ServiceBuilder::new().layer(middleware::from_fn(print_request_body))); + .layer(middleware::from_fn(print_request_body)); let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await