Can run cli as server
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2022-10-16 19:52:34 +02:00
parent 8990badaa4
commit 9a38a634eb
3 changed files with 26 additions and 11 deletions

View File

@ -28,6 +28,10 @@ pub struct CliArgs {
#[clap(long, value_enum)]
pub dev_screen: Option<TestDevScreen>,
/// Run as server instead of as client
#[clap(long, short)]
pub serve: bool,
}
impl CliArgs {

View File

@ -14,6 +14,7 @@ use tui::Terminal;
use cli_player::cli_args::{cli_args, TestDevScreen};
use cli_player::client::Client;
use cli_player::server::run_server;
use cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen;
use cli_player::ui_screens::game_screen::GameScreen;
use cli_player::ui_screens::input_screen::InputScreen;
@ -138,7 +139,11 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default()).init();
cli_args(); // Avoid a crash if help argument is triggered
if cli_args().serve {
run_server().await;
return Ok(());
}
// setup terminal
enable_raw_mode()?;

View File

@ -6,6 +6,21 @@ 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!(
@ -15,16 +30,7 @@ pub async fn start_server_if_missing() {
std::thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().unwrap();
let local_set = task::LocalSet::new();
rt.block_on(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!")
}));
rt.block_on(run_server());
});
network_utils::wait_for_port(cli_args().listen_port()).await;