Issue migrating ServeDir handler from Axum 0.6 to 0.7
Closed this issue · 0 comments
JasonGH17 commented
- I have looked for existing issues (including closed) about this
Bug Report
Recently started to migrate to Axum 0.7
I had a static file hosting handler setup, but when updating to 0.7 it started throwing an error at compile time.
Compile error:
type annotations needed
cannot satisfy `<_ as axum::body::HttpBody>::Data == axum::body::Bytes`
required for `HandlerService<fn() -> impl Future<Output = (StatusCode, &str)> {not_found}, ((),), ()>` to implement `tower::Service<axum::http::Request<_>>`
1 redundant requirement hidden
required for `ServeDir<SetStatus<HandlerService<fn() -> impl Future<Output = (StatusCode, &str)> {not_found}, ((),), ()>>>` to implement `tower::Service<axum::http::Request<_>>`
Version
axum: 0.7.8
Platform
Windows (64-bit)
Crates
tower: 0.4.13
tower-http: 0.5.2
Description
The snippet of code:
use axum::{
handler::HandlerWithoutStateExt,
http::{HeaderValue, StatusCode},
middleware::from_fn_with_state,
Router,
};
use tower::ServiceExt;
use tower_http::services::ServeDir;
pub fn setup_static_fs_handler() -> Router {
async fn not_found() -> (StatusCode, &'static str) {
(StatusCode::NOT_FOUND, "Resource not found")
}
let not_found_service = not_found.into_service();
let serve = ServeDir::new("data")
.precompressed_gzip()
.not_found_service(not_found_service)
.map_response(|mut res| {
if res.status() != StatusCode::NOT_FOUND {
res.headers_mut()
.insert("Cache-Control", HeaderValue::from_static("max-age=3600"));
}
res
});
Router::new().nest_service("/", serve)
}