little_hyper/
little_hyper.rsuse crate::{Error, Logger, Request, Response, Result, Router};
use std::io::{prelude::*, BufReader};
use std::net::{TcpListener, TcpStream, ToSocketAddrs};
#[derive(Default)]
pub struct LittleServer {
logger: Logger,
router: Router,
}
impl LittleServer {
pub fn new(log: bool) -> Self {
Self {
logger: Logger::new(log),
..Default::default()
}
}
pub fn add_router(&mut self, router: Router) {
self.router.merge_router(router);
}
fn handle_connection(&self, stream: TcpStream) {
let buffer = self.read_stream_info(&stream);
if buffer.is_err() {
return;
}
let mut request = Request::from(buffer.unwrap());
let mut response = Response::new(stream);
self.logger.request(&request);
if let Some(handler) = self.router.get_handler(&mut request) {
handler(&mut request, &mut response);
} else {
}
}
fn read_stream_info(&self, stream: &TcpStream) -> Result<Vec<String>> {
let buffer = BufReader::new(stream)
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect::<Vec<String>>();
if buffer.is_empty() {
return Err(Error::ReadStreamInfo);
}
Ok(buffer)
}
pub fn listen<A: ToSocketAddrs>(&self, address: A) -> Result<()> {
let listener = TcpListener::bind(address).expect("ERROR: Listen");
for stream in listener.incoming() {
let stream = stream.unwrap();
self.handle_connection(stream);
}
Ok(())
}
}