git-next/crates/server/src/actors/webhook/router.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use actix::prelude::*;
use tracing::{debug, info};
2024-05-11 18:57:18 +01:00
use crate::{actors::webhook::message::WebhookMessage, config::RepoAlias};
pub struct WebhookRouter {
span: tracing::Span,
repos: HashMap<RepoAlias, Recipient<WebhookMessage>>,
}
impl WebhookRouter {
pub fn new() -> Self {
let span = tracing::info_span!("WebhookRouter");
Self {
span,
repos: Default::default(),
}
}
}
impl Actor for WebhookRouter {
type Context = Context<Self>;
}
impl Handler<WebhookMessage> for WebhookRouter {
type Result = ();
fn handle(&mut self, msg: WebhookMessage, _ctx: &mut Self::Context) -> Self::Result {
let _gaurd = self.span.enter();
let repo_alias = RepoAlias(msg.path().clone());
debug!(repo = %repo_alias, "Router...");
if let Some(recipient) = self.repos.get(&repo_alias) {
info!(repo = %repo_alias, "Sending to Recipient");
recipient.do_send(msg);
}
}
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct AddWebhookRecipient(pub RepoAlias, pub Recipient<WebhookMessage>);
impl Handler<AddWebhookRecipient> for WebhookRouter {
type Result = ();
fn handle(&mut self, msg: AddWebhookRecipient, _ctx: &mut Self::Context) -> Self::Result {
let _gaurd = self.span.enter();
info!(repo = %msg.0, "Register Recipient");
self.repos.insert(msg.0, msg.1);
}
}