mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-17 00:00:16 +02:00
* Move `axum-handle-error-extract` into axum With 0.4 underway we can now nuke `axum-handle-error-extract` and move its code directly into axum. So this replaces the old `HandleErrorLayer` with one that supports async functions and extractors. * changelog * fix CI
41 lines
1.1 KiB
Rust
41 lines
1.1 KiB
Rust
//! Run with
|
|
//!
|
|
//! ```not_rust
|
|
//! cargo run -p example-static-file-server
|
|
//! ```
|
|
|
|
use axum::{http::StatusCode, routing::get_service, Router};
|
|
use std::net::SocketAddr;
|
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
|
if std::env::var_os("RUST_LOG").is_none() {
|
|
std::env::set_var(
|
|
"RUST_LOG",
|
|
"example_static_file_server=debug,tower_http=debug",
|
|
)
|
|
}
|
|
tracing_subscriber::fmt::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),
|
|
)
|
|
}),
|
|
)
|
|
.layer(TraceLayer::new_for_http());
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
tracing::debug!("listening on {}", addr);
|
|
axum::Server::bind(&addr)
|
|
.serve(app.into_make_service())
|
|
.await
|
|
.unwrap();
|
|
}
|