67 lines
2.3 KiB
Rust
67 lines
2.3 KiB
Rust
//
|
|
use actix::prelude::*;
|
|
|
|
use std::{collections::BTreeMap, net::SocketAddr};
|
|
|
|
use tracing::{info, warn};
|
|
|
|
use crate::repo::messages::WebhookNotification;
|
|
|
|
use git_next_core::{webhook, ForgeAlias, ForgeNotification, RepoAlias};
|
|
|
|
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 = 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(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;
|
|
}
|