feat: modularize codebase and add multi-route support with live reload
This commit is contained in:
52
src/middleware.rs
Normal file
52
src/middleware.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Request, State},
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
};
|
||||
use chrono::Local;
|
||||
use std::time::Instant;
|
||||
use crate::state::SharedState;
|
||||
|
||||
// Note: Axum body might need to be imported differently depending on version
|
||||
// but since the original used axum::body::Body, I'll stick to that or similar.
|
||||
// Actually, let's use axum::body::Body.
|
||||
|
||||
pub async fn logging_middleware(
|
||||
State(_state): State<SharedState>,
|
||||
req: Request<Body>,
|
||||
next: Next,
|
||||
) -> Response {
|
||||
let start_time = Local::now();
|
||||
let start_instant = Instant::now();
|
||||
|
||||
// Load config live for masking settings
|
||||
let config = crate::config::Config::load();
|
||||
|
||||
println!("---- REQUEST START {} ----", start_time.format("%Y-%m-%d %H:%M:%S"));
|
||||
println!("Method: {}", req.method());
|
||||
println!("Path: {}", req.uri().path());
|
||||
println!("Headers:");
|
||||
|
||||
for (name, value) in req.headers() {
|
||||
let name_str = name.as_str();
|
||||
|
||||
let should_mask = config.masking.enabled &&
|
||||
config.masking.headers.iter().any(|h| h.eq_ignore_ascii_case(name_str));
|
||||
|
||||
if should_mask {
|
||||
println!(" {}: ***", name_str);
|
||||
} else {
|
||||
println!(" {}: {:?}", name_str, value);
|
||||
}
|
||||
}
|
||||
|
||||
let response = next.run(req).await;
|
||||
|
||||
let end_time = Local::now();
|
||||
let duration = start_instant.elapsed();
|
||||
|
||||
println!("--- REQUEST END {} - {:?} ---------", end_time.format("%H:%M:%S"), duration);
|
||||
|
||||
response
|
||||
}
|
||||
Reference in New Issue
Block a user