use bytes::Bytes;
use http_body_util::BodyExt;
use hyper::{Request, Response};
use tracing::info;
pub type BoxBody = http_body_util::combinators::BoxBody<Bytes, hyper::Error>;
pub trait RequestHandler: Send + Sync {
fn handle_request(&self, req: &mut Request<BoxBody>);
fn handle_response(&self, res: &mut Response<BoxBody>);
}
pub struct LoggingHandler;
impl RequestHandler for LoggingHandler {
fn handle_request(&self, req: &mut Request<BoxBody>) {
let path = req.uri().path();
let display_uri = if req.uri().query().is_some() {
format!("{path}?<redacted>")
} else {
path.to_string()
};
info!(">> {} {} {:?}", req.method(), display_uri, req.version());
}
fn handle_response(&self, res: &mut Response<BoxBody>) {
info!("<< {}", res.status());
}
}
pub fn boxed_body<B>(body: B) -> BoxBody
where
B: hyper::body::Body<Data = Bytes, Error = hyper::Error> + Send + Sync + 'static,
{
body.boxed()
}