10 changed files with 116 additions and 195 deletions
@ -1,81 +0,0 @@ |
|||||||
use crate::commands::{ClapArgs, CommandExecutor, ConfigurableCommand}; |
|
||||||
use crate::config::AppConfig; |
|
||||||
use crate::container::Container; |
|
||||||
|
|
||||||
use crate::defaults; |
|
||||||
use anyhow::Result; |
|
||||||
use async_trait::async_trait; |
|
||||||
use config::ConfigBuilder; |
|
||||||
use config::builder::DefaultState; |
|
||||||
use serde::Deserialize; |
|
||||||
use tokio::signal; |
|
||||||
use tracing::{info, warn}; |
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)] |
|
||||||
pub struct Config { |
|
||||||
pub port: u16, |
|
||||||
} |
|
||||||
|
|
||||||
#[derive(ClapArgs, Debug, Clone)] |
|
||||||
pub struct ServerArgs { |
|
||||||
#[arg(short, long, help = defaults::HELP_PORT)] |
|
||||||
pub port: Option<u16>, |
|
||||||
} |
|
||||||
|
|
||||||
impl ConfigurableCommand for ServerArgs { |
|
||||||
fn apply_defaults( |
|
||||||
&self, |
|
||||||
builder: ConfigBuilder<DefaultState>, |
|
||||||
) -> Result<ConfigBuilder<DefaultState>> { |
|
||||||
builder |
|
||||||
.set_default("server.host", defaults::HOST)? |
|
||||||
.set_default("server.port", defaults::PORT) |
|
||||||
.map_err(Into::into) |
|
||||||
} |
|
||||||
|
|
||||||
fn apply_overrides( |
|
||||||
&self, |
|
||||||
builder: ConfigBuilder<DefaultState>, |
|
||||||
) -> Result<ConfigBuilder<DefaultState>> { |
|
||||||
let mut builder = builder; |
|
||||||
if let Some(port) = self.port { |
|
||||||
builder = builder.set_override("server.port", port)?; |
|
||||||
} |
|
||||||
Ok(builder) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#[async_trait] |
|
||||||
impl CommandExecutor for ServerArgs { |
|
||||||
async fn execute(&self, config: &AppConfig, _container: &Container) -> Result<()> { |
|
||||||
let config = config.server.as_ref().expect("Server config not set"); |
|
||||||
|
|
||||||
info!("Running server with config: {:#?}", config); |
|
||||||
tokio::select! { |
|
||||||
_ = server_loop() => {}, |
|
||||||
_ = wait_for_shutdown_signal() => { |
|
||||||
info!("Shutting down server..."); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
Ok(()) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
async fn wait_for_shutdown_signal() { |
|
||||||
match signal::ctrl_c().await { |
|
||||||
Ok(()) => { |
|
||||||
info!("Received shutdown signal (SIGINT/SIGTERM)"); |
|
||||||
} |
|
||||||
Err(err) => { |
|
||||||
warn!("Failed to listen for shutdown signal: {}", err); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
async fn server_loop() { |
|
||||||
loop { |
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; |
|
||||||
info!("Health check... "); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,13 +0,0 @@ |
|||||||
pub const HOST: &str = "127.0.0.1"; |
|
||||||
pub const PORT: u16 = 8080; |
|
||||||
pub const LOG_LEVEL: &str = "info"; |
|
||||||
pub const IMPORT_DICT_NAME: &str = ""; |
|
||||||
pub const IMPORT_DICT_PATH: &str = ""; |
|
||||||
|
|
||||||
use const_format::formatcp; |
|
||||||
|
|
||||||
pub const HELP_PORT: &str = formatcp!("Override Port [default: {}]", PORT); |
|
||||||
pub const HELP_LOG: &str = formatcp!("Override Log Level [default: {}]", LOG_LEVEL); |
|
||||||
|
|
||||||
pub const HELP_IMPORT_DICT_NAME: &str = formatcp!("Dictionary name"); |
|
||||||
pub const HELP_IMPORT_DICT_INPUT: &str = formatcp!("Dictionary file path"); |
|
||||||
Loading…
Reference in new issue