From: Carl Worth Date: Tue, 10 Mar 2026 05:20:10 +0000 (-0700) Subject: Add dependencies on axum and tokio and implement a do-nothing server X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=74714d369f58c7df9a6fb376ea0f7d82a7b0b5b0;p=rust-learning Add dependencies on axum and tokio and implement a do-nothing server This server binds to port 3000 on all interfaces and then just listens, (with no routes configured to do anything). The axum library gives us the server and router, (which, like I said, we aren't using yet). And tokio gives us some network functionality (see the TCP listener and the bind function) as well as some asynchronous functionality, (see the #[tokio::main] attribute which saves bunch of boilerplate that would otherwise be needed (creating a tokio::runtime::Builder and giving it a function to run), and instead lets us just use the entire body of main as the async function body that tokio launches. --- diff --git a/Cargo.toml b/Cargo.toml index c2fd62e..8d2cc62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +axum = "0.8.8" serde = { version = "1", features = ["derive"]} serde_json = "1" +tokio = { version = "1.50.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 7154081..baca8fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ mod config; use std::error::Error; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); if args.len() < 2 { return Err(format!("Usage: {} ", args[0]).into()); @@ -16,5 +17,19 @@ fn main() -> Result<(), Box> { eprintln!("Race: {}", config.name); + let bind = "0.0.0.0:3000"; + eprintln!("Listening on {}", bind); + + let listener = tokio::net::TcpListener::bind(&bind).await?; + + let app = axum::Router::new(); + + axum::serve(listener, app) + .with_graceful_shutdown(async move { + tokio::signal::ctrl_c().await.ok(); + eprintln!("\nShutting down."); + }) + .await?; + Ok(()) }