repos

darkfurrow.com-rust

mirror archived upstream

A living almanac of seasons, soil, and the quiet knowledge that used to be common. Rust axum with minijinja and Vite.

agriculturealmanacaxumfolk-knowledgegardeningminijinjarustseasonalvite

883 B · 28 lines · Rust Raw History
 1use axum::extract::Request;
 2use axum::middleware::Next;
 3use axum::response::Response;
 4use chrono::Local;
 5use std::time::Instant;
 6
 7pub async fn log_requests(req: Request, next: Next) -> Response {
 8    let method = req.method().clone();
 9    let path = req
10        .uri()
11        .path_and_query()
12        .map(|p| p.as_str().to_string())
13        .unwrap_or_else(|| req.uri().path().to_string());
14    let start = Instant::now();
15    let response = next.run(req).await;
16    let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
17    let status = response.status().as_u16();
18    let now = Local::now().format("%H:%M:%S");
19    let color = match status {
20        200..=299 => "\x1b[32m",
21        300..=399 => "\x1b[36m",
22        400..=499 => "\x1b[33m",
23        _ => "\x1b[31m",
24    };
25    eprintln!("{now} {method:<5} {color}{status}\x1b[0m {elapsed_ms:>7.2}ms  {path}");
26    response
27}