use std::io; use std::net::TcpListener; use std::net::TcpStream; use std::thread; use rusty_http::Http; use rusty_http::Request; use rusty_http::Response; use rusty_http::StatusCode; const DIALOG_A_NOT_FOUND: &str = "
Parameter 'a' not found
"; const DIALOG_B_NOT_FOUND: &str = "Parameter 'b' not found
"; const DIALOG_A_INVALID: &str = "Parameter 'a' can only be a number from 0 to 255
"; const DIALOG_B_INVALID: &str = "Parameter 'b' can only be a number from 0 to 255
"; fn add(request: Request, response: Response) -> io::Result<()> { let Some(a) = request.params.get("a") else { response.status_code(StatusCode::BadRequest) .html(&DIALOG_A_NOT_FOUND)?; return Ok(()); }; let Some(b) = request.params.get("b") else { response.status_code(StatusCode::BadRequest) .html(&DIALOG_B_NOT_FOUND)?; return Ok(()); }; let Ok(a) = a.parse::Page '{}' could not been found
", request.uri); response.status_code(StatusCode::NotFound) .html(content)?; Ok(()) } fn init_stream(stream: TcpStream) -> io::Result<()> { let (request, response) = Http::from(stream)?; match request.uri.as_str() { "/add" => add(request, response)?, "/sub" => sub(request, response)?, _ => not_found(request, response)? } Ok(()) } fn main() -> io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; for stream in listener.incoming().filter_map(Result::ok) { thread::spawn(|| init_stream(stream)); } Ok(()) }