[feat] use clap to pass IP and port

This commit is contained in:
2024-11-08 22:38:11 +01:00
parent 9d8f579a0f
commit 9f63ace39c
8 changed files with 180 additions and 30 deletions

View File

@ -19,6 +19,7 @@ lanspread-utils = { path = "../lanspread-utils" }
# external
bytes = { workspace = true }
clap = { workspace = true }
eyre = { workspace = true }
itertools = { workspace = true }
s2n-quic = { workspace = true }

View File

@ -1,9 +1,10 @@
use std::{
net::SocketAddr,
net::{IpAddr, SocketAddr},
path::{Path, PathBuf},
sync::Arc,
};
use clap::Parser;
use lanspread_db::db::GameDB;
use lanspread_proto::{Message as _, Request, Response};
use lanspread_utils::maybe_addr;
@ -17,9 +18,6 @@ mod testing;
static KEY_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../key.pem"));
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
const SERVER_ADDR: &str = "0.0.0.0";
const SERVER_PORT: u16 = 13337;
pub(crate) struct Server {
db_path: PathBuf,
}
@ -162,14 +160,22 @@ impl RequestHandler {
const GAME_DB_PATH: &str = "/home/pfs/shm/game.db";
#[derive(Debug, Parser)]
struct Cli {
/// The IP address to bind to.
#[clap(long, default_value = "127.0.0.1")]
ip: IpAddr,
/// The listen port.
#[clap(long, default_value = "13337")]
port: u16,
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
let cli = Cli::parse();
generate_test_db(GAME_DB_PATH);
let mut server = Server::new(GAME_DB_PATH);
#[allow(clippy::unwrap_used)]
server
.run(format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap())
.await
server.run(SocketAddr::from((cli.ip, cli.port))).await
}