mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-22 06:55:31 +01:00
* Set RUST_LOG environment var for all examples using tracing Signed-off-by: Spencer Gilbert <spencer.gilbert@gmail.com> * Update examples/multipart_form.rs Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
//! Run with
|
|
//!
|
|
//! ```not_rust
|
|
//! cargo run --example static_file_server
|
|
//! ```
|
|
|
|
use axum::{prelude::*, routing::nest, service::ServiceExt};
|
|
use http::StatusCode;
|
|
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("RUST_LOG").is_err() {
|
|
std::env::set_var("RUST_LOG", "static_file_server=debug,tower_http=debug")
|
|
}
|
|
tracing_subscriber::fmt::fmt()
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
.init();
|
|
|
|
let app = nest(
|
|
"/static",
|
|
axum::service::get(ServeDir::new(".").handle_error(|error: std::io::Error| {
|
|
Ok::<_, std::convert::Infallible>((
|
|
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();
|
|
}
|