use std::sync::Arc;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use log::{error, info, warn};
use crate::dispatcher::Dispatcher;
pub struct WebhookServer {
dispatcher: Arc<Dispatcher>,
secret: Option<String>,
path: String,
}
impl WebhookServer {
pub fn new(dispatcher: Dispatcher) -> Self {
Self {
dispatcher: Arc::new(dispatcher),
secret: None,
path: "/".to_string(),
}
}
pub fn secret(mut self, secret: impl Into<String>) -> Self {
self.secret = Some(secret.into());
self
}
pub fn path(mut self, path: impl Into<String>) -> Self {
self.path = path.into();
self
}
pub async fn serve(self, bind: impl AsRef<str>) -> std::io::Result<()> {
let bind = bind.as_ref().to_string();
let data = Arc::new(self);
info!("Starting webhook server on {}", bind);
info!("Webhook path: {}", data.path);
HttpServer::new(move || {
let data = data.clone();
App::new()
.app_data(web::Data::new(data.clone()))
.route(&data.path, web::post().to(handle_update))
})
.bind(bind)?
.run()
.await
}
}
async fn handle_update(
state: web::Data<Arc<WebhookServer>>,
req: HttpRequest,
body: web::Bytes,
) -> impl Responder {
if let Some(expected) = &state.secret {
let provided = req
.headers()
.get("x-max-bot-api-secret")
.and_then(|v| v.to_str().ok());
match provided {
Some(secret) if secret == expected => {}
Some(secret) => {
warn!("Webhook secret mismatch: got '{}'", secret);
return HttpResponse::Unauthorized().body("Invalid secret");
}
None => {
warn!("Missing X-Max-Bot-Api-Secret header");
return HttpResponse::Unauthorized().body("Missing secret header");
}
}
}
let update_json = match serde_json::from_slice(&body) {
Ok(json) => json,
Err(e) => {
error!("Failed to parse webhook payload: {}", e);
return HttpResponse::Ok().finish();
}
};
state.dispatcher.dispatch_raw(update_json).await;
HttpResponse::Ok().finish()
}