Add print-request-response example (#216)

* Add `print-request-response` example

Someone asked about this on Discord. I think its worth adding as an
example.

* add missing feature
This commit is contained in:
David Pedersen 2021-08-20 09:26:31 +02:00 committed by GitHub
parent 1bda638c6b
commit 39a0c26795
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 0 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "example-print-request-response"
version = "0.1.0"
edition = "2018"
publish = false
[dependencies]
axum = { path = "../.." }
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = "0.2"
tower = { version = "0.4", features = ["util", "filter"] }
hyper = { version = "0.14", features = ["full"] }

View file

@ -0,0 +1,64 @@
//! Run with
//!
//! ```not_rust
//! cargo run -p example-print-request-response
//! ```
use axum::{
body::{Body, BoxBody, Bytes},
handler::post,
http::{Request, Response},
Router,
};
use std::net::SocketAddr;
use tower::{filter::AsyncFilterLayer, util::AndThenLayer, BoxError};
#[tokio::main]
async fn main() {
// Set the RUST_LOG, if it hasn't been explicitly defined
if std::env::var("RUST_LOG").is_err() {
std::env::set_var(
"RUST_LOG",
"example_print_request_response=debug,tower_http=debug",
)
}
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/", post(|| async move { "Hello from `POST /`" }))
.layer(AsyncFilterLayer::new(map_request))
.layer(AndThenLayer::new(map_response));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn map_request(req: Request<Body>) -> Result<Request<Body>, BoxError> {
let (parts, body) = req.into_parts();
let bytes = buffer_and_print("request", body).await?;
let req = Request::from_parts(parts, Body::from(bytes));
Ok(req)
}
async fn map_response(res: Response<BoxBody>) -> Result<Response<Body>, BoxError> {
let (parts, body) = res.into_parts();
let bytes = buffer_and_print("response", body).await?;
let res = Response::from_parts(parts, Body::from(bytes));
Ok(res)
}
async fn buffer_and_print<B>(direction: &str, body: B) -> Result<Bytes, BoxError>
where
B: axum::body::HttpBody<Data = Bytes>,
B::Error: Into<BoxError>,
{
let bytes = hyper::body::to_bytes(body).await.map_err(Into::into)?;
if let Ok(body) = std::str::from_utf8(&bytes) {
tracing::debug!("{} body = {:?}", direction, body);
}
Ok(bytes)
}