Skip to content
Myr

main.rs

This file

Path

examples/global-404-handler/src/main.rs

45 lines · 1.2 KiB · Rust

Last changed by

Fix return types of serve futures (#3601)Jonas Plattebefore adoptionf3a95d7

History of this file

Take it

1//! Run with2//!3//! ```not_rust4//! cargo run -p example-global-404-handler5//! ```67use axum::{8    http::StatusCode,9    response::{Html, IntoResponse},10    routing::get,11    Router,12};13use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};1415#[tokio::main]16async fn main() {17    tracing_subscriber::registry()18        .with(19            tracing_subscriber::EnvFilter::try_from_default_env()20                .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),21        )22        .with(tracing_subscriber::fmt::layer())23        .init();2425    // build our application with a route26    let app = Router::new().route("/", get(handler));2728    // add a fallback service for handling routes to unknown paths29    let app = app.fallback(handler_404);3031    // run it32    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")33        .await34        .unwrap();35    tracing::debug!("listening on {}", listener.local_addr().unwrap());36    axum::serve(listener, app).await;37}3839async fn handler() -> Html<&'static str> {40    Html("<h1>Hello, World!</h1>")41}4243async fn handler_404() -> impl IntoResponse {44    (StatusCode::NOT_FOUND, "nothing to see here")45}