diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 22:32:27 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 22:32:27 -0400 |
| commit | b63380b251d04dd92f06aa5bbc22a72ca3e4bf8e (patch) | |
| tree | 3956ec457131ce049fd91f2f53309bc0620fffe2 /src/push/handlers/mod.rs | |
| parent | 2e42057694851b82574e0a406ded429fb95a07fa (diff) | |
wip: 83B78D40-D7CB-4419-9FE7-E7D858909443
Diffstat (limited to 'src/push/handlers/mod.rs')
| -rw-r--r-- | src/push/handlers/mod.rs | 79 |
1 files changed, 10 insertions, 69 deletions
diff --git a/src/push/handlers/mod.rs b/src/push/handlers/mod.rs index e4a531b..90edaa7 100644 --- a/src/push/handlers/mod.rs +++ b/src/push/handlers/mod.rs @@ -1,74 +1,15 @@ -use std::env; +use axum::extract::State; -use axum::{ - extract::{Json}, -}; +use crate::app::App; -use web_push::{ - SubscriptionInfo, - VapidSignatureBuilder, - WebPushMessageBuilder, - ContentEncoding, - WebPushClient, - IsahcWebPushClient, -}; +mod echo; +mod register; +mod unregister; +pub use echo::handler as echo; +pub use register::handler as register; +pub use unregister::handler as unregister; -pub async fn vapid() -> String { - let vapid_public_key = env::var("VAPID_PUBLIC_KEY").unwrap_or_default(); - String::from(vapid_public_key) -} - - -pub async fn register() -> String { - String::from("OK") -} - - -pub async fn unregister() -> String { - String::from("OK") -} - -async fn push_message( - endpoint: String, - keys: Keys, - message: &String, -) -> Result<(), crate::error::Internal> { - let content = message.as_bytes(); - - let subscription_info = SubscriptionInfo::new(endpoint, keys.p256dh, keys.auth); - // This will need to come from the DB eventually: - let private_key = String::from(env::var("VAPID_PRIVATE_KEY").unwrap_or_default()); - let sig_builder = VapidSignatureBuilder::from_base64(&private_key, &subscription_info)?.build()?; - let mut builder = WebPushMessageBuilder::new(&subscription_info); - builder.set_payload(ContentEncoding::Aes128Gcm, content); - builder.set_vapid_signature(sig_builder); - let client = IsahcWebPushClient::new()?; - client.send(builder.build()?).await?; - - Ok(()) -} - - -#[axum::debug_handler] -pub async fn echo( - Json(payload): Json<PushPayload>, -) -> Result<(), crate::error::Internal> { - push_message(payload.endpoint, payload.keys, &payload.msg).await?; - - Ok(()) -} - - -#[derive(serde::Deserialize)] -pub struct Keys { - pub p256dh: String, - pub auth: String, -} - -#[derive(serde::Deserialize)] -pub struct PushPayload { - pub msg: String, - pub endpoint: String, - pub keys: Keys, +pub async fn vapid(State(app): State<App>) -> String { + app.push().public_key().to_owned() } |
