From 163900c32b21e2dcac2cef290a8bea5fbb2f7c14 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Wed, 4 Oct 2023 09:14:31 -0700 Subject: [PATCH] fix(all): lint and fix logger --- backend/src/logger.rs | 14 +++++------ backend/src/main.rs | 42 +++++++++++++++++--------------- backend/src/plugins/minecraft.rs | 2 +- backend/src/web/middleware.rs | 7 ++++-- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/backend/src/logger.rs b/backend/src/logger.rs index 660d1c3..e9371c6 100644 --- a/backend/src/logger.rs +++ b/backend/src/logger.rs @@ -30,27 +30,27 @@ pub fn append_logfile(message: String) { .unwrap(); } -pub fn print_pretty(kind: String, message: String) { +pub fn print_pretty(kind: String, message: impl AsRef + std::fmt::Display) { let pretty_date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); println!("[{}] {} {}", pretty_date, kind.bold(), message); } -pub fn _print_error(message: String) { - print_pretty("[ERROR]".red().to_string(), message.clone()); +pub fn _print_error(message: impl AsRef + std::fmt::Display) { + print_pretty("[ERROR]".red().to_string(), &message); // Write to log file append_logfile(format!("[ERROR] {}", message)); } -pub fn _print_warning(message: String) { - print_pretty("[WARNING]".yellow().to_string(), message.clone()); +pub fn _print_warning(message: impl AsRef + std::fmt::Display) { + print_pretty("[WARNING]".yellow().to_string(), &message); // Write to log file append_logfile(format!("[WARNING] {}", message)); } -pub fn print_info(message: String) { - print_pretty("[INFO]".blue().to_string(), message.clone()); +pub fn print_info(message: impl AsRef + std::fmt::Display) { + print_pretty("[INFO]".blue().to_string(), &message); // Write to log file append_logfile(format!("[INFO] {}", message)); diff --git a/backend/src/main.rs b/backend/src/main.rs index 881baf7..9778819 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -5,9 +5,9 @@ use rpassword::read_password; use sha2::Digest; use std::{io::Write, path::Path}; use tide::utils::async_trait; -use tide_http_auth::{BasicAuthRequest, Storage}; -use tide_acme::{AcmeConfig, TideRustlsExt}; use tide_acme::rustls_acme::caches::DirCache; +use tide_acme::{AcmeConfig, TideRustlsExt}; +use tide_http_auth::{BasicAuthRequest, Storage}; #[cfg(feature = "plugins")] use crate::plugins::parse_enable_plugins; @@ -168,32 +168,36 @@ fn main() { parse_enable_plugins(&mut app, args.plugins.clone(), args.address.clone()); if args.https { - logger::print_info(format!("Putting ACME cache in {}/.acme_cache", tmp_dir.display().to_string())); + logger::print_info(format!( + "Putting ACME cache in {}/.acme_cache", + tmp_dir.display() + )); } logger::print_info(format!("Starting server on port {}...", args.port)); - logger::print_info(format!("Retaining {} elements of metric history", args.history_max)); + logger::print_info(format!( + "Retaining {} elements of metric history", + args.history_max + )); logger::print_info(format!("Updating every {} seconds", args.update_rate)); - logger::print_info( - format!( - "Done! Access the web interface at http{}://{}:{}/", - // Lol this is so dumb - if args.https { "s" } else { "" }, - args.address, - args.port - ) - ); + logger::print_info(format!( + "Done! Access the web interface at http{}://{}:{}/", + // Lol this is so dumb + if args.https { "s" } else { "" }, + args.address, + args.port + )); task::block_on(async { if args.https { app - .listen( + .listen( tide_rustls::TlsListener::build() - .acme( - AcmeConfig::new(vec![args.address.clone()]) - .cache(DirCache::new(format!("{}/.acme_cache", tmp_dir.display().to_string()))) - ) - .addrs(format!("{}:{}", args.address.as_str(), args.port)) + .acme( + AcmeConfig::new(vec![args.address.clone()]) + .cache(DirCache::new(format!("{}/.acme_cache", tmp_dir.display()))), + ) + .addrs(format!("{}:{}", args.address.as_str(), args.port)), ) .await .unwrap(); diff --git a/backend/src/plugins/minecraft.rs b/backend/src/plugins/minecraft.rs index bc2072e..45cb929 100644 --- a/backend/src/plugins/minecraft.rs +++ b/backend/src/plugins/minecraft.rs @@ -1,7 +1,7 @@ use mcping; use serde::Serialize; -use crate::{State, logger}; +use crate::{logger, State}; #[derive(Serialize, Clone)] struct McData { diff --git a/backend/src/web/middleware.rs b/backend/src/web/middleware.rs index 9c1cac9..38f5eb7 100644 --- a/backend/src/web/middleware.rs +++ b/backend/src/web/middleware.rs @@ -1,6 +1,6 @@ use tide::utils::async_trait; -use crate::{User, logger}; +use crate::{logger, User}; pub struct AuthMiddleware {} @@ -14,7 +14,10 @@ where let mut res: tide::Response = tide::Response::new(401); res.insert_header("WWW-Authenticate", "Basic"); - logger::print_info(format!("Attempted access by {}", req.remote().unwrap_or("unknown"))); + logger::print_info(format!( + "Attempted access by {}", + req.remote().unwrap_or("unknown") + )); return Ok(res); }