Skip to content

Commit

Permalink
mintd: add support for HTTP compression
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 authored and thesimplekid committed Dec 14, 2024
1 parent 9f54655 commit 824bce5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +27,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")]
Expand Down Expand Up @@ -296,6 +300,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")]
Expand Down Expand Up @@ -344,6 +350,29 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

/// Logs infos about the request and the response
async fn logging_middleware<B>(req: Request<B>, next: Next<B>) -> 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<PathBuf> {
let home_dir = home::home_dir().ok_or(anyhow!("Unknown home dir"))?;

Expand Down
1 change: 1 addition & 0 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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"
Expand Down
7 changes: 7 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,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;
Expand All @@ -164,10 +168,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;
Expand Down

0 comments on commit 824bce5

Please sign in to comment.