mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-23 00:00:15 +02:00
Add compression example (#2623)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
use axum::{routing::post, Json, Router};
|
||||
use serde_json::Value;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::{compression::CompressionLayer, decompression::RequestDecompressionLayer};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "example-compression=trace".into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let app: Router = app();
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
.await
|
||||
.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
fn app() -> Router {
|
||||
Router::new().route("/", post(root)).layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(RequestDecompressionLayer::new())
|
||||
.layer(CompressionLayer::new()),
|
||||
)
|
||||
}
|
||||
|
||||
async fn root(Json(value): Json<Value>) -> Json<Value> {
|
||||
Json(value)
|
||||
}
|
||||
Reference in New Issue
Block a user