Files
axum/examples/static-file-server/src/main.rs
T

41 lines
1.1 KiB
Rust
Raw Normal View History

2021-08-02 23:09:09 +02:00
//! Run with
//!
//! ```not_rust
//! cargo run -p example-static-file-server
2021-08-02 23:09:09 +02:00
//! ```
2021-11-16 20:49:07 +01:00
use axum::{http::StatusCode, routing::get_service, Router};
2021-10-24 19:33:03 +02:00
use std::net::SocketAddr;
2021-06-06 21:53:22 +02:00
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();
2021-06-06 21:53:22 +02:00
let app = Router::new()
.nest(
"/static",
get_service(ServeDir::new(".")).handle_error(|error: std::io::Error| async move {
2021-10-24 19:33:03 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
2021-10-24 19:33:03 +02:00
)
}),
)
.layer(TraceLayer::new_for_http());
2021-06-06 21:53:22 +02:00
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
2021-06-19 12:50:33 +02:00
.serve(app.into_make_service())
.await
.unwrap();
2021-06-06 21:53:22 +02:00
}