diff --git a/src/main.rs b/src/main.rs index f8fadbb..44432f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,15 @@ pub mod error; pub mod globals; pub mod store; pub mod types; +pub mod web; use crate::config::Config; use crate::error::Error; use crate::globals::GLOBALS; use crate::store::Store; +use hyper::{Body, Request, Response}; use std::env; +use std::error::Error as StdError; use std::fs::OpenOptions; use std::io::Read; use tokio::net::TcpListener; @@ -39,13 +42,44 @@ async fn main() -> Result<(), Error> { let _ = GLOBALS.store.set(store); // Bind listener to port - let _listener = TcpListener::bind((&*config.ip_address, config.port)).await?; + let listener = TcpListener::bind((&*config.ip_address, config.port)).await?; log::info!("Running on {}:{}", config.ip_address, config.port); // Store config into GLOBALS *GLOBALS.config.write().await = config; - log::error!("No main yet."); + let mut http_server = hyper::server::conn::Http::new(); + http_server.http1_only(true); + http_server.http1_keep_alive(true); - Ok(()) + // Accepts network connections and spawn a task to serve each one + loop { + let (tcp_stream, peer_addr) = listener.accept().await?; + + let connection = + http_server.serve_connection(tcp_stream, hyper::service::service_fn(handle_request)); + + tokio::spawn(async move { + // If our service exits with an error, log the error + if let Err(he) = connection.await { + if let Some(src) = he.source() { + if &*format!("{}", src) == "Transport endpoint is not connected (os error 107)" + { + // do nothing + } else { + // Print in detail + log::info!("{:?}", src); + } + } else { + // Print in less detail + let e: Error = he.into(); + log::info!("{}", e); + } + } + }); + } +} + +async fn handle_request(_request: Request
) -> Result