Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
62 lines
2.2 KiB
Rust
62 lines
2.2 KiB
Rust
use std::sync::Arc;
|
|
|
|
use axum::routing::post;
|
|
use axum::{Extension, Router};
|
|
|
|
use compliance_core::shutdown::{self, Shutdown};
|
|
|
|
use crate::agent::ComplianceAgent;
|
|
use crate::error::AgentError;
|
|
use crate::webhooks::{gitea, gitea_source, github, gitlab};
|
|
|
|
/// Serve the webhook receivers on `agent.config.webhook_port` until `shutdown`
|
|
/// fires, then drain in-flight deliveries for at most
|
|
/// [`shutdown::GRACE_PERIOD`].
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the port cannot be bound or serving fails.
|
|
pub async fn start_webhook_server(
|
|
agent: &ComplianceAgent,
|
|
shutdown: Shutdown,
|
|
) -> Result<(), AgentError> {
|
|
let app = Router::new()
|
|
// Per-tenant per-repo webhook URLs: /webhook/{tenant_id}/{platform}/{repo_id}
|
|
// The tenant_id is resolved from the URL path because webhooks
|
|
// arrive without a JWT — they're authenticated via per-repo HMAC,
|
|
// not via the tenant gate. The dashboard surfaces the full URL
|
|
// including the tenant_id when the repo is registered.
|
|
.route(
|
|
"/webhook/{tenant_id}/github/{repo_id}",
|
|
post(github::handle_github_webhook),
|
|
)
|
|
.route(
|
|
"/webhook/{tenant_id}/gitlab/{repo_id}",
|
|
post(gitlab::handle_gitlab_webhook),
|
|
)
|
|
.route(
|
|
"/webhook/{tenant_id}/gitea/{repo_id}",
|
|
post(gitea::handle_gitea_webhook),
|
|
)
|
|
// The managed git source's system/org webhook (#439): one per tenant,
|
|
// HMAC-verified with the source's secret.
|
|
.route(
|
|
"/webhook/{tenant_id}/gitea-source",
|
|
post(gitea_source::handle_gitea_source_webhook),
|
|
)
|
|
.layer(Extension(Arc::new(agent.clone())));
|
|
|
|
let addr = format!("0.0.0.0:{}", agent.config.webhook_port);
|
|
let listener = tokio::net::TcpListener::bind(&addr)
|
|
.await
|
|
.map_err(|e| AgentError::Other(format!("Failed to bind webhook server to {addr}: {e}")))?;
|
|
|
|
tracing::info!("Webhook server listening on {addr}");
|
|
shutdown::serve(listener, app, shutdown, shutdown::GRACE_PERIOD)
|
|
.await
|
|
.map_err(|e| AgentError::Other(format!("Webhook server error: {e}")))?;
|
|
tracing::info!("Webhook server stopped");
|
|
|
|
Ok(())
|
|
}
|