2022-10-01 18:44:01 +00:00
|
|
|
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;
|
|
|
|
|
2022-10-16 17:52:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-01 18:44:01 +00:00
|
|
|
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();
|
|
|
|
|
2022-10-16 17:52:34 +00:00
|
|
|
rt.block_on(run_server());
|
2022-10-01 18:44:01 +00:00
|
|
|
});
|
2022-10-16 16:06:31 +00:00
|
|
|
|
|
|
|
network_utils::wait_for_port(cli_args().listen_port()).await;
|
2022-10-01 18:44:01 +00:00
|
|
|
}
|
|
|
|
}
|