2024-05-22 08:41:30 +01:00
|
|
|
//
|
2024-05-25 11:25:13 +01:00
|
|
|
use std::{collections::HashMap, net::SocketAddr};
|
2024-05-10 22:01:47 +01:00
|
|
|
|
2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
use config::{ForgeAlias, RepoAlias};
|
|
|
|
use git_next_config as config;
|
|
|
|
|
2024-05-18 20:26:26 +01:00
|
|
|
use tracing::{info, warn};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
pub async fn start(
|
|
|
|
socket_addr: SocketAddr,
|
|
|
|
address: actix::prelude::Recipient<config::WebhookMessage>,
|
|
|
|
) {
|
2024-04-13 16:16:09 +01:00
|
|
|
// 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())
|
2024-05-29 19:22:05 +01:00
|
|
|
.and(warp::path::param())
|
2024-04-14 19:12:51 +01:00
|
|
|
.and(warp::header::headers_cloned())
|
2024-04-13 16:16:09 +01:00
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(
|
2024-05-25 11:25:13 +01:00
|
|
|
|recipient: Recipient<config::WebhookMessage>,
|
2024-05-29 19:22:05 +01:00
|
|
|
forge_alias: String,
|
|
|
|
repo_alias: String,
|
2024-04-13 16:16:09 +01:00
|
|
|
// query: String,
|
2024-04-14 19:12:51 +01:00
|
|
|
headers: warp::http::HeaderMap,
|
2024-04-13 16:16:09 +01:00
|
|
|
body: bytes::Bytes| async move {
|
2024-05-18 20:26:26 +01:00
|
|
|
info!("POST received");
|
2024-05-29 19:22:05 +01:00
|
|
|
let forge_alias = ForgeAlias::new(forge_alias);
|
|
|
|
let repo_alias = RepoAlias::new(repo_alias);
|
2024-04-13 16:16:09 +01:00
|
|
|
let bytes = body.to_vec();
|
2024-05-25 11:25:13 +01:00
|
|
|
let body = config::webhook::message::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::<HashMap<String, String>>();
|
|
|
|
let message = config::WebhookMessage::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()
|
|
|
|
})
|
2024-04-13 16:16:09 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the server
|
2024-05-10 22:01:47 +01:00
|
|
|
info!("Starting webhook server: {}", socket_addr);
|
|
|
|
warp::serve(route).run(socket_addr).await;
|
2024-04-13 16:16:09 +01:00
|
|
|
}
|