Making tower-http's ServeDir fall back to axum::Router #3206
-
SummaryI'm trying to combine tower-http's ServeDir and axum's Router in such a way that if a static file exists, it should be served, otherwise the request should be passed to the axum Router. Here's what I have so far: use axum::ServiceExt;
#[tokio::main]
async fn main() {
let app = axum::Router::new().route(
"/{*wildcard}",
axum::routing::get(|| async { "Hello, World!" }),
);
let app = tower_http::services::ServeDir::new("src").fallback(app);
let listener = tokio::net::TcpListener::bind("0.0.0.0:4000").await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
} With this, I expect to get the contents of This fails to compile:
I have tried many variations of this but could not get any to compile. Doing it the other way, where axum's Router falls back to ServeDir works fine, but I want the static files to be served first. [dependencies]
axum = "0.8.1"
tokio = { version = "1.43.0", features = ["full"] }
tower-http = { version = "0.6.2", features = ["fs"] } Any pointers? axum version0.8.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can try looking at #3202, or more specifically just calling |
Beta Was this translation helpful? Give feedback.
Sorry, I thought that method was available on the services themselves, not just the builder.
You can do this to force the correct types:
Then the code you posted originally compiles.
The
into_service
call seems like something we should be able to get rid of, but it seems the type inference needs it right now.