http_server_rs/
lib.rs

1use std::{thread, time::Duration};
2
3use connectionthreadpool::ThreadPool;
4use httpconstants::HttpConstants;
5use httpserver::HttpServer;
6
7pub mod connectionthreadpool;
8pub mod httpreader;
9pub mod httpconstants;
10pub mod httpserver;
11pub mod httperror;
12pub mod executor;
13
14fn main() {
15    println!("{:?}", HttpConstants::get_current_formatted_date());
16    
17
18    let mut http_server : HttpServer<ThreadPool> = HttpServer::new(10000, ThreadPool::new(16));
19    
20    http_server.add_endpoint("/", | mut reader | {
21        let mut vec_response : Vec<u8> = Vec::new();
22        vec_response.append(&mut "<h1>Hi from http</h1>this thing works".to_string().into_bytes());
23        reader.write_response_headers(200, vec_response.len()).unwrap_or_default();
24        reader.write_response_body(vec_response.as_slice()).unwrap_or_default(); 
25    });
26    
27    http_server.add_endpoint("/sleep", | mut reader | {
28        thread::sleep(Duration::from_secs(5));
29        let mut vec_response : Vec<u8> = Vec::new();
30        vec_response.append(&mut "<h1>Hi from http</h1>i woke up from my sleep".to_string().into_bytes());
31        reader.write_response_headers(200, vec_response.len()).unwrap_or_default();
32        reader.write_response_body(vec_response.as_slice()).unwrap_or_default(); 
33    });
34    
35    http_server.start("0.0.0.0:9000").unwrap();
36    
37}