SeaBattle/rust/sea_battle_cli_player/src/server.rs
2022-10-17 19:13:16 +02:00

39 lines
1.0 KiB
Rust

use tokio::runtime::Builder;
use tokio::task;
use sea_battle_backend::args::Args;
use sea_battle_backend::utils::network_utils;
use crate::cli_args::cli_args;
pub async fn run_server() {
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
sea_battle_backend::server::start_server(Args {
listen_address: cli_args().listen_address.clone(),
cors: None,
})
.await
.expect("Failed to run local server!")
})
.await;
}
pub async fn start_server_if_missing() {
if !network_utils::is_port_open(cli_args().listen_port()).await {
log::info!(
"Local server will start on {}...",
cli_args().listen_address
);
std::thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(run_server());
});
network_utils::wait_for_port(cli_args().listen_port()).await;
}
}