mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-16 00:00:18 +02:00
* Refactor initializing tracing-subscriber
* Revert "Refactor initializing tracing-subscriber"
This reverts commit 0876260bf9 in favor of tracing_subscriber::registry.
* Use EnvFilter::try_from_default_env in chat example
* Use EnvFilter::try_from_default_env in examples
48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
//! Run with
|
|
//!
|
|
//! ```not_rust
|
|
//! cd examples && cargo run -p example-global-404-handler
|
|
//! ```
|
|
|
|
use axum::{
|
|
http::StatusCode,
|
|
response::{Html, IntoResponse},
|
|
routing::get,
|
|
Router,
|
|
};
|
|
use std::net::SocketAddr;
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
.unwrap_or_else(|_| "example_global_404_handler=debug".into()),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
|
|
// build our application with a route
|
|
let app = Router::new().route("/", get(handler));
|
|
|
|
// add a fallback service for handling routes to unknown paths
|
|
let app = app.fallback(handler_404);
|
|
|
|
// run it
|
|
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();
|
|
}
|
|
|
|
async fn handler() -> Html<&'static str> {
|
|
Html("<h1>Hello, World!</h1>")
|
|
}
|
|
|
|
async fn handler_404() -> impl IntoResponse {
|
|
(StatusCode::NOT_FOUND, "nothing to see here")
|
|
}
|