diff --git a/services/backup/src/config.rs b/services/backup/src/config.rs new file mode 100644 --- /dev/null +++ b/services/backup/src/config.rs @@ -0,0 +1,38 @@ +use clap::{builder::FalseyValueParser, Parser}; +use once_cell::sync::Lazy; + +use crate::constants::{ + DEFAULT_BLOB_SERVICE_URL, DEFAULT_GRPC_SERVER_PORT, DEFAULT_LOCALSTACK_URL, + SANDBOX_ENV_VAR, +}; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +pub struct AppConfig { + /// gRPC server listening port + #[arg(long = "port", default_value_t = DEFAULT_GRPC_SERVER_PORT)] + pub listening_port: u64, + /// Run the service in sandbox + #[arg(long = "sandbox", default_value_t = false)] + // support the env var for compatibility reasons + #[arg(env = SANDBOX_ENV_VAR)] + #[arg(value_parser = FalseyValueParser::new())] + pub is_sandbox: bool, + /// AWS Localstack service URL, applicable in sandbox mode + #[arg(long, default_value_t = DEFAULT_LOCALSTACK_URL.to_string())] + pub localstack_url: String, + /// Blob service URL + #[arg(long, default_value_t = DEFAULT_BLOB_SERVICE_URL.to_string())] + pub blob_service_url: String, +} + +/// Stores configuration parsed from command-line arguments +/// and environment variables +pub static CONFIG: Lazy = Lazy::new(|| AppConfig::parse()); + +/// Processes the command-line arguments and environment variables. +/// Should be called at the beginning of the `main()` function. +pub(super) fn parse_cmdline_args() { + // force evaluation of the lazy initialized config + Lazy::force(&CONFIG); +} diff --git a/services/backup/src/constants.rs b/services/backup/src/constants.rs --- a/services/backup/src/constants.rs +++ b/services/backup/src/constants.rs @@ -1,3 +1,11 @@ +// Configuration defaults + pub const DEFAULT_GRPC_SERVER_PORT: u64 = 50051; +pub const DEFAULT_LOCALSTACK_URL: &str = "http://localhost:4566"; +pub const DEFAULT_BLOB_SERVICE_URL: &str = "http://localhost:50053"; + +// Environment variable names + +pub const SANDBOX_ENV_VAR: &str = "COMM_SERVICES_SANDBOX"; pub const LOG_LEVEL_ENV_VAR: &str = tracing_subscriber::filter::EnvFilter::DEFAULT_ENV; diff --git a/services/backup/src/main.rs b/services/backup/src/main.rs --- a/services/backup/src/main.rs +++ b/services/backup/src/main.rs @@ -6,9 +6,13 @@ use crate::service::{BackupServiceServer, MyBackupService}; +pub mod config; pub mod constants; pub mod service; +// re-export this to be available as crate::CONFIG +pub use config::CONFIG; + fn configure_logging() -> Result<()> { let filter = EnvFilter::builder() .with_default_directive(Level::INFO.into()) @@ -21,8 +25,7 @@ } async fn run_grpc_server() -> Result<()> { - let addr: SocketAddr = - format!("[::]:{}", constants::DEFAULT_GRPC_SERVER_PORT).parse()?; + let addr: SocketAddr = format!("[::]:{}", CONFIG.listening_port).parse()?; let backup_service = MyBackupService::default(); info!("Starting gRPC server listening at {}", addr.to_string()); @@ -36,6 +39,7 @@ #[tokio::main] async fn main() -> Result<()> { + config::parse_cmdline_args(); configure_logging()?; run_grpc_server().await