mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 05:05:52 +01:00
Improve mailing and logging
This commit is contained in:
parent
f4cec274bf
commit
6a090414fe
7 changed files with 36 additions and 8 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,3 @@
|
||||||
/target
|
/target
|
||||||
/data
|
/data
|
||||||
/wkd.toml
|
/config.toml
|
|
@ -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> {
|
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||||
debug!("Sending email to {}", address);
|
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()
|
let email = Message::builder()
|
||||||
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
||||||
Ok(mailbox) => mailbox,
|
Ok(mailbox) => mailbox,
|
||||||
|
|
|
@ -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 actix_web::{get, post, web, App, HttpServer, Result};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::{task, time};
|
use tokio::{task, time};
|
||||||
|
@ -37,6 +38,9 @@ struct Email {
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
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() {
|
if init_logger().is_err() {
|
||||||
error!("Could not set up logger!");
|
error!("Could not set up logger!");
|
||||||
panic!("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!("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))
|
HttpServer::new(|| App::new().service(submit).service(confirm).service(delete))
|
||||||
.bind(("127.0.0.1", SETTINGS.port))?
|
.bind(("127.0.0.1", SETTINGS.port))?
|
||||||
.run()
|
.run()
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub enum SMTPEncryption {
|
||||||
|
|
||||||
fn get_settings() -> Settings {
|
fn get_settings() -> Settings {
|
||||||
debug!("Reading 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,
|
Ok(content) => content,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!("Unable to access settings file!");
|
error!("Unable to access settings file!");
|
||||||
|
|
29
src/utils.rs
29
src/utils.rs
|
@ -1,7 +1,7 @@
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
use crate::settings::SETTINGS;
|
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 rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||||
use sequoia_net::wkd::Url;
|
use sequoia_net::wkd::Url;
|
||||||
use sequoia_openpgp::{parse::Parse, Cert};
|
use sequoia_openpgp::{parse::Parse, Cert};
|
||||||
|
@ -64,7 +64,7 @@ pub fn get_filename(path: &Path) -> Option<&str> {
|
||||||
path.file_name()?.to_str()
|
path.file_name()?.to_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn custom_format(
|
pub fn custom_color_format(
|
||||||
w: &mut dyn std::io::Write,
|
w: &mut dyn std::io::Write,
|
||||||
now: &mut DeferredNow,
|
now: &mut DeferredNow,
|
||||||
record: &Record,
|
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> {
|
pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
|
||||||
Logger::try_with_env_or_str("simple_wkd=trace")?
|
Logger::try_with_env_or_str("simple_wkd=debug")?
|
||||||
.format(custom_format)
|
.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())
|
.set_palette("b1;3;2;4;6".to_string())
|
||||||
.start()
|
.start()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue