aboutsummaryrefslogtreecommitdiff
path: root/src/routes/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/mod.rs')
-rw-r--r--src/routes/mod.rs111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
new file mode 100644
index 0000000..b81a0a1
--- /dev/null
+++ b/src/routes/mod.rs
@@ -0,0 +1,111 @@
+pub mod auth;
+pub mod preferences;
+pub mod query;
+
+use crate::logging::logger::AuditLogger;
+use tracing::{error, info, warn};
+
+/// Helper function to log errors to both console (tracing) and file (AuditLogger)
+/// This eliminates code duplication across route handlers
+pub fn log_error_async(
+ logging: &AuditLogger,
+ request_id: &str,
+ error_msg: &str,
+ context: Option<&str>,
+ username: Option<&str>,
+ power: Option<i32>,
+) {
+ // Log to console immediately
+ error!("[{}] {}", request_id, error_msg);
+
+ // Clone everything needed for the async task
+ let logging = logging.clone();
+ let req_id = request_id.to_string();
+ let error_msg = error_msg.to_string();
+ let context = context.map(|s| s.to_string());
+ let username = username.map(|s| s.to_string());
+
+ // Spawn async task to log to file
+ tokio::spawn(async move {
+ let _ = logging
+ .log_error(
+ &req_id,
+ chrono::Utc::now(),
+ &error_msg,
+ context.as_deref(),
+ username.as_deref(),
+ power,
+ )
+ .await;
+ });
+}
+
+/// Helper function to log warnings to both console (tracing) and file (AuditLogger)
+/// This eliminates code duplication across route handlers
+pub fn log_warning_async(
+ logging: &AuditLogger,
+ request_id: &str,
+ message: &str,
+ context: Option<&str>,
+ username: Option<&str>,
+ power: Option<i32>,
+) {
+ // Log to console immediately
+ warn!("[{}] {}", request_id, message);
+
+ // Clone everything needed for the async task
+ let logging = logging.clone();
+ let req_id = request_id.to_string();
+ let message = message.to_string();
+ let context = context.map(|s| s.to_string());
+ let username = username.map(|s| s.to_string());
+
+ // Spawn async task to log to file
+ tokio::spawn(async move {
+ let _ = logging
+ .log_warning(
+ &req_id,
+ chrono::Utc::now(),
+ &message,
+ context.as_deref(),
+ username.as_deref(),
+ power,
+ )
+ .await;
+ });
+}
+
+/// Helper function to log info messages to both console (tracing) and file (AuditLogger)
+/// This eliminates code duplication across route handlers
+pub fn log_info_async(
+ logging: &AuditLogger,
+ request_id: &str,
+ message: &str,
+ context: Option<&str>,
+ username: Option<&str>,
+ power: Option<i32>,
+) {
+ // Log to console immediately
+ info!("[{}] {}", request_id, message);
+
+ // Clone everything needed for the async task
+ let logging = logging.clone();
+ let req_id = request_id.to_string();
+ let message = message.to_string();
+ let context = context.map(|s| s.to_string());
+ let username = username.map(|s| s.to_string());
+
+ // Spawn async task to log to file
+ tokio::spawn(async move {
+ let _ = logging
+ .log_info(
+ &req_id,
+ chrono::Utc::now(),
+ &message,
+ context.as_deref(),
+ username.as_deref(),
+ power,
+ )
+ .await;
+ });
+}