feat: Complete collaboration event delivery #3
+2
-14
@@ -1,202 +1,190 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use hmac::{Hmac, KeyInit, Mac};
|
||||
use sha2::Sha256;
|
||||
use syncode_collab_application::{
|
||||
Acceptance, EventInbox, EventPublisher, OutboxMessage, OutboxQueue, PublishError, accept,
|
||||
dispatch,
|
||||
};
|
||||
use syncode_collab_model::envelope::PROTOCOL_VERSION;
|
||||
use syncode_collab_model::{Envelope, MessageType, SchemaVersion};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct OutboxWorker<Q> {
|
||||
queue: Q,
|
||||
publisher: Publisher,
|
||||
interval: Duration,
|
||||
batch: i64,
|
||||
lease: i64,
|
||||
}
|
||||
|
||||
pub struct OutboxWorkerConfig {
|
||||
pub source_node_id: Uuid,
|
||||
pub control_url: String,
|
||||
pub secret: String,
|
||||
pub interval: Duration,
|
||||
pub batch: i64,
|
||||
pub lease: i64,
|
||||
}
|
||||
|
||||
impl<Q> OutboxWorker<Q>
|
||||
where
|
||||
Q: OutboxQueue,
|
||||
{
|
||||
pub fn new(
|
||||
queue: Q,
|
||||
inbox: Arc<dyn EventInbox>,
|
||||
config: OutboxWorkerConfig,
|
||||
) -> Result<Self, String> {
|
||||
if config.interval.is_zero() || config.batch <= 0 || config.lease <= 0 {
|
||||
return Err("outbox interval, batch and lease must be positive".to_owned());
|
||||
}
|
||||
let control = ControlPublisher::new(config.control_url, config.secret)?;
|
||||
Ok(Self {
|
||||
queue,
|
||||
publisher: Publisher {
|
||||
inbox,
|
||||
source_node_id: config.source_node_id,
|
||||
control,
|
||||
},
|
||||
interval: config.interval,
|
||||
batch: config.batch,
|
||||
lease: config.lease,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(self) -> Result<(), String> {
|
||||
loop {
|
||||
dispatch(&self.queue, &self.publisher, self.batch, self.lease)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
tokio::time::sleep(self.interval).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Publisher {
|
||||
inbox: Arc<dyn EventInbox>,
|
||||
source_node_id: Uuid,
|
||||
control: ControlPublisher,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventPublisher for Publisher {
|
||||
async fn publish(&self, message: &OutboxMessage) -> Result<(), PublishError> {
|
||||
let envelope = envelope(self.source_node_id, message)?;
|
||||
match accept(self.inbox.as_ref(), &envelope).await {
|
||||
Ok(Acceptance::Applied | Acceptance::AlreadyApplied) => {}
|
||||
Err(error) => return Err(PublishError::Unreachable(error.to_string())),
|
||||
}
|
||||
if control_event(message) {
|
||||
self.control.publish(&envelope).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn envelope(source_node_id: Uuid, message: &OutboxMessage) -> Result<Envelope, PublishError> {
|
||||
Ok(Envelope {
|
||||
message_id: message.message_id,
|
||||
protocol_version: PROTOCOL_VERSION,
|
||||
schema_version: SchemaVersion(1),
|
||||
source_node_id: source_node_id.to_string(),
|
||||
repository_id: message.resource_id,
|
||||
message_type: MessageType::new(message.message_type.clone())
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?,
|
||||
sequence: message.id,
|
||||
term: 1,
|
||||
payload: message.payload.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn control_event(message: &OutboxMessage) -> bool {
|
||||
matches!(
|
||||
message.message_type.as_str(),
|
||||
"collaboration.pull_request.created"
|
||||
| "collaboration.pull_request.synchronized"
|
||||
| "collaboration.pull_request.state_changed"
|
||||
) && message.payload.get("head_oid").is_some()
|
||||
}
|
||||
|
||||
struct ControlPublisher {
|
||||
client: reqwest::Client,
|
||||
url: reqwest::Url,
|
||||
secret: String,
|
||||
}
|
||||
|
||||
impl ControlPublisher {
|
||||
fn new(url: String, secret: String) -> Result<Self, String> {
|
||||
if secret.is_empty() {
|
||||
return Err("event secret must not be empty".to_owned());
|
||||
}
|
||||
let url = reqwest::Url::parse(&url).map_err(|error| error.to_string())?;
|
||||
let client = reqwest::Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(Self {
|
||||
client,
|
||||
url,
|
||||
secret,
|
||||
})
|
||||
}
|
||||
|
||||
async fn publish(&self, envelope: &Envelope) -> Result<(), PublishError> {
|
||||
let body = serde_json::to_vec(envelope)
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?;
|
||||
let signature = signature(&self.secret, &body)?;
|
||||
let response = self
|
||||
.client
|
||||
.post(self.url.clone())
|
||||
.header("x-syncode-event", envelope.message_type.as_str())
|
||||
.header("x-syncode-delivery", envelope.message_id.to_string())
|
||||
.header("x-syncode-signature", signature)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|error| PublishError::Unreachable(error.to_string()))?;
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PublishError::Rejected(format!(
|
||||
"control returned HTTP {}",
|
||||
response.status()
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn signature(secret: &str, body: &[u8]) -> Result<String, PublishError> {
|
||||
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes())
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?;
|
||||
mac.update(body);
|
||||
Ok(hex::encode(mac.finalize().into_bytes()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use syncode_collab_application::OutboxMessage;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::control_event;
|
||||
|
||||
fn message(message_type: &str, payload: serde_json::Value) -> OutboxMessage {
|
||||
OutboxMessage {
|
||||
id: 1,
|
||||
message_id: Uuid::nil(),
|
||||
message_type: message_type.to_owned(),
|
||||
resource_id: Uuid::nil(),
|
||||
payload,
|
||||
attempts: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sends_only_actionable_pull_request_facts_to_control() {
|
||||
assert!(control_event(&message(
|
||||
"collaboration.pull_request.created",
|
||||
serde_json::json!({"head_oid": "abc"}),
|
||||
)));
|
||||
assert!(!control_event(&message(
|
||||
"collaboration.pull_request.created",
|
||||
serde_json::json!({"number": 1}),
|
||||
)));
|
||||
assert!(!control_event(&message(
|
||||
"collaboration.pull_request.merged",
|
||||
serde_json::json!({"head_oid": "abc"}),
|
||||
)));
|
||||
}
|
||||
}
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use hmac::{Hmac, KeyInit, Mac};
|
||||
use sha2::Sha256;
|
||||
use syncode_collab_application::{
|
||||
EventPublisher, OutboxMessage, OutboxQueue, PublishError, dispatch,
|
||||
};
|
||||
use syncode_collab_model::envelope::PROTOCOL_VERSION;
|
||||
use syncode_collab_model::{Envelope, MessageType, SchemaVersion};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct OutboxWorker<Q> {
|
||||
queue: Q,
|
||||
publisher: Publisher,
|
||||
interval: Duration,
|
||||
batch: i64,
|
||||
lease: i64,
|
||||
}
|
||||
|
||||
pub struct OutboxWorkerConfig {
|
||||
pub source_node_id: Uuid,
|
||||
pub control_url: String,
|
||||
pub secret: String,
|
||||
pub interval: Duration,
|
||||
pub batch: i64,
|
||||
pub lease: i64,
|
||||
}
|
||||
|
||||
impl<Q> OutboxWorker<Q>
|
||||
where
|
||||
Q: OutboxQueue,
|
||||
{
|
||||
pub fn new(queue: Q, config: OutboxWorkerConfig) -> Result<Self, String> {
|
||||
if config.interval.is_zero() || config.batch <= 0 || config.lease <= 0 {
|
||||
return Err("outbox interval, batch and lease must be positive".to_owned());
|
||||
}
|
||||
let control = ControlPublisher::new(config.control_url, config.secret)?;
|
||||
Ok(Self {
|
||||
queue,
|
||||
publisher: Publisher {
|
||||
source_node_id: config.source_node_id,
|
||||
control,
|
||||
},
|
||||
interval: config.interval,
|
||||
batch: config.batch,
|
||||
lease: config.lease,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(self) -> Result<(), String> {
|
||||
loop {
|
||||
dispatch(&self.queue, &self.publisher, self.batch, self.lease)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
tokio::time::sleep(self.interval).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Publisher {
|
||||
source_node_id: Uuid,
|
||||
control: ControlPublisher,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventPublisher for Publisher {
|
||||
async fn publish(&self, message: &OutboxMessage) -> Result<(), PublishError> {
|
||||
let envelope = envelope(self.source_node_id, message)?;
|
||||
if control_event(message) {
|
||||
self.control.publish(&envelope).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn envelope(source_node_id: Uuid, message: &OutboxMessage) -> Result<Envelope, PublishError> {
|
||||
Ok(Envelope {
|
||||
message_id: message.message_id,
|
||||
protocol_version: PROTOCOL_VERSION,
|
||||
schema_version: SchemaVersion(1),
|
||||
source_node_id: source_node_id.to_string(),
|
||||
repository_id: message.resource_id,
|
||||
message_type: MessageType::new(message.message_type.clone())
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?,
|
||||
sequence: message.id,
|
||||
term: 1,
|
||||
payload: message.payload.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn control_event(message: &OutboxMessage) -> bool {
|
||||
matches!(
|
||||
message.message_type.as_str(),
|
||||
"collaboration.pull_request.created"
|
||||
| "collaboration.pull_request.synchronized"
|
||||
| "collaboration.pull_request.state_changed"
|
||||
) && message.payload.get("head_oid").is_some()
|
||||
}
|
||||
|
||||
struct ControlPublisher {
|
||||
client: reqwest::Client,
|
||||
url: reqwest::Url,
|
||||
secret: String,
|
||||
}
|
||||
|
||||
impl ControlPublisher {
|
||||
fn new(url: String, secret: String) -> Result<Self, String> {
|
||||
if secret.is_empty() {
|
||||
return Err("event secret must not be empty".to_owned());
|
||||
}
|
||||
let url = reqwest::Url::parse(&url).map_err(|error| error.to_string())?;
|
||||
let client = reqwest::Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(Self {
|
||||
client,
|
||||
url,
|
||||
secret,
|
||||
})
|
||||
}
|
||||
|
||||
async fn publish(&self, envelope: &Envelope) -> Result<(), PublishError> {
|
||||
let body = serde_json::to_vec(envelope)
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?;
|
||||
let signature = signature(&self.secret, &body)?;
|
||||
let response = self
|
||||
.client
|
||||
.post(self.url.clone())
|
||||
.header("x-syncode-event", envelope.message_type.as_str())
|
||||
.header("x-syncode-delivery", envelope.message_id.to_string())
|
||||
.header("x-syncode-signature", signature)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|error| PublishError::Unreachable(error.to_string()))?;
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PublishError::Rejected(format!(
|
||||
"control returned HTTP {}",
|
||||
response.status()
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn signature(secret: &str, body: &[u8]) -> Result<String, PublishError> {
|
||||
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes())
|
||||
.map_err(|error| PublishError::Rejected(error.to_string()))?;
|
||||
mac.update(body);
|
||||
Ok(hex::encode(mac.finalize().into_bytes()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use syncode_collab_application::OutboxMessage;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::control_event;
|
||||
|
||||
fn message(message_type: &str, payload: serde_json::Value) -> OutboxMessage {
|
||||
OutboxMessage {
|
||||
id: 1,
|
||||
message_id: Uuid::nil(),
|
||||
message_type: message_type.to_owned(),
|
||||
resource_id: Uuid::nil(),
|
||||
payload,
|
||||
attempts: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sends_only_actionable_pull_request_facts_to_control() {
|
||||
assert!(control_event(&message(
|
||||
"collaboration.pull_request.created",
|
||||
serde_json::json!({"head_oid": "abc"}),
|
||||
)));
|
||||
assert!(!control_event(&message(
|
||||
"collaboration.pull_request.created",
|
||||
serde_json::json!({"number": 1}),
|
||||
)));
|
||||
assert!(!control_event(&message(
|
||||
"collaboration.pull_request.merged",
|
||||
serde_json::json!({"head_oid": "abc"}),
|
||||
)));
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -1,162 +1,161 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use syncode_collab_api_grpc::CollabService;
|
||||
use syncode_collab_application::Collaboration;
|
||||
use syncode_collab_store::{
|
||||
PostgresCollaboration, PostgresInbox, PostgresOutbox, connect, migrate,
|
||||
};
|
||||
use tower_http::cors::{AllowOrigin, CorsLayer};
|
||||
|
||||
use crate::checks::ControlChecks;
|
||||
use crate::config::Config;
|
||||
use crate::http::router;
|
||||
use crate::identity::IdentityClientAccess;
|
||||
use crate::outbox_worker::{OutboxWorker, OutboxWorkerConfig};
|
||||
use crate::repository::RepositoryClientAccess;
|
||||
use crate::webhook_worker::WebhookWorker;
|
||||
|
||||
pub async fn run(config: Config) -> Result<(), RuntimeError> {
|
||||
if config.event_secret.is_empty() {
|
||||
return Err(RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_EVENT_SECRET must not be empty".to_owned(),
|
||||
));
|
||||
}
|
||||
if config.identity_shared_secret.is_empty() {
|
||||
return Err(RuntimeError::Configuration(
|
||||
"SYNCODE_IDENTITY_SHARED_SECRET must not be empty".to_owned(),
|
||||
));
|
||||
}
|
||||
let webhook_key = parse_webhook_key(&config.webhook_key)?;
|
||||
let pool = connect(&config.database_url, config.database_max_connections)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Store(error.to_string()))?;
|
||||
migrate(&pool)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Store(error.to_string()))?;
|
||||
|
||||
let http = tokio::net::TcpListener::bind(&config.listen_http)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Listen(config.listen_http.clone(), error.to_string()))?;
|
||||
let grpc = config
|
||||
.listen_grpc
|
||||
.parse()
|
||||
.map_err(|_| RuntimeError::Address(config.listen_grpc.clone()))?;
|
||||
|
||||
let identity = Arc::new(
|
||||
IdentityClientAccess::connect(config.identity_grpc_url, config.identity_shared_secret)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Identity(error.to_string()))?,
|
||||
);
|
||||
let repository = Arc::new(
|
||||
RepositoryClientAccess::connect(config.repository_grpc_url)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Repository(error.to_string()))?,
|
||||
);
|
||||
let checks = Arc::new(
|
||||
ControlChecks::connect(config.control_grpc_url)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Control(error.to_string()))?,
|
||||
);
|
||||
let store = PostgresCollaboration::with_webhook_key(pool.clone(), webhook_key);
|
||||
let collaboration = Arc::new(Collaboration::with_delivery(
|
||||
Arc::new(store.clone()),
|
||||
identity,
|
||||
repository,
|
||||
checks,
|
||||
));
|
||||
let inbox = Arc::new(PostgresInbox::new(pool.clone()));
|
||||
let outbox_worker = OutboxWorker::new(
|
||||
PostgresOutbox::new(pool.clone()),
|
||||
Arc::new(PostgresInbox::new(pool)),
|
||||
OutboxWorkerConfig {
|
||||
source_node_id: config.source_node_id,
|
||||
control_url: config.control_event_url,
|
||||
secret: config.event_secret.clone(),
|
||||
interval: Duration::from_secs(config.outbox_interval_seconds),
|
||||
batch: config.outbox_batch,
|
||||
lease: config.outbox_lease_seconds,
|
||||
},
|
||||
)
|
||||
.map_err(RuntimeError::Configuration)?
|
||||
.run();
|
||||
let mut http_router = router(
|
||||
inbox,
|
||||
Arc::clone(&collaboration),
|
||||
config.event_secret,
|
||||
config.session_cookie_name,
|
||||
);
|
||||
if !config.cors_origins.is_empty() {
|
||||
let allowed = config.cors_origins;
|
||||
http_router = http_router.layer(
|
||||
CorsLayer::new()
|
||||
.allow_credentials(true)
|
||||
.allow_methods([
|
||||
axum::http::Method::GET,
|
||||
axum::http::Method::POST,
|
||||
axum::http::Method::OPTIONS,
|
||||
])
|
||||
.allow_headers([axum::http::header::CONTENT_TYPE])
|
||||
.allow_origin(AllowOrigin::predicate(move |origin, _| {
|
||||
origin
|
||||
.to_str()
|
||||
.is_ok_and(|origin| allowed.iter().any(|value| value == origin))
|
||||
})),
|
||||
);
|
||||
}
|
||||
let serve_http = axum::serve(http, http_router);
|
||||
let serve_grpc = tonic::transport::Server::builder()
|
||||
.add_service(CollabService::new(collaboration).into_server())
|
||||
.serve(grpc);
|
||||
let webhook_worker = WebhookWorker::new(
|
||||
store,
|
||||
Duration::from_secs(config.webhook_interval_seconds),
|
||||
config.webhook_batch,
|
||||
)
|
||||
.map_err(RuntimeError::Configuration)?
|
||||
.run();
|
||||
|
||||
tokio::select! {
|
||||
result = serve_http => result.map_err(|error| RuntimeError::Serve(error.to_string())),
|
||||
result = serve_grpc => result.map_err(|error| RuntimeError::Serve(error.to_string())),
|
||||
result = webhook_worker => result.map_err(RuntimeError::Webhook),
|
||||
result = outbox_worker => result.map_err(RuntimeError::Outbox),
|
||||
_ = tokio::signal::ctrl_c() => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_webhook_key(value: &str) -> Result<[u8; 32], RuntimeError> {
|
||||
let decoded = hex::decode(value).map_err(|_| {
|
||||
RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_WEBHOOK_KEY must be 64 hexadecimal characters".to_owned(),
|
||||
)
|
||||
})?;
|
||||
decoded.try_into().map_err(|_| {
|
||||
RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_WEBHOOK_KEY must be 64 hexadecimal characters".to_owned(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RuntimeError {
|
||||
#[error("invalid collaboration configuration: {0}")]
|
||||
Configuration(String),
|
||||
#[error("{0} is not a valid socket address")]
|
||||
Address(String),
|
||||
#[error("cannot listen on {0}: {1}")]
|
||||
Listen(String, String),
|
||||
#[error("collaboration store failed: {0}")]
|
||||
Store(String),
|
||||
#[error("identity integration failed: {0}")]
|
||||
Identity(String),
|
||||
#[error("repository integration failed: {0}")]
|
||||
Repository(String),
|
||||
#[error("control integration failed: {0}")]
|
||||
Control(String),
|
||||
#[error("webhook delivery failed: {0}")]
|
||||
Webhook(String),
|
||||
#[error("event outbox failed: {0}")]
|
||||
Outbox(String),
|
||||
#[error("serving failed: {0}")]
|
||||
Serve(String),
|
||||
}
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use syncode_collab_api_grpc::CollabService;
|
||||
use syncode_collab_application::Collaboration;
|
||||
use syncode_collab_store::{
|
||||
PostgresCollaboration, PostgresInbox, PostgresOutbox, connect, migrate,
|
||||
};
|
||||
use tower_http::cors::{AllowOrigin, CorsLayer};
|
||||
|
||||
use crate::checks::ControlChecks;
|
||||
use crate::config::Config;
|
||||
use crate::http::router;
|
||||
use crate::identity::IdentityClientAccess;
|
||||
use crate::outbox_worker::{OutboxWorker, OutboxWorkerConfig};
|
||||
use crate::repository::RepositoryClientAccess;
|
||||
use crate::webhook_worker::WebhookWorker;
|
||||
|
||||
pub async fn run(config: Config) -> Result<(), RuntimeError> {
|
||||
if config.event_secret.is_empty() {
|
||||
return Err(RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_EVENT_SECRET must not be empty".to_owned(),
|
||||
));
|
||||
}
|
||||
if config.identity_shared_secret.is_empty() {
|
||||
return Err(RuntimeError::Configuration(
|
||||
"SYNCODE_IDENTITY_SHARED_SECRET must not be empty".to_owned(),
|
||||
));
|
||||
}
|
||||
let webhook_key = parse_webhook_key(&config.webhook_key)?;
|
||||
let pool = connect(&config.database_url, config.database_max_connections)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Store(error.to_string()))?;
|
||||
migrate(&pool)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Store(error.to_string()))?;
|
||||
|
||||
let http = tokio::net::TcpListener::bind(&config.listen_http)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Listen(config.listen_http.clone(), error.to_string()))?;
|
||||
let grpc = config
|
||||
.listen_grpc
|
||||
.parse()
|
||||
.map_err(|_| RuntimeError::Address(config.listen_grpc.clone()))?;
|
||||
|
||||
let identity = Arc::new(
|
||||
IdentityClientAccess::connect(config.identity_grpc_url, config.identity_shared_secret)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Identity(error.to_string()))?,
|
||||
);
|
||||
let repository = Arc::new(
|
||||
RepositoryClientAccess::connect(config.repository_grpc_url)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Repository(error.to_string()))?,
|
||||
);
|
||||
let checks = Arc::new(
|
||||
ControlChecks::connect(config.control_grpc_url)
|
||||
.await
|
||||
.map_err(|error| RuntimeError::Control(error.to_string()))?,
|
||||
);
|
||||
let store = PostgresCollaboration::with_webhook_key(pool.clone(), webhook_key);
|
||||
let collaboration = Arc::new(Collaboration::with_delivery(
|
||||
Arc::new(store.clone()),
|
||||
identity,
|
||||
repository,
|
||||
checks,
|
||||
));
|
||||
let inbox = Arc::new(PostgresInbox::new(pool.clone()));
|
||||
let outbox_worker = OutboxWorker::new(
|
||||
PostgresOutbox::new(pool),
|
||||
OutboxWorkerConfig {
|
||||
source_node_id: config.source_node_id,
|
||||
control_url: config.control_event_url,
|
||||
secret: config.event_secret.clone(),
|
||||
interval: Duration::from_secs(config.outbox_interval_seconds),
|
||||
batch: config.outbox_batch,
|
||||
lease: config.outbox_lease_seconds,
|
||||
},
|
||||
)
|
||||
.map_err(RuntimeError::Configuration)?
|
||||
.run();
|
||||
let mut http_router = router(
|
||||
inbox,
|
||||
Arc::clone(&collaboration),
|
||||
config.event_secret,
|
||||
config.session_cookie_name,
|
||||
);
|
||||
if !config.cors_origins.is_empty() {
|
||||
let allowed = config.cors_origins;
|
||||
http_router = http_router.layer(
|
||||
CorsLayer::new()
|
||||
.allow_credentials(true)
|
||||
.allow_methods([
|
||||
axum::http::Method::GET,
|
||||
axum::http::Method::POST,
|
||||
axum::http::Method::OPTIONS,
|
||||
])
|
||||
.allow_headers([axum::http::header::CONTENT_TYPE])
|
||||
.allow_origin(AllowOrigin::predicate(move |origin, _| {
|
||||
origin
|
||||
.to_str()
|
||||
.is_ok_and(|origin| allowed.iter().any(|value| value == origin))
|
||||
})),
|
||||
);
|
||||
}
|
||||
let serve_http = axum::serve(http, http_router);
|
||||
let serve_grpc = tonic::transport::Server::builder()
|
||||
.add_service(CollabService::new(collaboration).into_server())
|
||||
.serve(grpc);
|
||||
let webhook_worker = WebhookWorker::new(
|
||||
store,
|
||||
Duration::from_secs(config.webhook_interval_seconds),
|
||||
config.webhook_batch,
|
||||
)
|
||||
.map_err(RuntimeError::Configuration)?
|
||||
.run();
|
||||
|
||||
tokio::select! {
|
||||
result = serve_http => result.map_err(|error| RuntimeError::Serve(error.to_string())),
|
||||
result = serve_grpc => result.map_err(|error| RuntimeError::Serve(error.to_string())),
|
||||
result = webhook_worker => result.map_err(RuntimeError::Webhook),
|
||||
result = outbox_worker => result.map_err(RuntimeError::Outbox),
|
||||
_ = tokio::signal::ctrl_c() => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_webhook_key(value: &str) -> Result<[u8; 32], RuntimeError> {
|
||||
let decoded = hex::decode(value).map_err(|_| {
|
||||
RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_WEBHOOK_KEY must be 64 hexadecimal characters".to_owned(),
|
||||
)
|
||||
})?;
|
||||
decoded.try_into().map_err(|_| {
|
||||
RuntimeError::Configuration(
|
||||
"SYNCODE_COLLAB_WEBHOOK_KEY must be 64 hexadecimal characters".to_owned(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RuntimeError {
|
||||
#[error("invalid collaboration configuration: {0}")]
|
||||
Configuration(String),
|
||||
#[error("{0} is not a valid socket address")]
|
||||
Address(String),
|
||||
#[error("cannot listen on {0}: {1}")]
|
||||
Listen(String, String),
|
||||
#[error("collaboration store failed: {0}")]
|
||||
Store(String),
|
||||
#[error("identity integration failed: {0}")]
|
||||
Identity(String),
|
||||
#[error("repository integration failed: {0}")]
|
||||
Repository(String),
|
||||
#[error("control integration failed: {0}")]
|
||||
Control(String),
|
||||
#[error("webhook delivery failed: {0}")]
|
||||
Webhook(String),
|
||||
#[error("event outbox failed: {0}")]
|
||||
Outbox(String),
|
||||
#[error("serving failed: {0}")]
|
||||
Serve(String),
|
||||
}
|
||||
Reference in New Issue
Block a user