diff --git a/services/tunnelbroker/src/main.rs b/services/tunnelbroker/src/main.rs index 701be95ab..736b192a3 100644 --- a/services/tunnelbroker/src/main.rs +++ b/services/tunnelbroker/src/main.rs @@ -1,95 +1,112 @@ 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::notifs::apns::APNsClient; use crate::notifs::fcm::FCMClient; +use crate::notifs::web_push::WebPushClient; use crate::notifs::NotifClient; use anyhow::{anyhow, Result}; use config::CONFIG; 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 fcm_config = CONFIG.fcm_config.clone(); let fcm = match fcm_config { Some(config) => match FCMClient::new(&config) { Ok(fcm_client) => { info!("FCM client created successfully"); Some(fcm_client) } Err(err) => { error!("Error creating FCM client: {}", err); None } }, None => { error!("FCM config is missing"); None } }; let web_push_config = CONFIG.web_push_config.clone(); + let web_push = match web_push_config { + Some(config) => match WebPushClient::new(&config) { + Ok(web_client) => { + info!("Web Push client created successfully"); + Some(web_client) + } + Err(err) => { + error!("Error creating Web Push client: {}", err); + None + } + }, + None => { + error!("Web Push config is missing"); + None + } + }; let notif_client = NotifClient { apns, fcm, - web_push: None, + web_push, }; 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/web_push/error.rs b/services/tunnelbroker/src/notifs/web_push/error.rs new file mode 100644 index 000000000..8e8885c61 --- /dev/null +++ b/services/tunnelbroker/src/notifs/web_push/error.rs @@ -0,0 +1,15 @@ +use derive_more::{Display, Error, From}; + +#[derive(Debug, From, Display, Error)] +pub enum Error { + JWTError, + ReqwestError(reqwest::Error), + InvalidHeaderValue(reqwest::header::InvalidHeaderValue), + SerdeJson(serde_json::Error), +} + +impl From for Error { + fn from(_: jsonwebtoken::errors::Error) -> Self { + Self::JWTError + } +} diff --git a/services/tunnelbroker/src/notifs/web_push/mod.rs b/services/tunnelbroker/src/notifs/web_push/mod.rs index 15e96f220..12d79a95d 100644 --- a/services/tunnelbroker/src/notifs/web_push/mod.rs +++ b/services/tunnelbroker/src/notifs/web_push/mod.rs @@ -1,6 +1,20 @@ +use crate::notifs::web_push::config::WebPushConfig; + pub mod config; +mod error; #[derive(Clone)] pub struct WebPushClient { http_client: reqwest::Client, + config: WebPushConfig, +} + +impl WebPushClient { + pub fn new(config: &WebPushConfig) -> Result { + let http_client = reqwest::Client::builder().build()?; + Ok(WebPushClient { + http_client, + config: config.clone(), + }) + } }