fix: read email config as a URL for optionally disabling TLS

This commit is contained in:
Aravinth Manivannan 2024-05-18 20:41:27 +05:30
parent 70c4a967bf
commit d5bcd3493b
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 16 additions and 24 deletions

View file

@ -1 +1,9 @@
export POSTGRES_DATABASE_URL="postgres://postgres:password@localhost:5432/postgres" export POSTGRES_DATABASE_URL="postgres://postgres:password@localhost:5432/postgres"
# development maildev/maildev container creds
export VANIKAM_email_USERNAME=admin
export VANIKAM_email_PASSWORD=password
export VANIKAM_email_SERVER_HOSTNAME=localhost:10025
export MAILDEV_URL=http://localhost:1080
export VANIKAM_email_FROM="Vanikam Info <info@vanikam.app>"
export VANIKAM_email_REPLY_TO="Vanikam Support <support@vanikam.app>"

View file

@ -26,8 +26,6 @@ url = "postgres://example.org" # hack for tests to run successfully
pool = 4 pool = 4
[email] [email]
username="vanikam_mailer" url="smtps://username:password@smtp.example.com:465"
password="vanikam_mailer_password"
server_hostname="smtp.vanikam.example.com"
from="Vanikam Info <vanikam@example.com>" from="Vanikam Info <vanikam@example.com>"
reply_to="Vanikam Support <vanikam@example.com>" reply_to="Vanikam Support <vanikam@example.com>"

View file

@ -7,13 +7,12 @@ use std::env;
use config::{builder::DefaultState, ConfigBuilder}; use config::{builder::DefaultState, ConfigBuilder};
use lettre::message::Mailbox; use lettre::message::Mailbox;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url;
use validator::{Validate, ValidationError}; use validator::{Validate, ValidationError};
#[derive(Debug, Clone, Serialize, Deserialize, Validate, Eq, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, Validate, Eq, PartialEq)]
pub struct Email { pub struct Email {
pub username: String, pub url: Url,
pub password: String,
pub server_hostname: String,
#[validate(custom(function = "from_address_validation"))] #[validate(custom(function = "from_address_validation"))]
pub from: String, pub from: String,
#[validate(custom(function = "reply_to_address_validation"))] #[validate(custom(function = "reply_to_address_validation"))]
@ -35,9 +34,7 @@ fn reply_to_address_validation(s: &str) -> Result<(), ValidationError> {
impl Email { impl Email {
pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> { pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for (parameter, env_var_name) in [ for (parameter, env_var_name) in [
("email.username", "VANIKAM_email_USERNAME"), ("email.url", "VANIKAM_email_URL"),
("email.password", "VANIKAM_email_PASSWORD"),
("email.server_hostname", "VANIKAM_email_SERVER_HOSTNAME"),
("email.from", "VANIKAM_email_FROM"), ("email.from", "VANIKAM_email_FROM"),
("email.reply_to", "VANIKAM_email_REPLY_TO"), ("email.reply_to", "VANIKAM_email_REPLY_TO"),
] ]
@ -62,17 +59,12 @@ mod tests {
#[test] #[test]
fn test_db_env_override() { fn test_db_env_override() {
let init_settings = crate::settings::Settings::new().unwrap(); let init_settings = crate::settings::Settings::new().unwrap();
env_helper!( env_helper!(
init_settings, init_settings,
"VANIKAM_email_USERNAME", "VANIKAM_email_URL",
"email_username", Url::parse("smtp://smtp.example.org:465").unwrap(),
email.username email.url
);
env_helper!(
init_settings,
"VANIKAM_email_PASSWORD",
"email_password",
email.password
); );
env_helper!( env_helper!(
init_settings, init_settings,
@ -86,11 +78,5 @@ mod tests {
"reploy_to@example.com", "reploy_to@example.com",
email.reply_to email.reply_to
); );
env_helper!(
init_settings,
"VANIKAM_email_SERVER_HOSTNAME",
"smtp.example.com",
email.server_hostname
);
} }
} }