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/backend/src/settings.rs

98 lines
2.7 KiB
Rust
Raw Normal View History

2023-04-14 14:23:28 +02:00
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport};
2023-04-14 18:55:17 +02:00
use log::{debug, error, warn};
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,
pub max_age: i64,
2023-04-14 01:05:27 +02:00
pub cleanup_interval: u64,
2023-04-15 21:08:24 +02:00
pub allowed_domains: Vec<String>,
2023-04-14 00:52:54 +02:00
pub port: u16,
2023-04-16 19:08:46 +02:00
pub bind_host: String,
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 18:55:17 +02:00
debug!("Reading settings...");
2023-04-15 16:40:42 +02:00
let content = match fs::read_to_string("config.toml") {
2023-04-14 12:18:49 +02:00
Ok(content) => content,
2023-04-14 18:55:17 +02:00
Err(_) => {
error!("Unable to access settings file!");
panic!("Unable to access settings file!")
}
2023-04-14 12:18:49 +02:00
};
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,
2023-04-14 18:55:17 +02:00
Err(_) => {
error!("Unable to parse settings from file!");
panic!("Unable to parse settings from file!")
}
2023-04-14 16:33:59 +02:00
};
2023-04-14 18:55:17 +02:00
debug!("Successfully read setting!");
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 {
2023-04-14 18:55:17 +02:00
debug!("Setting up SMTP...");
2023-04-14 14:23:28 +02:00
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,
2023-04-14 18:55:17 +02:00
Err(_) => {
error!("Unable to set up smtp");
panic!("Unable to set up smtp")
}
2023-04-14 14:23:28 +02:00
}
.credentials(creds)
.port(SETTINGS.mail_settings.smtp_port)
2023-04-14 16:33:59 +02:00
.build();
2023-04-14 18:55:17 +02:00
if mailer.test_connection().is_err() {
warn!("Connection test to smtp host failed!");
}
debug!("SMTP setup successful!");
2023-04-14 16:33:59 +02:00
mailer
2023-04-14 14:23:28 +02:00
}
2023-04-16 13:58:52 +02:00
pub const ROOT_FOLDER: &str = "data";
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);