diff --git a/services/tunnelbroker/src/config.rs b/services/tunnelbroker/src/config.rs index 60967e25d..c84be29a6 100644 --- a/services/tunnelbroker/src/config.rs +++ b/services/tunnelbroker/src/config.rs @@ -1,66 +1,71 @@ use crate::constants; -use crate::constants::ENV_APNS_CONFIG; +use crate::constants::{ENV_APNS_CONFIG, ENV_FCM_CONFIG}; use crate::notifs::apns::config::APNsConfig; +use crate::notifs::fcm::config::FCMConfig; use anyhow::{ensure, Result}; use clap::Parser; use comm_lib::aws; use once_cell::sync::Lazy; use tracing::info; #[derive(Parser)] #[command(version, about, long_about = None)] pub struct AppConfig { /// gRPC server listening port #[arg(long, default_value_t = constants::GRPC_SERVER_PORT)] pub grpc_port: u16, /// HTTP server listening port #[arg(long, default_value_t = 51001)] pub http_port: u16, /// AMQP server URI #[arg(env = "AMQP_URI")] #[arg(long, default_value = "amqp://comm:comm@localhost:5672")] pub amqp_uri: String, /// AWS Localstack service URL #[arg(env = "LOCALSTACK_ENDPOINT")] #[arg(long)] pub localstack_endpoint: Option, /// Comm Identity service URL #[arg(env = "COMM_TUNNELBROKER_IDENTITY_ENDPOINT")] #[arg(long, default_value = "http://localhost:50054")] pub identity_endpoint: String, /// APNs secrets #[arg(env = ENV_APNS_CONFIG)] #[arg(long)] pub apns_config: Option, + /// FCM secrets + #[arg(env = ENV_FCM_CONFIG)] + #[arg(long)] + pub fcm_config: Option, } /// 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() -> Result<()> { // force evaluation of the lazy initialized config let cfg = Lazy::force(&CONFIG); // Perform some additional validation for CLI args ensure!( cfg.grpc_port != cfg.http_port, "gRPC and HTTP ports cannot be the same: {}", cfg.grpc_port ); Ok(()) } /// Provides region/credentials configuration for AWS SDKs pub async fn load_aws_config() -> aws::AwsConfig { let mut config_builder = aws::config::from_env(); if let Some(endpoint) = &CONFIG.localstack_endpoint { info!("Using localstack URL: {}", endpoint); config_builder = config_builder.endpoint_url(endpoint); } config_builder.load().await } diff --git a/services/tunnelbroker/src/main.rs b/services/tunnelbroker/src/main.rs index 5fac6325e..57c47cf9e 100644 --- a/services/tunnelbroker/src/main.rs +++ b/services/tunnelbroker/src/main.rs @@ -1,73 +1,73 @@ pub mod amqp; pub mod config; pub mod constants; pub mod database; pub mod error; pub mod grpc; pub mod identity; pub mod notifs; pub mod websockets; -use crate::constants::ENV_APNS_CONFIG; -use crate::notifs::apns::config::APNsConfig; use crate::notifs::apns::APNsClient; use crate::notifs::NotifClient; use anyhow::{anyhow, Result}; use config::CONFIG; use std::str::FromStr; use tracing::{self, error, info, Level}; use tracing_subscriber::EnvFilter; #[tokio::main] async fn main() -> 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) .expect("Unable to configure tracing"); config::parse_cmdline_args()?; let aws_config = config::load_aws_config().await; let db_client = database::DatabaseClient::new(&aws_config); let amqp_connection = amqp::connect().await; let apns_config = CONFIG.apns_config.clone(); let apns = match apns_config { Some(config) => match APNsClient::new(&config) { Ok(apns_client) => { info!("APNs client created successfully"); Some(apns_client) } Err(err) => { error!("Error creating APNs client: {}", err); None } }, None => { error!("APNs config is missing"); None } }; - let notif_client = NotifClient { apns }; + let fcm_config = CONFIG.fcm_config.clone(); + + let notif_client = NotifClient { apns, fcm: None }; let grpc_server = grpc::run_server(db_client.clone(), &amqp_connection); let websocket_server = websockets::run_server( db_client.clone(), &amqp_connection, notif_client.clone(), ); tokio::select! { Ok(_) = grpc_server => { Ok(()) }, Ok(_) = websocket_server => { Ok(()) }, else => { tracing::error!("A grpc or websocket server crashed."); Err(anyhow!("A grpc or websocket server crashed.")) } } } diff --git a/services/tunnelbroker/src/notifs/fcm/config.rs b/services/tunnelbroker/src/notifs/fcm/config.rs new file mode 100644 index 000000000..961077f75 --- /dev/null +++ b/services/tunnelbroker/src/notifs/fcm/config.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; +use std::str::FromStr; + +#[derive(clap::Args, Clone, Debug, Deserialize, Serialize)] +pub struct FCMConfig { + pub account_type: String, + pub project_id: String, + pub private_key_id: String, + pub private_key: String, + pub client_email: String, + pub client_id: String, + pub auth_uri: String, + pub token_uri: String, + pub auth_provider_x509_cert_url: String, + pub client_x509_cert_url: String, +} + +impl FromStr for FCMConfig { + type Err = serde_json::Error; + fn from_str(s: &str) -> Result { + serde_json::from_str(s) + } +} diff --git a/services/tunnelbroker/src/notifs/fcm/mod.rs b/services/tunnelbroker/src/notifs/fcm/mod.rs new file mode 100644 index 000000000..a486fe4d7 --- /dev/null +++ b/services/tunnelbroker/src/notifs/fcm/mod.rs @@ -0,0 +1,6 @@ +pub mod config; + +#[derive(Clone)] +pub struct FCMClient { + http2_client: reqwest::Client, +} diff --git a/services/tunnelbroker/src/notifs/mod.rs b/services/tunnelbroker/src/notifs/mod.rs index 8110cbad9..28e15f367 100644 --- a/services/tunnelbroker/src/notifs/mod.rs +++ b/services/tunnelbroker/src/notifs/mod.rs @@ -1,8 +1,11 @@ use crate::notifs::apns::APNsClient; +use crate::notifs::fcm::FCMClient; pub mod apns; +pub mod fcm; #[derive(Clone)] pub struct NotifClient { pub(crate) apns: Option, + pub(crate) fcm: Option, }