mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-12-06 14:52:41 +01:00
Improve mail text and add logger
This commit is contained in:
parent
1a85cb77fa
commit
b717852376
6 changed files with 96 additions and 11 deletions
|
@ -67,8 +67,23 @@ pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Res
|
|||
Ok(mailbox) => mailbox,
|
||||
Err(_) => return Err(Error::ParseEmail),
|
||||
})
|
||||
.subject(&SETTINGS.mail_settings.mail_subject)
|
||||
.body(format!("{action} - {token}"));
|
||||
.subject(
|
||||
SETTINGS
|
||||
.mail_settings
|
||||
.mail_subject
|
||||
.replace("%a", &action.to_string().to_lowercase()),
|
||||
)
|
||||
.body(format!(
|
||||
"{}",
|
||||
SETTINGS
|
||||
.external_url
|
||||
.join("api/")
|
||||
.unwrap()
|
||||
.join("confirm/")
|
||||
.unwrap()
|
||||
.join(token)
|
||||
.unwrap()
|
||||
));
|
||||
|
||||
let message = match email {
|
||||
Ok(message) => message,
|
||||
|
|
|
@ -12,10 +12,12 @@ use self::management::{clean_stale, store_pending_addition, store_pending_deleti
|
|||
use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
|
||||
|
||||
use actix_web::{get, post, web, App, HttpServer, Result};
|
||||
use log::error;
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tokio::{task, time};
|
||||
use utils::init_logger;
|
||||
|
||||
const PENDING_FOLDER: &str = "pending";
|
||||
|
||||
|
@ -36,13 +38,16 @@ struct Email {
|
|||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
if init_logger().is_err() {
|
||||
panic!("Could not set up logger!")
|
||||
};
|
||||
fs::create_dir_all(pending_path!())?;
|
||||
task::spawn(async {
|
||||
let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval));
|
||||
loop {
|
||||
metronome.tick().await;
|
||||
if clean_stale(SETTINGS.max_age).is_err() {
|
||||
eprintln!("Error while cleaning stale requests...");
|
||||
error!("Error while cleaning stale requests!");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -3,6 +3,7 @@ use once_cell::sync::Lazy;
|
|||
use sequoia_net::wkd::Variant;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Settings {
|
||||
|
@ -12,7 +13,7 @@ pub struct Settings {
|
|||
pub max_age: i64,
|
||||
pub cleanup_interval: u64,
|
||||
pub port: u16,
|
||||
pub external_url: String,
|
||||
pub external_url: Url,
|
||||
pub mail_settings: MailSettings,
|
||||
}
|
||||
|
||||
|
@ -41,15 +42,15 @@ pub enum SMTPEncryption {
|
|||
}
|
||||
|
||||
fn get_settings() -> Settings {
|
||||
println!("Reading settings...");
|
||||
let content = match fs::read_to_string("wkd.toml") {
|
||||
Ok(content) => content,
|
||||
Err(_) => panic!("Unable to access settings file!"),
|
||||
};
|
||||
match toml::from_str(&content) {
|
||||
let settings = match toml::from_str(&content) {
|
||||
Ok(settings) => settings,
|
||||
Err(_) => panic!("Unable to parse settings from file!"),
|
||||
}
|
||||
};
|
||||
settings
|
||||
}
|
||||
|
||||
fn get_mailer() -> SmtpTransport {
|
||||
|
@ -63,13 +64,14 @@ fn get_mailer() -> SmtpTransport {
|
|||
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host)
|
||||
}
|
||||
};
|
||||
match builder {
|
||||
let mailer = match builder {
|
||||
Ok(builder) => builder,
|
||||
Err(_) => panic!("Unable to set up smtp"),
|
||||
}
|
||||
.credentials(creds)
|
||||
.port(SETTINGS.mail_settings.smtp_port)
|
||||
.build()
|
||||
.build();
|
||||
mailer
|
||||
}
|
||||
|
||||
pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings);
|
||||
|
|
24
src/utils.rs
24
src/utils.rs
|
@ -1,6 +1,7 @@
|
|||
use crate::errors::Error;
|
||||
use crate::settings::SETTINGS;
|
||||
|
||||
use flexi_logger::{style, DeferredNow, FlexiLoggerError, Logger, LoggerHandle, Record};
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use sequoia_net::wkd::Url;
|
||||
use sequoia_openpgp::{parse::Parse, Cert};
|
||||
|
@ -58,3 +59,26 @@ pub fn key_exists(email: &str) -> Result<bool, Error> {
|
|||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn custom_format(
|
||||
w: &mut dyn std::io::Write,
|
||||
now: &mut DeferredNow,
|
||||
record: &Record,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let level = record.level();
|
||||
write!(
|
||||
w,
|
||||
"[{}] [{}] {}: {}",
|
||||
style(level).paint(now.format("%Y-%m-%d %H:%M:%S").to_string()),
|
||||
style(level).paint(record.module_path().unwrap_or("<unnamed>")),
|
||||
style(level).paint(record.level().to_string()),
|
||||
style(level).paint(&record.args().to_string())
|
||||
)
|
||||
}
|
||||
|
||||
pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
|
||||
Logger::try_with_env_or_str("simple_wkd=trace")?
|
||||
.format(custom_format)
|
||||
.set_palette("b1;3;2;4;6".to_string())
|
||||
.start()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue