2019-12-07 18:59:24 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-09-11 15:12:08 +02:00
|
|
|
use actix_service::{Service, Transform};
|
2020-09-12 17:49:45 +02:00
|
|
|
use actix_web::web::BytesMut;
|
2019-09-11 15:12:08 +02:00
|
|
|
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage};
|
2019-12-07 18:59:24 +01:00
|
|
|
use futures::future::{ok, Future, Ready};
|
|
|
|
use futures::stream::StreamExt;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
pub struct Logging;
|
|
|
|
|
|
|
|
impl<S: 'static, B> Transform<S> for Logging
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
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 {
|
|
|
|
ok(LoggingMiddleware {
|
|
|
|
service: Rc::new(RefCell::new(service)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LoggingMiddleware<S> {
|
|
|
|
// This is special: We need this to avoid lifetime issues.
|
|
|
|
service: Rc<RefCell<S>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service for LoggingMiddleware<S>
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>
|
|
|
|
+ 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
2019-12-07 18:59:24 +01:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
2019-12-07 18:59:24 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(cx)
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
|
|
|
let mut svc = self.service.clone();
|
|
|
|
|
2019-12-07 18:59:24 +01:00
|
|
|
Box::pin(async move {
|
|
|
|
let mut body = BytesMut::new();
|
|
|
|
let mut stream = req.take_payload();
|
|
|
|
while let Some(chunk) = stream.next().await {
|
|
|
|
body.extend_from_slice(&chunk?);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("request body: {:?}", body);
|
|
|
|
let res = svc.call(req).await?;
|
|
|
|
|
|
|
|
println!("response: {:?}", res.headers());
|
|
|
|
Ok(res)
|
|
|
|
})
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|