first commit

This commit is contained in:
Andrea Moro 2024-11-24 13:09:14 +01:00
commit 6b09029cfe
4 changed files with 779 additions and 0 deletions

56
src/main.rs Normal file
View file

@ -0,0 +1,56 @@
use log::{debug, error, info, trace, warn};
use std::env::args;
use std::net::{TcpListener, TcpStream};
#[derive(Debug)]
struct Settings {
host: String,
port: String,
}
impl Settings {
fn new(args: &[String]) -> Result<Settings, &'static str> {
if args.len() < 4 {
return Err("not enough arguments");
}
let port = args[2].clone();
let host = args[4].clone();
Ok(Settings { host, port })
}
fn get_full_host(&self) -> String {
format!("{}:{}", self.host, self.port)
}
}
async fn handle_client(mut stream: TcpStream) {
info!("Connected to {}", stream.peer_addr().unwrap());
stream.set_nodelay(true).unwrap();
loop {
}
}
#[tokio::main]
async fn main() {
colog::init();
info!("Starting...");
let args: Vec<String> = args().collect();
let settings = Settings::new(&args).unwrap();
info!("Server Address: {}:{}", settings.host, settings.port);
info!("Starting to listen to connections...");
let listener = TcpListener::bind(Settings::get_full_host(&settings)).unwrap();
match listener.accept() {
Ok((socket, _)) => {
tokio::spawn(async move {
handle_client(socket).await;
});
}
Err(e) => {
error!("Something went wrong {}", e);
}
}
}