2022-01-29 17:39:58 +01:00
|
|
|
use std::{
|
|
|
|
future::{ready, Ready},
|
|
|
|
rc::Rc,
|
|
|
|
};
|
2019-12-07 18:59:24 +01:00
|
|
|
|
2022-12-17 23:04:59 +01:00
|
|
|
use actix_http::h1;
|
2022-02-22 13:12:17 +01:00
|
|
|
use actix_web::{
|
|
|
|
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
|
2022-12-17 23:04:59 +01:00
|
|
|
web, Error,
|
2022-02-22 13:12:17 +01:00
|
|
|
};
|
2022-12-17 23:04:59 +01:00
|
|
|
use futures_util::future::LocalBoxFuture;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
pub struct Logging;
|
|
|
|
|
2022-01-29 17:39:58 +01:00
|
|
|
impl<S: 'static, B> Transform<S, ServiceRequest> for Logging
|
2019-09-11 15:12:08 +02:00
|
|
|
where
|
2022-01-29 17:39:58 +01:00
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-09-11 15:12:08 +02:00
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = LoggingMiddleware<S>;
|
2019-12-07 18:59:24 +01:00
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2022-01-29 17:39:58 +01:00
|
|
|
ready(Ok(LoggingMiddleware {
|
|
|
|
service: Rc::new(service),
|
|
|
|
}))
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LoggingMiddleware<S> {
|
|
|
|
// This is special: We need this to avoid lifetime issues.
|
2022-01-29 17:39:58 +01:00
|
|
|
service: Rc<S>,
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:39:58 +01:00
|
|
|
impl<S, B> Service<ServiceRequest> for LoggingMiddleware<S>
|
2019-09-11 15:12:08 +02:00
|
|
|
where
|
2022-01-29 17:39:58 +01:00
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
2019-09-11 15:12:08 +02:00
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
2022-01-29 17:39:58 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
2022-01-29 17:39:58 +01:00
|
|
|
dev::forward_ready!(service);
|
2019-09-11 15:12:08 +02:00
|
|
|
|
2022-01-29 17:39:58 +01:00
|
|
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
|
|
|
let svc = self.service.clone();
|
2019-09-11 15:12:08 +02:00
|
|
|
|
2019-12-07 18:59:24 +01:00
|
|
|
Box::pin(async move {
|
2022-12-17 23:04:59 +01:00
|
|
|
// extract bytes from request body
|
|
|
|
let body = req.extract::<web::Bytes>().await.unwrap();
|
|
|
|
println!("request body (middleware): {body:?}");
|
|
|
|
|
|
|
|
// re-insert body back into request to be used by handlers
|
|
|
|
req.set_payload(bytes_to_payload(body));
|
2019-12-07 18:59:24 +01:00
|
|
|
|
|
|
|
let res = svc.call(req).await?;
|
|
|
|
|
|
|
|
println!("response: {:?}", res.headers());
|
|
|
|
Ok(res)
|
|
|
|
})
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 23:04:59 +01:00
|
|
|
|
|
|
|
fn bytes_to_payload(buf: web::Bytes) -> dev::Payload {
|
|
|
|
let (_, mut pl) = h1::Payload::create(true);
|
|
|
|
pl.unread_data(buf);
|
|
|
|
dev::Payload::from(pl)
|
|
|
|
}
|