Feat: Added IP_ADDR args to easily set IP address
- Fixed documentation
This commit is contained in:
		
							
								
								
									
										19
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								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<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()?;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user