This commit is contained in:
parent
8990badaa4
commit
9a38a634eb
@ -28,6 +28,10 @@ pub struct CliArgs {
|
|||||||
|
|
||||||
#[clap(long, value_enum)]
|
#[clap(long, value_enum)]
|
||||||
pub dev_screen: Option<TestDevScreen>,
|
pub dev_screen: Option<TestDevScreen>,
|
||||||
|
|
||||||
|
/// Run as server instead of as client
|
||||||
|
#[clap(long, short)]
|
||||||
|
pub serve: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CliArgs {
|
impl CliArgs {
|
||||||
|
@ -14,6 +14,7 @@ use tui::Terminal;
|
|||||||
|
|
||||||
use cli_player::cli_args::{cli_args, TestDevScreen};
|
use cli_player::cli_args::{cli_args, TestDevScreen};
|
||||||
use cli_player::client::Client;
|
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::configure_game_rules::GameRulesConfigurationScreen;
|
||||||
use cli_player::ui_screens::game_screen::GameScreen;
|
use cli_player::ui_screens::game_screen::GameScreen;
|
||||||
use cli_player::ui_screens::input_screen::InputScreen;
|
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]
|
#[tokio::main]
|
||||||
pub async fn main() -> Result<(), Box<dyn Error>> {
|
pub async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
env_logger::Builder::from_env(Env::default()).init();
|
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
|
// setup terminal
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
|
@ -6,6 +6,21 @@ use sea_battle_backend::utils::network_utils;
|
|||||||
|
|
||||||
use crate::cli_args::cli_args;
|
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() {
|
pub async fn start_server_if_missing() {
|
||||||
if !network_utils::is_port_open(cli_args().listen_port()).await {
|
if !network_utils::is_port_open(cli_args().listen_port()).await {
|
||||||
log::info!(
|
log::info!(
|
||||||
@ -15,16 +30,7 @@ pub async fn start_server_if_missing() {
|
|||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let rt = Builder::new_current_thread().enable_all().build().unwrap();
|
let rt = Builder::new_current_thread().enable_all().build().unwrap();
|
||||||
|
|
||||||
let local_set = task::LocalSet::new();
|
rt.block_on(run_server());
|
||||||
|
|
||||||
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!")
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
network_utils::wait_for_port(cli_args().listen_port()).await;
|
network_utils::wait_for_port(cli_args().listen_port()).await;
|
||||||
|
Loading…
Reference in New Issue
Block a user