Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active June 13, 2025 01:40
Show Gist options
  • Save masakielastic/503162234e744f40a637a8034d361856 to your computer and use it in GitHub Desktop.
Save masakielastic/503162234e744f40a637a8034d361856 to your computer and use it in GitHub Desktop.
Axum v0.8.4 HTTP/1 サーバー最小構成
[dependencies]
axum = { version = "0.8" }
tokio = { version = "1", features = ["full"] }
use axum::{routing::get, Router};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Listening on http://{}", addr);
let listener = tokio::net::TcpListener::bind(addr)
.await
.expect("failed to bind");
axum::serve(listener, app)
.await
.expect("server error");
}
async fn handler() -> &'static str {
"Hello, Axum v0.8.4!"
}
use axum::{routing::get, Router};
use std::net::SocketAddr;
use tokio::signal;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Listening on http://{}", addr);
let listener = tokio::net::TcpListener::bind(addr)
.await
.expect("failed to bind");
// シャットダウンシグナルを受け取る
let shutdown_signal = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
println!("\nReceived shutdown signal. Cleaning up...");
};
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal)
.await
.expect("server error");
println!("Server shut down gracefully.");
}
async fn handler() -> &'static str {
"Hello, Axum v0.8.4 with graceful shutdown!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment