Clean-room Rust rewrite (written without reference to useSend source). Crates: mail-types, mail-core, mail-server, mail-auth, mail-submit, mail-hooks, mail-deliverability; sdks/ts (@scribase/mail); docs/. Each verified by running locally (see plans/scribase-mail-rust-2026-11-12.md). mail-mta, mail-inbound, mail-oci are still in progress and not in this commit. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
123 lines
4.0 KiB
Rust
123 lines
4.0 KiB
Rust
//! Standalone dev server for mail-auth on 127.0.0.1:8791.
|
|
//!
|
|
//! Mail is not sent: every message is written to
|
|
//! `/tmp/mail-auth-outbox/<id>.eml` and logged, so the flows can be driven
|
|
//! end to end with curl.
|
|
//!
|
|
//! createdb mail_dev_auth
|
|
//! AUTH_ADMIN_TOKEN=dev-admin cargo run -p mail-auth --example dev_server
|
|
|
|
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
|
|
|
|
use async_trait::async_trait;
|
|
use mail_auth::{AuthConfig, AuthState, SigningKeys};
|
|
use mail_types::{EmailSender, OutgoingEmail, SendError};
|
|
use sqlx::postgres::PgPoolOptions;
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
const BIND: &str = "127.0.0.1:8791";
|
|
const DEFAULT_DATABASE_URL: &str = "postgres://localhost/mail_dev_auth";
|
|
const OUTBOX: &str = "/tmp/mail-auth-outbox";
|
|
|
|
/// `EmailSender` that writes `.eml` files instead of talking to SMTP.
|
|
struct LogSender {
|
|
dir: PathBuf,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl EmailSender for LogSender {
|
|
async fn send(&self, email: OutgoingEmail) -> Result<String, SendError> {
|
|
let id = uuid::Uuid::new_v4().to_string();
|
|
let path = self.dir.join(format!("{id}.eml"));
|
|
let body = render_eml(&id, &email);
|
|
tokio::fs::write(&path, body)
|
|
.await
|
|
.map_err(|e| SendError::Other(format!("write {}: {e}", path.display())))?;
|
|
tracing::info!(
|
|
id = %id,
|
|
to = ?email.to,
|
|
subject = %email.subject,
|
|
file = %path.display(),
|
|
"outbox: message written"
|
|
);
|
|
Ok(id)
|
|
}
|
|
}
|
|
|
|
fn render_eml(id: &str, email: &OutgoingEmail) -> String {
|
|
let boundary = format!("b-{id}");
|
|
let mut out = String::new();
|
|
out.push_str(&format!("Message-ID: <{id}@mail-auth.local>\r\n"));
|
|
out.push_str(&format!("From: {}\r\n", email.from));
|
|
out.push_str(&format!("To: {}\r\n", email.to.join(", ")));
|
|
out.push_str(&format!("Subject: {}\r\n", email.subject));
|
|
for (k, v) in &email.tags {
|
|
out.push_str(&format!("X-Tag-{k}: {v}\r\n"));
|
|
}
|
|
out.push_str("MIME-Version: 1.0\r\n");
|
|
out.push_str(&format!(
|
|
"Content-Type: multipart/alternative; boundary=\"{boundary}\"\r\n\r\n"
|
|
));
|
|
if let Some(text) = &email.text {
|
|
out.push_str(&format!(
|
|
"--{boundary}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{text}\r\n"
|
|
));
|
|
}
|
|
if let Some(html) = &email.html {
|
|
out.push_str(&format!(
|
|
"--{boundary}\r\nContent-Type: text/html; charset=utf-8\r\n\r\n{html}\r\n"
|
|
));
|
|
}
|
|
out.push_str(&format!("--{boundary}--\r\n"));
|
|
out
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
tracing_subscriber::fmt()
|
|
.with_env_filter(
|
|
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info,sqlx=warn")),
|
|
)
|
|
.init();
|
|
|
|
if let Err(e) = run().await {
|
|
tracing::error!(error = %e, "dev server failed");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
async fn run() -> Result<(), Box<dyn std::error::Error>> {
|
|
let database_url =
|
|
std::env::var("DATABASE_URL").unwrap_or_else(|_| DEFAULT_DATABASE_URL.to_string());
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(8)
|
|
.connect(&database_url)
|
|
.await?;
|
|
mail_auth::migrate(&pool).await?;
|
|
tracing::info!(database = %database_url, "migrations applied");
|
|
|
|
let config = AuthConfig::from_env()?;
|
|
if config.admin_token.is_none() {
|
|
tracing::warn!("AUTH_ADMIN_TOKEN not set: admin endpoints will answer 503");
|
|
}
|
|
let keys = SigningKeys::from_env_or_generate(AuthConfig::is_production())?;
|
|
|
|
let outbox = PathBuf::from(OUTBOX);
|
|
tokio::fs::create_dir_all(&outbox).await?;
|
|
let sender: Arc<dyn EmailSender> = Arc::new(LogSender { dir: outbox });
|
|
|
|
let app = mail_auth::router(AuthState::new(pool, sender, keys, config));
|
|
let listener = tokio::net::TcpListener::bind(BIND).await?;
|
|
tracing::info!(
|
|
bind = BIND,
|
|
outbox = OUTBOX,
|
|
"mail-auth dev server listening"
|
|
);
|
|
axum::serve(
|
|
listener,
|
|
app.into_make_service_with_connect_info::<SocketAddr>(),
|
|
)
|
|
.await?;
|
|
Ok(())
|
|
}
|