2019-09-11 15:12:08 +02:00
|
|
|
use actix_service::{Service, Transform};
|
2019-09-18 06:16:47 +02:00
|
|
|
use actix_web::body::{BodySize, MessageBody, ResponseBody};
|
|
|
|
use std::marker::PhantomData;
|
2019-09-18 04:37:35 +02:00
|
|
|
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
2019-09-18 06:16:47 +02:00
|
|
|
use bytes::{Bytes, BytesMut};
|
|
|
|
use futures::Async;
|
2019-09-11 15:12:08 +02:00
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Future, Poll};
|
|
|
|
|
|
|
|
pub struct Logging;
|
|
|
|
|
|
|
|
impl<S: 'static, B> Transform<S> for Logging
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-09-18 04:37:35 +02:00
|
|
|
B: MessageBody + 'static,
|
2019-09-11 15:12:08 +02:00
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Response = ServiceResponse<BodyLogger<B>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
type Error = Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = LoggingMiddleware<S>;
|
|
|
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
ok(LoggingMiddleware {
|
2019-09-18 04:37:35 +02:00
|
|
|
service,
|
2019-09-11 15:12:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LoggingMiddleware<S> {
|
2019-09-18 04:37:35 +02:00
|
|
|
service: S,
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service for LoggingMiddleware<S>
|
|
|
|
where
|
2019-09-18 06:16:47 +02:00
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
B: MessageBody,
|
2019-09-11 15:12:08 +02:00
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Response = ServiceResponse<BodyLogger<B>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
type Error = Error;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Future = WrapperStream<S, B>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.poll_ready()
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:37:35 +02:00
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
2019-09-18 06:16:47 +02:00
|
|
|
WrapperStream {
|
|
|
|
fut: self.service.call(req),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WrapperStream<S, B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fut: S::Future,
|
|
|
|
_t: PhantomData<(B,)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Future for WrapperStream<S, B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
{
|
|
|
|
type Item = ServiceResponse<BodyLogger<B>>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let res = futures::try_ready!(self.fut.poll());
|
|
|
|
|
|
|
|
Ok(Async::Ready(res.map_body(move |_, body| {
|
|
|
|
ResponseBody::Body(BodyLogger {
|
|
|
|
body,
|
|
|
|
body_accum: BytesMut::new(),
|
|
|
|
})
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BodyLogger<B> {
|
|
|
|
body: ResponseBody<B>,
|
|
|
|
body_accum: BytesMut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> Drop for BodyLogger<B> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("response body: {:?}", self.body_accum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> MessageBody for BodyLogger<B> {
|
|
|
|
fn size(&self) -> BodySize {
|
|
|
|
self.body.size()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_next(&mut self) -> Poll<Option<Bytes>, Error> {
|
|
|
|
match self.body.poll_next()? {
|
|
|
|
Async::Ready(Some(chunk)) => {
|
|
|
|
self.body_accum.extend_from_slice(&chunk);
|
|
|
|
Ok(Async::Ready(Some(chunk)))
|
|
|
|
}
|
|
|
|
val => Ok(val),
|
|
|
|
}
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|