-
Notifications
You must be signed in to change notification settings - Fork 967
/
main.rs
47 lines (40 loc) · 1.19 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! 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")
}