Add worker service with e-mail delivery and dev Mailpit
- worker: sends email_outbox mail over SMTP (lettre), woken by LISTEN/NOTIFY, SKIP LOCKED claiming, retries with backoff, hu/en templates (text + HTML), hourly cleanup of expired data - migration 0002: next_attempt_at, failed_at, notify trigger - docker-compose.override.yml: Mailpit for development only - Dockerfiles: lock shared cargo cache mounts for parallel builds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
//! Configuration from environment variables.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
use lettre::message::Mailbox;
|
||||
|
||||
pub struct Config {
|
||||
pub database_url: String,
|
||||
/// `smtp://host:port` (plain, e.g. Mailpit), `smtp://user:pass@host:587?tls=required`
|
||||
/// (STARTTLS) or `smtps://...` (TLS). Unset: e-mails stay in the outbox.
|
||||
pub smtp_url: Option<String>,
|
||||
/// Sender, e.g. `Sorsfonalak <noreply@sorsfonalak.hu>`.
|
||||
pub mail_from: Mailbox,
|
||||
/// Fallback polling interval; new e-mails are picked up at once via LISTEN/NOTIFY.
|
||||
pub poll_interval: Duration,
|
||||
/// After this many failed attempts an e-mail is marked as failed.
|
||||
pub max_email_attempts: i32,
|
||||
}
|
||||
|
||||
fn var(name: &str) -> Option<String> {
|
||||
std::env::var(name).ok().map(|v| v.trim().to_owned()).filter(|v| !v.is_empty())
|
||||
}
|
||||
|
||||
fn parsed<T: std::str::FromStr>(name: &str, default: T) -> anyhow::Result<T>
|
||||
where
|
||||
T::Err: std::fmt::Display,
|
||||
{
|
||||
match var(name) {
|
||||
Some(raw) => raw.parse().map_err(|e| anyhow::anyhow!("invalid {name} '{raw}': {e}")),
|
||||
None => Ok(default),
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_env() -> anyhow::Result<Self> {
|
||||
let mail_from = var("MAIL_FROM").unwrap_or_else(|| "Sorsfonalak <noreply@sorsfonalak.local>".into());
|
||||
Ok(Self {
|
||||
database_url: var("DATABASE_URL").context("DATABASE_URL is required")?,
|
||||
smtp_url: var("SMTP_URL"),
|
||||
mail_from: mail_from.parse().with_context(|| format!("invalid MAIL_FROM '{mail_from}'"))?,
|
||||
poll_interval: Duration::from_secs(parsed("POLL_INTERVAL_SECS", 30)?),
|
||||
max_email_attempts: parsed("MAX_EMAIL_ATTEMPTS", 8)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user