0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-10-30 09:05:52 +01:00
simple-wkd/src/settings.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

2023-04-14 14:23:28 +02:00
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport};
2023-04-14 00:52:54 +02:00
use once_cell::sync::Lazy;
use sequoia_net::wkd::Variant;
use serde::{Deserialize, Serialize};
2023-04-14 12:18:49 +02:00
use std::fs;
2023-04-14 16:33:59 +02:00
use url::Url;
2023-04-14 00:52:54 +02:00
#[derive(Serialize, Deserialize, Debug)]
pub struct Settings {
#[serde(with = "VariantDef")]
pub variant: Variant,
2023-04-14 12:18:49 +02:00
pub root_folder: String,
2023-04-14 00:52:54 +02:00
pub max_age: i64,
2023-04-14 01:05:27 +02:00
pub cleanup_interval: u64,
2023-04-14 00:52:54 +02:00
pub port: u16,
2023-04-14 16:33:59 +02:00
pub external_url: Url,
2023-04-14 12:18:49 +02:00
pub mail_settings: MailSettings,
2023-04-14 00:52:54 +02:00
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MailSettings {
pub smtp_host: String,
pub smtp_username: String,
pub smtp_password: String,
pub smtp_port: u16,
2023-04-14 12:18:49 +02:00
pub smtp_tls: SMTPEncryption,
2023-04-14 00:52:54 +02:00
pub mail_from: String,
pub mail_subject: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(remote = "Variant")]
pub enum VariantDef {
Advanced,
Direct,
}
2023-04-14 12:18:49 +02:00
#[derive(Serialize, Deserialize, Debug)]
pub enum SMTPEncryption {
Tls,
Starttls,
}
2023-04-14 00:52:54 +02:00
fn get_settings() -> Settings {
2023-04-14 12:18:49 +02:00
let content = match fs::read_to_string("wkd.toml") {
Ok(content) => content,
Err(_) => panic!("Unable to access settings file!"),
};
2023-04-14 16:33:59 +02:00
let settings = match toml::from_str(&content) {
2023-04-14 12:18:49 +02:00
Ok(settings) => settings,
Err(_) => panic!("Unable to parse settings from file!"),
2023-04-14 16:33:59 +02:00
};
settings
2023-04-14 00:52:54 +02:00
}
2023-04-14 14:23:28 +02:00
fn get_mailer() -> SmtpTransport {
let creds = Credentials::new(
SETTINGS.mail_settings.smtp_username.to_owned(),
SETTINGS.mail_settings.smtp_password.to_owned(),
);
let builder = match &SETTINGS.mail_settings.smtp_tls {
SMTPEncryption::Tls => SmtpTransport::relay(&SETTINGS.mail_settings.smtp_host),
SMTPEncryption::Starttls => {
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host)
}
};
2023-04-14 16:33:59 +02:00
let mailer = match builder {
2023-04-14 14:23:28 +02:00
Ok(builder) => builder,
Err(_) => panic!("Unable to set up smtp"),
}
.credentials(creds)
.port(SETTINGS.mail_settings.smtp_port)
2023-04-14 16:33:59 +02:00
.build();
mailer
2023-04-14 14:23:28 +02:00
}
2023-04-14 00:52:54 +02:00
pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings);
2023-04-14 14:23:28 +02:00
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer);