feat: init

This commit is contained in:
zhenyi
2026-06-07 11:30:56 +08:00
commit 563381c1ca
361 changed files with 41327 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
use serde::Serialize;
use crate::error::{AppError, AppResult};
use super::NatsQueue;
pub struct PublishResult {
pub stream: String,
pub sequence: u64,
}
impl NatsQueue {
pub async fn publish(&self, subject: &str, payload: &[u8]) -> AppResult<PublishResult> {
let subject = subject.to_string();
let ack = self
.inner
.js
.publish(subject.clone(), payload.to_vec().into())
.await
.map_err(|e| AppError::Config(format!("publish to {subject} failed: {e}")))?
.await
.map_err(|e| AppError::Config(format!("publish ack for {subject} failed: {e}")))?;
Ok(PublishResult {
stream: ack.stream,
sequence: ack.sequence,
})
}
pub async fn publish_json<T: Serialize>(
&self,
subject: &str,
payload: &T,
) -> AppResult<PublishResult> {
let data = serde_json::to_vec(payload)?;
self.publish(subject, &data).await
}
pub async fn publish_with_headers(
&self,
subject: &str,
payload: &[u8],
headers: Vec<(String, String)>,
) -> AppResult<PublishResult> {
let subject = subject.to_string();
let mut nats_headers = async_nats::HeaderMap::new();
for (k, v) in headers {
nats_headers.insert(k, v);
}
let ack = self
.inner
.js
.publish_with_headers(subject.clone(), nats_headers, payload.to_vec().into())
.await
.map_err(|e| AppError::Config(format!("publish to {subject} failed: {e}")))?
.await
.map_err(|e| AppError::Config(format!("publish ack for {subject} failed: {e}")))?;
Ok(PublishResult {
stream: ack.stream,
sequence: ack.sequence,
})
}
}