refactor(nats): strip NatsEventPublisher, add NatsTransport implementing Transport
This commit is contained in:
@@ -4,13 +4,14 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
domain = { workspace = true }
|
domain = { workspace = true }
|
||||||
event-payload = { workspace = true }
|
event-payload = { workspace = true }
|
||||||
async-nats = { workspace = true }
|
event-publisher = { workspace = true }
|
||||||
async-stream = { workspace = true }
|
async-nats = { workspace = true }
|
||||||
serde_json = { workspace = true }
|
async-stream = { workspace = true }
|
||||||
futures = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
tokio = { workspace = true }
|
futures = { workspace = true }
|
||||||
async-trait = { workspace = true }
|
tokio = { workspace = true }
|
||||||
tracing = { workspace = true }
|
async-trait = { workspace = true }
|
||||||
uuid = { workspace = true }
|
tracing = { workspace = true }
|
||||||
|
uuid = { workspace = true }
|
||||||
|
|||||||
@@ -2,36 +2,33 @@ use async_trait::async_trait;
|
|||||||
use domain::{
|
use domain::{
|
||||||
errors::DomainError,
|
errors::DomainError,
|
||||||
events::{DomainEvent, EventEnvelope},
|
events::{DomainEvent, EventEnvelope},
|
||||||
ports::{EventConsumer, EventPublisher},
|
ports::EventConsumer,
|
||||||
};
|
};
|
||||||
use event_payload::EventPayload;
|
use event_payload::EventPayload;
|
||||||
|
use event_publisher::Transport;
|
||||||
use futures::stream::BoxStream;
|
use futures::stream::BoxStream;
|
||||||
|
|
||||||
// ── NatsEventPublisher ────────────────────────────────────────────────────
|
// ── NatsTransport — raw NATS publish backend ────────────────────────────────
|
||||||
|
|
||||||
pub struct NatsEventPublisher {
|
pub struct NatsTransport {
|
||||||
client: async_nats::Client,
|
client: async_nats::Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NatsEventPublisher {
|
impl NatsTransport {
|
||||||
pub fn new(client: async_nats::Client) -> Self { Self { client } }
|
pub fn new(client: async_nats::Client) -> Self { Self { client } }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventPublisher for NatsEventPublisher {
|
impl Transport for NatsTransport {
|
||||||
async fn publish(&self, event: &DomainEvent) -> Result<(), DomainError> {
|
async fn publish_bytes(&self, subject: &str, bytes: &[u8]) -> Result<(), DomainError> {
|
||||||
let payload = EventPayload::from(event);
|
|
||||||
let subject = payload.subject();
|
|
||||||
let bytes = serde_json::to_vec(&payload)
|
|
||||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
|
||||||
self.client
|
self.client
|
||||||
.publish(subject, bytes.into())
|
.publish(subject.to_string(), bytes.to_vec().into())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── NatsEventConsumer ─────────────────────────────────────────────────────
|
// ── NatsEventConsumer — subscribes and yields EventEnvelopes ────────────────
|
||||||
|
|
||||||
pub struct NatsEventConsumer {
|
pub struct NatsEventConsumer {
|
||||||
client: async_nats::Client,
|
client: async_nats::Client,
|
||||||
@@ -66,7 +63,6 @@ impl EventConsumer for NatsEventConsumer {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Basic NATS: no ack/nack (at-most-once delivery)
|
|
||||||
yield EventEnvelope {
|
yield EventEnvelope {
|
||||||
event,
|
event,
|
||||||
ack: Box::new(|| {}),
|
ack: Box::new(|| {}),
|
||||||
|
|||||||
Reference in New Issue
Block a user