Skip to content
Clone
main
37 lines 1.2 KB Rust
use api::http::State;
use api::rest::AppResponseData;
use serde::Serialize;
use utoipa::ToSchema;
use config::push::PushConfig;
/// 前端注册 Service Worker 所需的公开参数。
///
/// 只有公钥是公开的(它就是浏览器 `applicationServerKey` 要的东西),私钥永远
/// 不出现在任何响应里。`enabled=false` 时前端把整个"系统通知"开关藏起来,
/// 而不是让用户点了才发现服务端没开。
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct PushConfigDto {
pub enabled: bool,
pub public_key: String,
}
#[utoipa::path(
get,
path = "/push/config",
tag = "push",
description = "读取 Web Push 是否启用及其 VAPID 公钥(注册推送订阅所需,无会话要求)。",
responses(
(status = 200, description = "web push configuration", body = AppResponseData<PushConfigDto>)
)
)]
pub async fn push_config(State(push): State<PushConfig>) -> AppResponseData<PushConfigDto> {
AppResponseData::ok(PushConfigDto {
enabled: push.enabled,
public_key: if push.enabled {
push.vapid_public_key.trim().to_string()
} else {
String::new()
},
})
}