git-next/crates/webhook-actor/src/server.rs

68 lines
2.3 KiB
Rust
Raw Normal View History

//
use std::{collections::BTreeMap, net::SocketAddr};
use actix::prelude::*;
use config::{ForgeAlias, RepoAlias};
use git_next_config as config;
use git_next_repo_actor::messages::WebhookNotification;
use tracing::{info, warn};
pub async fn start(
socket_addr: SocketAddr,
address: actix::prelude::Recipient<WebhookNotification>,
) {
// start webhook server
use warp::Filter;
// Define the Warp route to handle incoming HTTP requests
let route = warp::post()
.map(move || address.clone())
.and(warp::path::param())
.and(warp::path::param())
.and(warp::header::headers_cloned())
.and(warp::body::bytes())
.and_then(
|recipient: Recipient<WebhookNotification>,
forge_alias: String,
repo_alias: String,
// query: String,
headers: warp::http::HeaderMap,
body: bytes::Bytes| async move {
info!("POST received");
let forge_alias = ForgeAlias::new(forge_alias);
let repo_alias = RepoAlias::new(repo_alias);
let bytes = body.to_vec();
let body = config::webhook::forge_notification::Body::new(
String::from_utf8_lossy(&bytes).to_string(),
);
let headers = headers
.into_iter()
.filter_map(|(k, v)| {
k.map(|k| (k.to_string(), v.to_str().unwrap_or_default().to_string()))
})
.collect::<BTreeMap<String, String>>();
let message = WebhookNotification::new(config::ForgeNotification::new(
forge_alias,
repo_alias,
headers,
body,
));
recipient
.try_send(message)
.map(|_| {
info!("Message sent ok");
warp::reply::with_status("OK", warp::http::StatusCode::OK)
})
.map_err(|e| {
warn!("Unknown error: {:?}", e);
warp::reject()
})
},
);
// Start the server
info!("Starting webhook server: {}", socket_addr);
warp::serve(route).run(socket_addr).await;
}