Feat: Added IP_ADDR args to easily set IP address

- Fixed documentation
This commit is contained in:
minhtrannhat 2022-08-18 20:06:12 -04:00
parent 756e934e93
commit aea63234a1
No known key found for this signature in database
GPG Key ID: 894C6A5801E01CA9
2 changed files with 19 additions and 9 deletions

View File

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

View File

@ -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<String> = 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()?;