2024-05-10 22:01:47 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-05-18 20:26:26 +01:00
|
|
|
use tracing::{info, warn};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-05-11 18:57:18 +01:00
|
|
|
use crate::actors::webhook::message::WebhookMessage;
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-05-10 22:01:47 +01:00
|
|
|
pub async fn start(
|
|
|
|
socket_addr: SocketAddr,
|
|
|
|
address: actix::prelude::Recipient<super::message::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())
|
|
|
|
// .and(warp::query::raw())
|
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(
|
|
|
|
|recipient: Recipient<WebhookMessage>,
|
|
|
|
path,
|
|
|
|
// 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-04-13 16:16:09 +01:00
|
|
|
let bytes = body.to_vec();
|
|
|
|
let request_data = String::from_utf8_lossy(&bytes).to_string();
|
|
|
|
let id = ulid::Ulid::new().to_string();
|
2024-04-14 19:12:51 +01:00
|
|
|
match headers.get("Authorization") {
|
|
|
|
Some(auhorisation) => {
|
2024-05-18 20:26:26 +01:00
|
|
|
info!(id, path, "Received webhook");
|
2024-04-14 19:12:51 +01:00
|
|
|
let authorisation = auhorisation
|
|
|
|
.to_str()
|
2024-05-18 20:26:26 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
warn!("Invalid value in authorization: {:?}", e);
|
|
|
|
warp::reject()
|
|
|
|
})? // valid characters
|
2024-04-14 19:12:51 +01:00
|
|
|
.strip_prefix("Basic ")
|
2024-05-18 20:26:26 +01:00
|
|
|
.ok_or_else(|| {
|
|
|
|
warn!("Authorization must be 'Basic'");
|
|
|
|
warp::reject()
|
|
|
|
})? // must start with "Basic "
|
2024-04-14 19:12:51 +01:00
|
|
|
.to_string();
|
|
|
|
let message = WebhookMessage::new(
|
|
|
|
id,
|
|
|
|
path,
|
|
|
|
/* query, headers, */ request_data,
|
|
|
|
authorisation,
|
|
|
|
);
|
|
|
|
recipient
|
|
|
|
.try_send(message)
|
2024-05-18 20:26:26 +01:00
|
|
|
.map(|_| {
|
|
|
|
info!("Message sent ok");
|
|
|
|
warp::reply::with_status("OK", warp::http::StatusCode::OK)
|
|
|
|
})
|
|
|
|
.map_err(|e| {
|
|
|
|
warn!("Unknown error: {:?}", e);
|
|
|
|
warp::reject()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
warn!("No Authorization header");
|
|
|
|
Err(warp::reject())
|
2024-04-14 19:12:51 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|