44 lines
941 B
Rust
44 lines
941 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 set_metrics(&mut self, registry: track::MetricsRegistry) {
|
|
self.producer.set_metrics(registry);
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|