1use bytes::Bytes;
2use http_body_util::BodyExt;
3use hyper::{Request, Response};
4use tracing::info;
5
6pub type BoxBody = http_body_util::combinators::BoxBody<Bytes, hyper::Error>;
7
8pub trait RequestHandler: Send + Sync {
10 fn handle_request(&self, req: &mut Request<BoxBody>);
13
14 fn handle_response(&self, res: &mut Response<BoxBody>);
17}
18
19pub struct LoggingHandler;
21
22impl RequestHandler for LoggingHandler {
23 fn handle_request(&self, req: &mut Request<BoxBody>) {
24 let path = req.uri().path();
25 let display_uri = if req.uri().query().is_some() {
26 format!("{path}?<redacted>")
27 } else {
28 path.to_string()
29 };
30 info!(">> {} {} {:?}", req.method(), display_uri, req.version());
31 }
32
33 fn handle_response(&self, res: &mut Response<BoxBody>) {
34 info!("<< {}", res.status());
35 }
36}
37
38pub fn boxed_body<B>(body: B) -> BoxBody
40where
41 B: hyper::body::Body<Data = Bytes, Error = hyper::Error> + Send + Sync + 'static,
42{
43 body.boxed()
44}