2024-04-13 16:16:09 +01:00
|
|
|
// crate::server::actors::webhook
|
|
|
|
|
|
|
|
mod message;
|
|
|
|
mod router;
|
|
|
|
mod server;
|
|
|
|
|
2024-05-10 22:01:47 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
|
|
|
pub use message::WebhookMessage;
|
|
|
|
pub use router::AddWebhookRecipient;
|
|
|
|
pub use router::WebhookRouter;
|
2024-05-04 12:37:35 +01:00
|
|
|
use tracing::Instrument;
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WebhookActor {
|
2024-05-10 22:01:47 +01:00
|
|
|
socket_addr: SocketAddr,
|
2024-05-04 12:37:35 +01:00
|
|
|
span: tracing::Span,
|
2024-04-13 16:16:09 +01:00
|
|
|
spawn_handle: Option<actix::SpawnHandle>,
|
|
|
|
message_receiver: Recipient<WebhookMessage>,
|
|
|
|
}
|
|
|
|
impl WebhookActor {
|
2024-05-10 22:01:47 +01:00
|
|
|
pub fn new(socket_addr: SocketAddr, message_receiver: Recipient<WebhookMessage>) -> Self {
|
2024-05-04 12:37:35 +01:00
|
|
|
let span = tracing::info_span!("WebhookActor");
|
2024-04-13 16:16:09 +01:00
|
|
|
Self {
|
2024-05-10 22:01:47 +01:00
|
|
|
socket_addr,
|
2024-05-04 12:37:35 +01:00
|
|
|
span,
|
2024-04-13 16:16:09 +01:00
|
|
|
message_receiver,
|
|
|
|
spawn_handle: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Actor for WebhookActor {
|
|
|
|
type Context = actix::Context<Self>;
|
|
|
|
fn started(&mut self, ctx: &mut Self::Context) {
|
2024-05-04 12:37:35 +01:00
|
|
|
let _gaurd = self.span.enter();
|
2024-04-13 16:16:09 +01:00
|
|
|
let address: Recipient<WebhookMessage> = self.message_receiver.clone();
|
2024-05-10 22:01:47 +01:00
|
|
|
let server = server::start(self.socket_addr, address);
|
2024-05-04 12:37:35 +01:00
|
|
|
let spawn_handle = ctx.spawn(server.in_current_span().into_actor(self));
|
2024-04-13 16:16:09 +01:00
|
|
|
self.spawn_handle.replace(spawn_handle);
|
|
|
|
}
|
|
|
|
}
|
2024-05-07 19:32:15 +01:00
|
|
|
|
2024-05-15 21:01:19 +01:00
|
|
|
#[derive(Debug, Message)]
|
|
|
|
#[rtype(result = "()")]
|
2024-05-07 19:32:15 +01:00
|
|
|
pub struct ShutdownWebhook;
|
|
|
|
impl Handler<ShutdownWebhook> for WebhookActor {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, _msg: ShutdownWebhook, ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
self.spawn_handle.take();
|
|
|
|
ctx.stop();
|
|
|
|
}
|
|
|
|
}
|