How to use tower_http::normalize_path
in axum v0.7?
#2377
Answered
by
davidpdrsn
duskmoon314
asked this question in
Q&A
-
SummaryI'm trying to upgrade my project from Axum v0.6.17 to v0.7.1 and come across the error that I need to provide type annotations. I followed the information provided by Rustc but need help finding out how to fix this. Previously in Axum v0.6.17, which works fine Ok(Server::bind(&(cfg.ip, cfg.port).into())
.serve(
NormalizePathLayer::trim_trailing_slash()
.layer(
Router::new()
.merge(data::api())
.merge(request_inspection::api())
.merge(http_method::api())
.layer(CorsLayer::new().allow_origin(AllowOrigin::mirror_request()))
.layer(TraceLayer::new_for_http()),
)
.into_make_service_with_connect_info::<SocketAddr>(),
)
.await?) What I'm trying in Axum v0.7.1 let app = Router::new()
.merge(data::api())
.merge(request_inspection::api())
.merge(http_method::api())
.layer(CorsLayer::new().allow_origin(AllowOrigin::mirror_request()))
.layer(TraceLayer::new_for_http());
let app = NormalizePathLayer::trim_trailing_slash().layer(app);
let listener = tokio::net::TcpListener::bind((cfg.ip, cfg.port)).await?;
Ok(axum::serve(listener, app.into_make_service()).await?) rustc gives this error: error[E0284]: type annotations needed
--> httpbin-axum/src/main.rs:37:34
|
37 | Ok(axum::serve(listener, app.into_make_service()).await?)
| ^^^^^^^^^^^^^^^^^
|
= note: cannot satisfy `<_ as HttpBody>::Data == axum::body::Bytes`
= note: required for `Router` to implement `tower_service::Service<axum::http::Request<_>>`
= note: 1 redundant requirement hidden
= note: required for `NormalizePath<Router>` to implement `tower_service::Service<axum::http::Request<_>>`
note: required by a bound in `axum::ServiceExt::into_make_service`
--> /Users/duskmoon/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/axum-0.7.1/src/service_ext.rs:8:26
|
8 | pub trait ServiceExt<R>: Service<R> + Sized {
| ^^^^^^^^^^ required by this bound in `ServiceExt::into_make_service`
...
18 | fn into_make_service(self) -> IntoMakeService<Self>;
| ----------------- required by a bound in this associated function
help: try using a fully qualified path to specify the expected types
|
37 | Ok(axum::serve(listener, <NormalizePath<Router> as ServiceExt<axum::http::Request<ReqBody>>>::into_make_service(app)).await?)
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ~
For more information about this error, try `rustc --explain E0284`. I cannot find the definition of axum version0.7.1 |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Nov 29, 2023
Replies: 1 comment 4 replies
-
You can use "fully qualified" syntax to call use axum::{extract::Request, Router, ServiceExt};
use tower::Layer;
use tower_http::normalize_path::NormalizePathLayer;
#[tokio::main]
async fn main() {
let app = Router::new();
let app = NormalizePathLayer::trim_trailing_slash().layer(app);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, ServiceExt::<Request>::into_make_service(app)) // <-- this
.await
.unwrap();
} |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
duskmoon314
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use "fully qualified" syntax to call
ServiceExt::into_make_service
and specific the type parameters: