From aea63234a10abe9b16c300414f280a6d5544cb0e Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Thu, 18 Aug 2022 20:06:12 -0400 Subject: [PATCH] Feat: Added IP_ADDR args to easily set IP address - Fixed documentation --- README.md | 9 +++++++-- src/main.rs | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0adff40..842e23e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,12 @@ A wannabe discord clone but currently only support websocket real time chatting - Run `cargo build` at project root to build the binary. ## Running -- Run the server with `cargo run` or `RUST_LOG=info cargo run` to run with debug. +### Development +- Run the server with `cargo run` or `RUST_LOG=debug cargo run` to run with debug. +### Production +- Run the binary with `minh_cord {LOCAL_IP_ADDRESS}`. ## Connecting to the server -- If server is running on localhost, run `telnet 127.0.0.1 8080` to connect to the server. +- Get your local IP address of the machine you're running the server on: i.e `192.168.0.100` +- Run the server with the above commands. +- Run `telnet 192.168.0.100` from a client to connect to the server. diff --git a/src/main.rs b/src/main.rs index 2e949cd..061d83c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,17 +4,22 @@ use tokio::{ net::TcpListener, sync::broadcast, }; -use log::info; +use log::debug; +use std::env; #[tokio::main] async fn main() -> Result<(), Report> { + let args: Vec = env::args().collect(); + env_logger::init(); // Setup the Environment setup()?; - // Set up a TCP listener to listen for incoming tcp requests - let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); - info!("Bounded to localhost port 8080"); + let ip_address = &args[1]; + + // Set up a TCP listener to listen for incoming tcp requests at the ip address that is passed from the command line + let listener = TcpListener::bind(format!("{ip_address}:8080")).await.unwrap(); + debug!("{}", format!("Bounded to {ip_address} port 8080")); // Setting up broadcast channel: A channel that will accept messages and broadcast them to everyone connected to the TCP server // the channel accepts an i32 that is the maximum number of messages the channel can retain at any given time @@ -28,7 +33,7 @@ async fn main() -> Result<(), Report> { // accept the requests let (mut socket, addr) = listener.accept().await.unwrap(); - info!("Accepted a websocket request from {}", addr); + debug!("Accepted a websocket request from {}", addr); tokio::spawn(async move { // Splitting the TCP socket into read/write halves @@ -69,11 +74,11 @@ async fn main() -> Result<(), Report> { } fn setup() -> Result<(), Report> { - info!("Changing some environment variables!"); + debug!("Changing some environment variables!"); if std::env::var("RUST_LIB_BACKTRACE").is_err() { std::env::set_var("RUST_LIB_BACKTRACE", "1"); - info!("RUST_LIB_BACKTRACE is set. There will be pretty error messages !") + debug!("RUST_LIB_BACKTRACE is set. There will be pretty error messages !") } color_eyre::install()?;