diff --git a/services/backup/src/config.rs b/services/backup/src/config.rs new file mode 100644 index 000000000..3294aec07 --- /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 index 3497b91a7..6ed661e80 100644 --- 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 index 61ff25338..3c3a62658 100644 --- a/services/backup/src/main.rs +++ b/services/backup/src/main.rs @@ -1,42 +1,46 @@ use anyhow::Result; use std::net::SocketAddr; use tonic::transport::Server; use tracing::{info, Level}; use tracing_subscriber::EnvFilter; 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()) .with_env_var(constants::LOG_LEVEL_ENV_VAR) .from_env_lossy(); let subscriber = tracing_subscriber::fmt().with_env_filter(filter).finish(); tracing::subscriber::set_global_default(subscriber)?; Ok(()) } 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()); Server::builder() .add_service(BackupServiceServer::new(backup_service)) .serve(addr) .await?; Ok(()) } #[tokio::main] async fn main() -> Result<()> { + config::parse_cmdline_args(); configure_logging()?; run_grpc_server().await }