Skip to content

Commit

Permalink
fix(backend): fix terminal on single-core machines
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenclaw900 committed Nov 8, 2021
1 parent f1c96ab commit 122c38b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,3 +23,4 @@ infer = { version = "0.5.0", default-features = false }
lto = "fat"
panic = "abort"
codegen-units = 1
incremental = false
141 changes: 74 additions & 67 deletions src/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}

0 comments on commit 122c38b

Please sign in to comment.