Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tuples instead of ServiceBuilder internally #2229

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)),
)),
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
);

let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await.unwrap();
Expand Down
16 changes: 8 additions & 8 deletions axum/src/routing/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,12 +57,12 @@ impl<E> Route<E> {
<L::Service as Service<Request>>::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))
}
Expand Down
50 changes: 17 additions & 33 deletions axum/src/routing/tests/handle_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use std::future::{pending, ready};
use tower::{timeout::TimeoutLayer, ServiceBuilder};
use tower::timeout::TimeoutLayer;

async fn unit() {}

Expand Down Expand Up @@ -33,13 +33,10 @@ impl<R> Service<R> 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);
Expand All @@ -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),
);

Expand All @@ -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),
);

Expand All @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions axum/src/routing/tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
));
Comment on lines +130 to +133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

let app = one.merge(two);

let client = TestClient::new(app);
Expand Down
23 changes: 6 additions & 17 deletions axum/src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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)),
))
Comment on lines +304 to +307
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

.route("/two", get(|| async {}));

let client = TestClient::new(app);
Expand Down
3 changes: 1 addition & 2 deletions examples/consume-body-in-extractor-or-middleware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use axum::{
routing::post,
Router,
};
use tower::ServiceBuilder;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
Expand All @@ -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
Expand Down