diff --git a/crates/cdk-mintd/Cargo.toml b/crates/cdk-mintd/Cargo.toml index f3369d6f..abe9c8d7 100644 --- a/crates/cdk-mintd/Cargo.toml +++ b/crates/cdk-mintd/Cargo.toml @@ -30,7 +30,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } futures = { version = "0.3.28", default-features = false } serde = { version = "1", default-features = false, features = ["derive"] } bip39 = "2.0" -tower-http = { version = "0.4.4", features = ["cors"] } +tower-http = { version = "0.4.4", features = ["cors", "compression-full"] } lightning-invoice = { version = "0.32.0", features = ["serde", "std"] } home = "0.5.5" url = "2.3" diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index ac61daa6..6385626a 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -9,7 +9,10 @@ use std::str::FromStr; use std::sync::Arc; use anyhow::{anyhow, bail, Result}; -use axum::Router; +use axum::http::Request; +use axum::middleware::Next; +use axum::response::Response; +use axum::{middleware, Router}; use bip39::Mnemonic; use cdk::cdk_database::{self, MintDatabase}; use cdk::cdk_lightning; @@ -26,6 +29,7 @@ use cdk_redb::MintRedbDatabase; use cdk_sqlite::MintSqliteDatabase; use clap::Parser; use tokio::sync::Notify; +use tower_http::compression::CompressionLayer; use tower_http::cors::CorsLayer; use tracing_subscriber::EnvFilter; #[cfg(feature = "swagger")] @@ -343,6 +347,8 @@ async fn main() -> anyhow::Result<()> { let mut mint_service = Router::new() .merge(v1_service) + .layer(CompressionLayer::new()) + .layer(middleware::from_fn(logging_middleware)) .layer(CorsLayer::permissive()); #[cfg(feature = "swagger")] @@ -391,6 +397,29 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +/// Logs infos about the request and the response +async fn logging_middleware(req: Request, next: Next) -> Response { + let start = std::time::Instant::now(); + let path = req.uri().path().to_owned(); + let method = req.method().clone(); + + let response = next.run(req).await; + + let duration = start.elapsed(); + let status = response.status(); + let compression = response + .headers() + .get("content-encoding") + .map(|h| h.to_str().unwrap_or("none")) + .unwrap_or("none"); + + tracing::trace!( + "Request: {method} {path} | Status: {status} | Compression: {compression} | Duration: {duration:?}", + ); + + response +} + fn work_dir() -> Result { let home_dir = home::home_dir().ok_or(anyhow!("Unknown home dir"))?; let dir = home_dir.join(".cdk-mintd"); diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 83ea0401..d3f7bc2f 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -39,6 +39,7 @@ reqwest = { version = "0.12", default-features = false, features = [ "rustls-tls", "rustls-tls-native-roots", "socks", + "zstd", "brotli", "gzip", "deflate" ], optional = true } serde = { version = "1", default-features = false, features = ["derive"] } serde_json = "1" diff --git a/flake.nix b/flake.nix index b4d69593..0b1e343d 100644 --- a/flake.nix +++ b/flake.nix @@ -239,11 +239,15 @@ cargo update -p serde_with --precise 3.1.0 cargo update -p regex --precise 1.9.6 cargo update -p backtrace --precise 0.3.58 + cargo update -p async-compression --precise 0.4.3 + cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5 + # For wasm32-unknown-unknown target cargo update -p bumpalo --precise 3.12.0 cargo update -p moka --precise 0.11.1 cargo update -p triomphe --precise 0.1.11 cargo update -p url --precise 2.5.2 + "; buildInputs = buildInputs ++ WASMInputs ++ [ msrv_toolchain ]; inherit nativeBuildInputs; @@ -263,10 +267,13 @@ cargo update -p home --precise 0.5.5 cargo update -p tokio --precise 1.38.1 cargo update -p tokio-stream --precise 0.1.15 + cargo update -p tokio-util --precise 0.7.11 cargo update -p serde_with --precise 3.1.0 cargo update -p reqwest --precise 0.12.4 cargo update -p url --precise 2.5.2 cargo update -p allocator-api2 --precise 0.2.18 + cargo update -p async-compression --precise 0.4.3 + cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5 ''; buildInputs = buildInputs ++ WASMInputs ++ [ db_msrv_toolchain ]; inherit nativeBuildInputs;