diff --git a/src/backend/Cargo.lock b/src/backend/Cargo.lock index 1f2710b4..1e144c9c 100644 --- a/src/backend/Cargo.lock +++ b/src/backend/Cargo.lock @@ -365,6 +365,7 @@ dependencies = [ "lazy_static", "log", "nanoserde", + "num_cpus", "pty-process", "simple_logger", "tokio", diff --git a/src/backend/Cargo.toml b/src/backend/Cargo.toml index c2b7129b..8f9a7d6f 100644 --- a/src/backend/Cargo.toml +++ b/src/backend/Cargo.toml @@ -7,7 +7,8 @@ edition = "2021" [dependencies] warp = {version = "0.3.1", default-features = false, features = ["compression", "websocket", "tls"]} -tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] } +tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "sync"] } +num_cpus = "1.13.0" simple_logger = "1.13.0" log = "0.4.14" include_dir = "0.6.2" @@ -22,3 +23,4 @@ infer = { version = "0.5.0", default-features = false } lto = "fat" panic = "abort" codegen-units = 1 +incremental = false diff --git a/src/backend/src/main.rs b/src/backend/src/main.rs index a0b441ba..132bf045 100644 --- a/src/backend/src/main.rs +++ b/src/backend/src/main.rs @@ -9,82 +9,89 @@ mod systemdata; mod terminal; mod types; -#[tokio::main] -async fn main() { - const DIR: include_dir::Dir = include_dir::include_dir!("dist"); +fn main() { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(num_cpus::get().max(2)) // We have to use num_cpus because heim is async, and the runtime hasn't been started yet. Minimum of 2 threads. + .enable_all() + .build() + .unwrap() + .block_on(async { + const DIR: include_dir::Dir = include_dir::include_dir!("dist"); - let cfg = config::config(); + let cfg = config::config(); - SimpleLogger::new() - .with_level(log::LevelFilter::Info) - .env() - .init() - .unwrap(); + SimpleLogger::new() + .with_level(log::LevelFilter::Info) + .env() + .init() + .unwrap(); - let favicon_route = warp::path("favicon.png").map(|| { - warp::reply::with_header( - DIR.get_file("favicon.png").unwrap().contents(), - "content-type", - "image/png", - ) - }); + let favicon_route = warp::path("favicon.png").map(|| { + warp::reply::with_header( + DIR.get_file("favicon.png").unwrap().contents(), + "content-type", + "image/png", + ) + }); - let assets_route = warp::path("assets") - .and(warp::path::param()) - .map(|path: String| { - warp::reply::with_header( - DIR.get_file(format!("assets/{}", path)).unwrap().contents(), - "content-type", - format!( - "text/{}", - if path.rsplit('.').next().unwrap() == "js" { - "javascript" - } else { - path.rsplit('.').next().unwrap() - } - ), - ) - }); + let assets_route = warp::path("assets") + .and(warp::path::param()) + .map(|path: String| { + warp::reply::with_header( + DIR.get_file(format!("assets/{}", path)).unwrap().contents(), + "content-type", + format!( + "text/{}", + if path.rsplit('.').next().unwrap() == "js" { + "javascript" + } else { + path.rsplit('.').next().unwrap() + } + ), + ) + }); - let terminal_route = warp::path!("ws" / "term") - .and(warp::ws()) - .map(|ws: warp::ws::Ws| ws.on_upgrade(terminal::term_handler)); + let terminal_route = warp::path!("ws" / "term") + .and(warp::ws()) + .map(|ws: warp::ws::Ws| ws.on_upgrade(terminal::term_handler)); - let socket_route = warp::path("ws") - .and(warp::ws()) - .map(|ws: warp::ws::Ws| ws.on_upgrade(sockets::socket_handler)); + let socket_route = warp::path("ws") + .and(warp::ws()) + .map(|ws: warp::ws::Ws| ws.on_upgrade(sockets::socket_handler)); - let main_route = warp::any() - .map(|| warp::reply::html(DIR.get_file("index.html").unwrap().contents_utf8().unwrap())); + let main_route = warp::any().map(|| { + warp::reply::html(DIR.get_file("index.html").unwrap().contents_utf8().unwrap()) + }); - let page_routes = favicon_route - .or(assets_route) - .or(main_route) - .with(warp::compression::gzip()); + let page_routes = favicon_route + .or(assets_route) + .or(main_route) + .with(warp::compression::gzip()); - let socket_routes = terminal_route.or(socket_route); + let socket_routes = terminal_route.or(socket_route); - let routes = socket_routes - .or(page_routes) - .with(warp::log::custom(|info| { - log::info!("Request to {}", info.path()); - log::debug!( - "by {}, using {} {:?}, with response of HTTP code {:?}", - info.remote_addr().unwrap().ip(), - info.user_agent().unwrap(), - info.version(), - info.status() - ); - })); + let routes = socket_routes + .or(page_routes) + .with(warp::log::custom(|info| { + log::info!("Request to {}", info.path()); + log::debug!( + "by {}, using {} {:?}, with response of HTTP code {:?}", + info.remote_addr().unwrap().ip(), + info.user_agent().unwrap(), + info.version(), + info.status() + ); + })); - if cfg.tls { - warp::serve(routes) - .tls() - .cert_path(cfg.cert) - .key_path(cfg.key) - .run(([0, 0, 0, 0], cfg.port)) - .await; - } else { - warp::serve(routes).run(([0, 0, 0, 0], cfg.port)).await; - } + if cfg.tls { + warp::serve(routes) + .tls() + .cert_path(cfg.cert) + .key_path(cfg.key) + .run(([0, 0, 0, 0], cfg.port)) + .await; + } else { + warp::serve(routes).run(([0, 0, 0, 0], cfg.port)).await; + } + }); }