Files
axum/examples/global-404-handler/src/main.rs
T

44 lines
1022 B
Rust

//! Run with
//!
//! ```not_rust
//! cargo run -p example-global-404-handler
//! ```
use axum::{
handler::{get, Handler},
response::Html,
Router,
};
use std::net::SocketAddr;
#[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", "example_global_404_handler=debug")
}
tracing_subscriber::fmt::init();
// build our application with a route
let app = Router::new().route("/", get(handler));
// make sure this is added as the very last thing
let app = app.or(handler_404.into_service());
// 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() -> &'static str {
"nothing to see here"
}