add argument to bind server to specific address

This commit is contained in:
Adam 2024-10-09 20:39:48 -04:00
parent 43e312445b
commit b3b9cd92af
5 changed files with 36 additions and 11 deletions

7
Cargo.lock generated
View file

@ -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",

View file

@ -20,6 +20,8 @@ Then:
./test
```
Use `-b <ADDR>` or `--bind <ADDR>` 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

View file

@ -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"] }

View file

@ -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 <ADDR> "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::<String>("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::<String>::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,

2
test
View file

@ -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"