Last active
June 13, 2025 01:40
-
-
Save masakielastic/503162234e744f40a637a8034d361856 to your computer and use it in GitHub Desktop.
Axum v0.8.4 HTTP/1 サーバー最小構成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[dependencies] | |
axum = { version = "0.8" } | |
tokio = { version = "1", features = ["full"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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