From b3b9cd92afcf097a7d70d2204fe2716e19a7471b Mon Sep 17 00:00:00 2001 From: Adam Doyle Date: Wed, 9 Oct 2024 20:39:48 -0400 Subject: [PATCH] add argument to bind server to specific address --- Cargo.lock | 7 ++++--- readme.md | 2 ++ server/Cargo.toml | 1 + server/src/main.rs | 35 ++++++++++++++++++++++++++++------- test | 2 +- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c222a1d..f7f8b10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1399,7 +1399,7 @@ dependencies = [ [[package]] name = "leptos-use" version = "0.14.0-beta5" -source = "git+https://github.com/Synphonyte/leptos-use.git?branch=leptos-0.7#c6d22e19cb73318d979f5fe4b355c133adcaa5ea" +source = "git+https://github.com/Synphonyte/leptos-use.git?branch=leptos-0.7#0d0d4616f9f0af0482386123f9d91c51f2a17dcf" dependencies = [ "cfg-if", "codee", @@ -2321,6 +2321,7 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", + "clap", "futures", "lib", "rand", @@ -2804,9 +2805,9 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.5.2" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" +checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" dependencies = [ "async-compression", "bitflags", diff --git a/readme.md b/readme.md index c8d2f69..ef6124c 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,8 @@ Then: ./test ``` +Use `-b ` or `--bind ` to bind the server to a specific address. Default is 127.0.0.1:3030. You can also pass arguments to the test script. + Without auto-reload: ```sh diff --git a/server/Cargo.toml b/server/Cargo.toml index 74d414b..e54b8dc 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -9,6 +9,7 @@ lib = { workspace = true } anyhow = "1" axum = { version = "0", features = ["ws"] } +clap = { version = "4", features = ["cargo"] } futures = "0" rand = "0" serde = { version = "1", features = ["derive"] } diff --git a/server/src/main.rs b/server/src/main.rs index c586a69..45a8e4f 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -3,6 +3,7 @@ use crate::message_handler::*; use crate::websocket::*; use anyhow::{Context, Result}; use axum::{routing::get, Router}; +use clap::{arg, command}; use server::*; use std::{ collections::{HashMap, HashSet}, @@ -26,11 +27,27 @@ async fn main() -> Result<()> { .with(tracing_subscriber::fmt::layer()) .init(); + // Handle command-line args + let matches = command!() + .arg( + arg!(-b --bind "Bind to specific address. Default is 127.0.0.1:3030") + .required(false), + ) + .get_matches(); + + let bind_addr; + + if let Some(addr) = matches.get_one::("bind") { + bind_addr = String::from(addr); + } else { + bind_addr = String::from("127.0.0.1:3030"); + } + // Set up state - let (broadcast_tx, _rx) = broadcast::channel(100); - let (users_tx, mut users_rx) = mpsc::channel(100); - let (messages_tx, mut messages_rx) = mpsc::channel(100); - let (games_tx, mut games_rx) = mpsc::channel(100); + let (broadcast_tx, _rx) = broadcast::channel(1000); + let (users_tx, mut users_rx) = mpsc::channel(1000); + let (messages_tx, mut messages_rx) = mpsc::channel(1000); + let (games_tx, mut games_rx) = mpsc::channel(1000); let first_names = load_names("data/first.txt")?; let last_names = load_names("data/last.txt")?; let reserved_names = RwLock::new(HashSet::::new()); @@ -94,10 +111,14 @@ async fn main() -> Result<()> { .with_state(app_state); // send it - let address = "127.0.0.1:3030"; - let listener = tokio::net::TcpListener::bind(address) + let listener = tokio::net::TcpListener::bind(bind_addr.to_owned()) .await - .with_context(|| format!("{} is not a valid bind address.", address))?; + .with_context(|| { + format!( + "{} is not a valid socket address. Did you forget to specify a port?", + bind_addr + ) + })?; tracing::info!("listening on {}", listener.local_addr()?); axum::serve( listener, diff --git a/test b/test index ac04955..d2a2cfc 100755 --- a/test +++ b/test @@ -1,3 +1,3 @@ #!/bin/sh -cargo watch -i client/ -i tools/ -cx run +cargo watch -i client/ -i tools/ -cx "run -p server -- $1 $2"