feat: v0.3.0 — standardize codebase, centralize infra, merge QR into CMS
- Enforce axum best practices across all 13 workspace crates (max 200 LOC/file, no comments, no unwrap, clean architecture) - Fix domain→infrastructure dependency inversions in imphnen-iam and imphnen-dimentorin - Extract imphnen-storage (MinIO) and imphnen-email (Lettre) as standalone crates - Centralize all config in ENV struct: CDN_URL, CORS_ALLOWED_ORIGINS - Centralize SMTP through imphnen-email; remove dead HackathonConfig - Centralize database: QR crate now shares main DB pool (single DATABASE_URL) - Rename QR users table to qr_users to avoid collision with main users table - Merge imphnen-qr into imphnen-cms/src/qr (13 crates, down from 14) - Restructure imphnen-hackathon flat modules into clean architecture - Remove all stale env vars from .env.example (SurrealDB, QR_JWT, Hackathon infra) - Fix Dockerfile to include all current workspace crates - Bump all crate versions 0.2.0 → 0.3.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
2ae43b3bcc
commit
331a4a4e88
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "imphnen-email"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
imphnen-libs.workspace = true
|
||||
lettre.workspace = true
|
||||
tracing.workspace = true
|
||||
@@ -0,0 +1,20 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EmailError {
|
||||
SmtpConfig(String),
|
||||
MessageBuild(String),
|
||||
Transport(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for EmailError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
EmailError::SmtpConfig(msg) => write!(f, "SMTP configuration error: {msg}"),
|
||||
EmailError::MessageBuild(msg) => write!(f, "Message building error: {msg}"),
|
||||
EmailError::Transport(msg) => write!(f, "SMTP transport error: {msg}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for EmailError {}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod error;
|
||||
pub mod service;
|
||||
|
||||
pub use error::EmailError;
|
||||
pub use service::send_email;
|
||||
@@ -0,0 +1,48 @@
|
||||
use imphnen_libs::{ENV, Env};
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::transport::smtp::authentication::Credentials;
|
||||
use lettre::{Message, SmtpTransport, Transport};
|
||||
use std::error::Error;
|
||||
|
||||
use crate::error::EmailError;
|
||||
|
||||
pub fn send_email(
|
||||
to: &str,
|
||||
subject: &str,
|
||||
body: &str,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let env = &ENV;
|
||||
let message = build_message(to, subject, body, env)?;
|
||||
let mailer = build_transport(env)?;
|
||||
mailer.send(&message).map_err(|e| {
|
||||
tracing::error!("Failed to send email to {}: {}", to, e);
|
||||
Box::new(EmailError::Transport(e.to_string())) as Box<dyn Error>
|
||||
})?;
|
||||
tracing::info!("Email sent to: {}", to);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build_message(
|
||||
to: &str,
|
||||
subject: &str,
|
||||
body: &str,
|
||||
env: &Env,
|
||||
) -> Result<Message, Box<dyn Error>> {
|
||||
let sender_name = env.smtp_name.replace("-", " ");
|
||||
Message::builder()
|
||||
.from(Mailbox::new(Some(sender_name), env.smtp_email.parse()?))
|
||||
.to(to.parse()?)
|
||||
.subject(subject)
|
||||
.body(body.to_string())
|
||||
.map_err(|e| Box::new(EmailError::MessageBuild(e.to_string())) as Box<dyn Error>)
|
||||
}
|
||||
|
||||
fn build_transport(env: &Env) -> Result<SmtpTransport, Box<dyn Error>> {
|
||||
let credentials =
|
||||
Credentials::new(env.smtp_email.clone(), env.smtp_password.replace("-", " "));
|
||||
Ok(
|
||||
SmtpTransport::relay(&env.smtp_host)?
|
||||
.credentials(credentials)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user