use std::env; use axum::{ extract::{Json}, }; use web_push::{ SubscriptionInfo, VapidSignatureBuilder, WebPushMessageBuilder, ContentEncoding, WebPushClient, IsahcWebPushClient, }; 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") } #[axum::debug_handler] pub async fn echo( Json(payload): Json, ) -> Result<(), crate::error::Internal> { let endpoint = payload.endpoint; let p256dh = payload.keys.p256dh; let auth = payload.keys.auth; let subscription_info = SubscriptionInfo::new(endpoint, p256dh, 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); // Eventually, this message will be something other than an echo: let content = payload.msg.as_bytes(); builder.set_payload(ContentEncoding::Aes128Gcm, content); builder.set_vapid_signature(sig_builder); let client = IsahcWebPushClient::new()?; client.send(builder.build()?).await?; // Fix up return type? 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, }