gitdataai/lib/email/app.rs
2026-05-30 01:38:40 +08:00

40 lines
819 B
Rust

use config::AppConfig;
use queue::NatsProducer;
use crate::EmailMessage;
#[derive(Clone)]
pub struct AppEmail {
producer: NatsProducer,
topic: String,
}
impl AppEmail {
pub async fn init(config: &AppConfig) -> anyhow::Result<Self> {
Ok(Self {
producer: NatsProducer::new(config).await?,
topic: config.email_topic(),
})
}
pub fn with_topic(
producer: NatsProducer,
topic: impl Into<String>,
) -> Self {
Self {
producer,
topic: topic.into(),
}
}
pub async fn send(&self, message: EmailMessage) -> anyhow::Result<()> {
self.producer
.send(&self.topic, &message.to, &message, None)
.await
}
pub fn topic(&self) -> &str {
&self.topic
}
}