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

Improve mailing and logging

This commit is contained in:
Delta1925 2023-04-15 16:40:42 +02:00
parent f4cec274bf
commit 6a090414fe
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
7 changed files with 36 additions and 8 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
/target
/data
/wkd.toml
/config.toml

View file

@ -104,7 +104,7 @@ pub fn confirm_action(token: &str) -> Result<(Action, String), Error> {
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<(), Error> {
debug!("Sending email to {}", address);
let template = fs::read_to_string(Path::new("static").join("mail-template.html")).unwrap();
let template = fs::read_to_string(Path::new("assets").join("mail-template.html")).unwrap();
let email = Message::builder()
.from(match SETTINGS.mail_settings.mail_from.parse() {
Ok(mailbox) => mailbox,

View file

@ -13,6 +13,7 @@ use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
use actix_web::{get, post, web, App, HttpServer, Result};
use log::{error, info};
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::Path;
use tokio::{task, time};
@ -37,6 +38,9 @@ struct Email {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
if let Ok(value) = env::var("RUST_LOG") {
env::set_var("RUST_LOG", format!("simple_wkd={}", value));
}
if init_logger().is_err() {
error!("Could not set up logger!");
panic!("Could not set up logger!")
@ -51,7 +55,10 @@ async fn main() -> std::io::Result<()> {
info!("Cleanup completed!");
}
});
info!("Running server on http://127.0.0.1:{} (External URL: {})", SETTINGS.port, SETTINGS.external_url);
info!(
"Running server on http://127.0.0.1:{} (External URL: {})",
SETTINGS.port, SETTINGS.external_url
);
HttpServer::new(|| App::new().service(submit).service(confirm).service(delete))
.bind(("127.0.0.1", SETTINGS.port))?
.run()

View file

@ -44,7 +44,7 @@ pub enum SMTPEncryption {
fn get_settings() -> Settings {
debug!("Reading settings...");
let content = match fs::read_to_string("wkd.toml") {
let content = match fs::read_to_string("config.toml") {
Ok(content) => content,
Err(_) => {
error!("Unable to access settings file!");

View file

@ -1,7 +1,7 @@
use crate::errors::Error;
use crate::settings::SETTINGS;
use flexi_logger::{style, DeferredNow, FlexiLoggerError, Logger, LoggerHandle, Record};
use flexi_logger::{style, DeferredNow, FileSpec, FlexiLoggerError, Logger, LoggerHandle, Record};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use sequoia_net::wkd::Url;
use sequoia_openpgp::{parse::Parse, Cert};
@ -64,7 +64,7 @@ pub fn get_filename(path: &Path) -> Option<&str> {
path.file_name()?.to_str()
}
pub fn custom_format(
pub fn custom_color_format(
w: &mut dyn std::io::Write,
now: &mut DeferredNow,
record: &Record,
@ -80,9 +80,30 @@ pub fn custom_format(
)
}
pub fn custom_monochrome_format(
w: &mut dyn std::io::Write,
now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
write!(
w,
"[{}] [{}] {}: {}",
now.format("%Y-%m-%d %H:%M:%S"),
record.module_path().unwrap_or("<unnamed>"),
record.level(),
record.args()
)
}
pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
Logger::try_with_env_or_str("simple_wkd=trace")?
.format(custom_format)
Logger::try_with_env_or_str("simple_wkd=debug")?
.log_to_file(FileSpec::default().directory("logs"))
.duplicate_to_stdout(flexi_logger::Duplicate::All)
.format_for_files(custom_monochrome_format)
.adaptive_format_for_stdout(flexi_logger::AdaptiveFormat::Custom(
custom_monochrome_format,
custom_color_format,
))
.set_palette("b1;3;2;4;6".to_string())
.start()
}