a0fea6edb4
* Migrate nym-api HTTP server from rocket to axum (#4698) Migrate endpoints to Axum * Squashed after PR review Initial WIP - bootstrap axum server with same data as rocket - start axum server alongside rocket - add routes for circulating-supply, contract-cache, network - write simple bash validation that migrated APIs return 200 - mark rocket parts of code as deprecated - start more complicated routes: WIP Init storage always Add coconut routes Add api-status routes Expand tests WIP Migrate unstable APIs with query params Update bash tests Add node-status routes Redirect / to /swagger Update API tests Implement graceful shutdown rustfmt Fix clippy * Add ecash routes after rebase * PR feedback - add CORS layer - move logger to common crate - remove global log filters for nym-api and axum * Serve OpenAPI for all endpoints (#4761) * Playing around with swagger * Generate OpenAPI for /status routes * Phase out static_routes as strings - also nest routers in a clearer way * Generate OpenAPI for /network routes * Generate OpenAPI for /api-status routes * Generate OpenAPI for "nym nodes" routes * Fix some network-monitor routes * Generate OpenAPI for /ecash routes * Add utoipa feature to /common mods * Add OpenAPI for unstable routes * Fix MixNodeDetails field in models * Introduce axum feature flag (#4775) * Add Axum bind_address to config * Introduce axum feature flag * Add comment to template.rs * Add Github action to build wtih `axum` feature * Refactor server start & shutdown (#4777) * Clippy: don't forget axum feature * Refactor router so it's safer * Implement graceful shutdown * Nicer pattern matching * Better Result syntax
64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
use axum::extract::{ConnectInfo, Request};
|
|
use axum::http::header::{HOST, USER_AGENT};
|
|
use axum::http::HeaderValue;
|
|
use axum::middleware::Next;
|
|
use axum::response::IntoResponse;
|
|
use colored::Colorize;
|
|
use std::net::SocketAddr;
|
|
use std::time::Instant;
|
|
use tracing::info;
|
|
|
|
pub async fn logger(
|
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
|
request: Request,
|
|
next: Next,
|
|
) -> impl IntoResponse {
|
|
// TODO dz use `OriginalUri` extractor to get full URI even for nested
|
|
// routers if routes aren't logged correctly in handlers
|
|
fn header_map(header: Option<&HeaderValue>, msg: String) -> String {
|
|
header
|
|
.map(|x| x.to_str().unwrap_or(&msg).to_string())
|
|
.unwrap_or(msg)
|
|
}
|
|
|
|
let method = request.method().to_string().green();
|
|
let uri = request.uri().to_string().blue();
|
|
let agent = header_map(
|
|
request.headers().get(USER_AGENT),
|
|
"Unknown User Agent".to_string(),
|
|
);
|
|
|
|
let host = header_map(request.headers().get(HOST), "Unknown Host".to_string());
|
|
|
|
let start = Instant::now();
|
|
// run request through all middleware, incl. extractors
|
|
let res = next.run(request).await;
|
|
let time_taken = start.elapsed();
|
|
let status = res.status();
|
|
let print_status = if status.is_client_error() || status.is_server_error() {
|
|
status.to_string().red()
|
|
} else if status.is_success() {
|
|
status.to_string().green()
|
|
} else {
|
|
status.to_string().yellow()
|
|
};
|
|
|
|
let taken = "time taken".bold();
|
|
|
|
let time_taken = match time_taken.as_millis() {
|
|
ms if ms > 500 => format!("{taken}: {}", format!("{ms}ms").red()),
|
|
ms if ms > 200 => format!("{taken}: {}", format!("{ms}ms").yellow()),
|
|
ms if ms > 50 => format!("{taken}: {}", format!("{ms}ms").bright_yellow()),
|
|
ms => format!("{taken}: {ms}ms"),
|
|
};
|
|
|
|
let agent_str = "agent".bold();
|
|
|
|
info!("[{addr} -> {host}] {method} '{uri}': {print_status} {time_taken} {agent_str}: {agent}");
|
|
|
|
res
|
|
}
|