2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-04-14 15:46:21 +01:00
|
|
|
use tracing::{debug, info};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
use crate::server::actors::webhook::message::WebhookMessage;
|
|
|
|
|
|
|
|
pub async fn start(address: actix::prelude::Recipient<super::message::WebhookMessage>) {
|
|
|
|
// 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 {
|
|
|
|
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) => {
|
|
|
|
debug!(id, path, "Received webhook");
|
|
|
|
let authorisation = auhorisation
|
|
|
|
.to_str()
|
|
|
|
.map_err(|_| warp::reject())? // valid characters
|
|
|
|
.strip_prefix("Basic ")
|
|
|
|
.ok_or_else(warp::reject)? // must start with "Basic "
|
|
|
|
.to_string();
|
|
|
|
let message = WebhookMessage::new(
|
|
|
|
id,
|
|
|
|
path,
|
|
|
|
/* query, headers, */ request_data,
|
|
|
|
authorisation,
|
|
|
|
);
|
|
|
|
recipient
|
|
|
|
.try_send(message)
|
|
|
|
.map(|_| warp::reply::with_status("OK", warp::http::StatusCode::OK))
|
|
|
|
.map_err(|_| warp::reject())
|
|
|
|
}
|
|
|
|
_ => Err(warp::reject()),
|
|
|
|
}
|
2024-04-13 16:16:09 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
info!("Starting webhook server: http://0.0.0.0:8080/");
|
|
|
|
warp::serve(route).run(([0, 0, 0, 0], 8080)).await;
|
|
|
|
}
|