Put Http server into GLOBALS

This commit is contained in:
Mike Dilger 2023-10-26 20:04:18 +13:00
parent 445cc89530
commit 259883d2b7
2 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,6 @@
use crate::config::Config;
use crate::store::Store;
use hyper::server::conn::Http;
use lazy_static::lazy_static;
use std::sync::OnceLock;
use tokio::sync::RwLock;
@ -7,13 +8,19 @@ use tokio::sync::RwLock;
pub struct Globals {
pub config: RwLock<Config>,
pub store: OnceLock<Store>,
pub http_server: Http,
}
lazy_static! {
pub static ref GLOBALS: Globals = {
let mut http_server = hyper::server::conn::Http::new();
http_server.http1_only(true);
http_server.http1_keep_alive(true);
Globals {
config: RwLock::new(Config::default()),
store: OnceLock::new(),
http_server,
}
};
}

View File

@ -81,21 +81,17 @@ async fn main() -> Result<(), Error> {
// Store config into GLOBALS
*GLOBALS.config.write().await = config;
let mut http_server = hyper::server::conn::Http::new();
http_server.http1_only(true);
http_server.http1_keep_alive(true);
// Accepts network connections and spawn a task to serve each one
loop {
let (tcp_stream, peer_addr) = listener.accept().await?;
let acceptor = tls_acceptor.clone();
let http_server_clone = http_server.clone();
tokio::spawn(async move {
match acceptor.accept(tcp_stream).await {
Err(e) => log::error!("{}", e),
Ok(tls_stream) => {
let connection = http_server_clone
let connection = GLOBALS
.http_server
.serve_connection(tls_stream, hyper::service::service_fn(handle_request));
tokio::spawn(async move {
// If our service exits with an error, log the error