Update static file server example (#1011)

This commit is contained in:
David Pedersen 2022-05-08 21:52:34 +02:00 committed by GitHub
parent 852e548e19
commit 7774cfd1f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 12 deletions

View file

@ -6,6 +6,7 @@ publish = false
[dependencies]
axum = { path = "../../axum" }
axum-extra = { path = "../../axum-extra", features = ["spa"] }
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

View file

@ -4,8 +4,13 @@
//! cd examples && cargo run -p example-static-file-server
//! ```
use axum::{http::StatusCode, routing::get_service, Router};
use std::net::SocketAddr;
use axum::{
http::StatusCode,
response::IntoResponse,
routing::{get, get_service},
Router,
};
use std::{io, net::SocketAddr};
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -19,16 +24,17 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();
let app = Router::new()
.nest(
"/static",
get_service(ServeDir::new(".")).handle_error(|error: std::io::Error| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
}),
)
// `SpaRouter` is the easiest way to serve assets at a nested route like `/assets`
// let app = Router::new()
// .route("/foo", get(|| async { "Hi from /foo" }))
// .merge(axum_extra::routing::SpaRouter::new("/assets", "."))
// .layer(TraceLayer::new_for_http());
// for serving assets directly at the root you can use `tower_http::services::ServeDir`
// as the fallback to a `Router`
let app: _ = Router::new()
.route("/foo", get(|| async { "Hi from /foo" }))
.fallback(get_service(ServeDir::new(".")).handle_error(handle_error))
.layer(TraceLayer::new_for_http());
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@ -38,3 +44,7 @@ async fn main() {
.await
.unwrap();
}
async fn handle_error(_err: io::Error) -> impl IntoResponse {
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong...")
}