-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.rs
46 lines (38 loc) · 1.18 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
use axum::{response::Html, routing::get, Router};
use tokio::signal;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
// build our application with a route
let app = Router::new().route("/", get(handler));
let bind_addr = std::env::var("BIND_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
// run it
let listener = tokio::net::TcpListener::bind(bind_addr)
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()).await.unwrap();
}
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, Francesco!</h1>")
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}