Skip to content

Commit

Permalink
fix(all): lint and fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Oct 4, 2023
1 parent c277fd4 commit 163900c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
14 changes: 7 additions & 7 deletions backend/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str> + 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<str> + 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<str> + 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<str> + std::fmt::Display) {
print_pretty("[INFO]".blue().to_string(), &message);

// Write to log file
append_logfile(format!("[INFO] {}", message));
Expand Down
42 changes: 23 additions & 19 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/minecraft.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mcping;
use serde::Serialize;

use crate::{State, logger};
use crate::{logger, State};

#[derive(Serialize, Clone)]
struct McData {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/web/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tide::utils::async_trait;

use crate::{User, logger};
use crate::{logger, User};

pub struct AuthMiddleware {}

Expand All @@ -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);
}
Expand Down

0 comments on commit 163900c

Please sign in to comment.