feat: add middleware for logging requests and responses
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{ConnectInfo, Path, Query, State},
|
extract::{ConnectInfo, Path, Query, State},
|
||||||
http::{HeaderMap, StatusCode},
|
http::{HeaderMap, StatusCode, Method, Uri},
|
||||||
response::{Html, Json},
|
response::{Html, Json},
|
||||||
routing::get,
|
routing::get,
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn, trace};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -16,10 +16,12 @@ use std::net::SocketAddr;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Instant;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::CorsLayer;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
|
use tower::Layer;
|
||||||
|
|
||||||
use crate::compression_engine::{CompressionType, get_compression_engine};
|
use crate::compression_engine::{CompressionType, get_compression_engine};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
@@ -126,6 +128,7 @@ async fn run_server(
|
|||||||
.route("/content/:id", get(handle_get_content))
|
.route("/content/:id", get(handle_get_content))
|
||||||
.route("/openapi.json", get(handle_openapi))
|
.route("/openapi.json", get(handle_openapi))
|
||||||
.route("/swagger/", get(handle_swagger_ui))
|
.route("/swagger/", get(handle_swagger_ui))
|
||||||
|
.layer(axum::middleware::from_fn(logging_middleware))
|
||||||
.layer(
|
.layer(
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
@@ -152,6 +155,28 @@ async fn run_server(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Custom middleware for logging requests and responses
|
||||||
|
async fn logging_middleware<B>(
|
||||||
|
req: http::Request<B>,
|
||||||
|
next: axum::middleware::Next<B>,
|
||||||
|
) -> Result<http::Response<axum::body::Body>, axum::response::Response> {
|
||||||
|
let method = req.method().clone();
|
||||||
|
let uri = req.uri().clone();
|
||||||
|
let headers = req.headers().clone();
|
||||||
|
|
||||||
|
// Log incoming request
|
||||||
|
info!("SERVER: {} {} - Headers: {:?}", method, uri, headers);
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
let response = next.run(req).await;
|
||||||
|
let duration = start.elapsed();
|
||||||
|
|
||||||
|
// Log response
|
||||||
|
info!("SERVER: {} {} - Status: {} - Duration: {:?}", method, uri, response.status(), duration);
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
fn check_auth(headers: &HeaderMap, password: &Option<String>) -> bool {
|
fn check_auth(headers: &HeaderMap, password: &Option<String>) -> bool {
|
||||||
if let Some(expected_password) = password {
|
if let Some(expected_password) = password {
|
||||||
if let Some(auth_header) = headers.get("authorization") {
|
if let Some(auth_header) = headers.get("authorization") {
|
||||||
|
|||||||
Reference in New Issue
Block a user