diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs index 65d52df..5f3b8c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -use hyper::service::{make_service_fn, service_fn}; -use hyper::Server; +use hyper::server::conn::http1; +use hyper::service::service_fn; +use hyper_util::rt::TokioIo; use sqlx::sqlite::SqlitePool; -use std::convert::Infallible; +use tokio::net::TcpListener; #[macro_use] extern crate log; @@ -23,7 +24,7 @@ mod validation; use model::config; #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { env_logger::Builder::from_env( env_logger::Env::default().default_filter_or("warn"), ) @@ -42,26 +43,36 @@ async fn main() { tokio::spawn(jobs::start(config.clone(), pool.clone(), templates.clone())); - let make_svc = make_service_fn(|_conn| { + let listener = TcpListener::bind(config.socket_address).await?; + info!("Starting server at {}", config.socket_address); + + loop { let config = config.clone(); let pool = pool.clone(); let assets = assets.clone(); let templates = templates.clone(); - async move { - Ok::<_, Infallible>(service_fn(move |req| { - routes::routes( - config.clone(), - pool.clone(), - assets.clone(), - templates.clone(), - req, - ) - })) - } - }); + let (stream, _) = listener.accept().await?; - info!("Starting server at {}", config.socket_address); - if let Err(e) = Server::bind(&config.socket_address).serve(make_svc).await { - error!("server error: {}", e); + let io = TokioIo::new(stream); + + tokio::task::spawn(async move { + if let Err(err) = http1::Builder::new() + .serve_connection( + io, + service_fn(move |req| { + routes::routes( + config.clone(), + pool.clone(), + assets.clone(), + templates.clone(), + req, + ) + }), + ) + .await + { + println!("Error serving connection: {:?}", err); + } + }); } } |