diff --git a/.gitignore b/.gitignore index 8a0f2a3..5649d47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target /data -/wkd.toml \ No newline at end of file +/config.toml \ No newline at end of file diff --git a/static/mail-template.html b/assets/mail-template.html similarity index 100% rename from static/mail-template.html rename to assets/mail-template.html diff --git a/example.wkd.toml b/example.config.toml similarity index 100% rename from example.wkd.toml rename to example.config.toml diff --git a/src/confirmation.rs b/src/confirmation.rs index 4ad15d7..b0e3466 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -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, diff --git a/src/main.rs b/src/main.rs index f72df86..ccc8ce0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() diff --git a/src/settings.rs b/src/settings.rs index 1b8b88c..a5382d5 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -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!"); diff --git a/src/utils.rs b/src/utils.rs index b4daae6..7525989 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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(""), + record.level(), + record.args() + ) +} + pub fn init_logger() -> Result { - 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() }