Reputation: 933
I would like to read the content of rpc body. I know I need to create my own middleware. I follow the tower example from https://github.com/hyperium/tonic/blob/master/examples/src/tower/server.rs
I do
#[derive(Debug, Clone)]
struct MyMiddleware<S> {
inner: S,
}
type BoxFuture<'a, T> = Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for MyMiddleware<S>
where
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
S::Future: Send + 'static,
ReqBody: Send + 'static + BodyExt,
{
type Response = S::Response;
type Error = S::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
// See: https://docs.rs/tower/latest/tower/trait.Service.html#be-careful-when-cloning-inner-services
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
let (parts, body) = req.into_parts();
let content = body.collect().await.unwrap().to_bytes();
println!("body : {:?}", content);
let response = inner.call(req).await?;
Ok(response)
})
}
}
But get no method named collect fouind for type parameter ReqBody in the current scope
I miss something ?
Update: I add BodyExt to ReqBody, but now, I get <ReqBody as HttpBody>::Error
doesn't implement std::fmt::Debug
.
I need to add Debug trait to ReqBody too ?
Upvotes: 0
Views: 47