mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
v3 examples (#364)
This commit is contained in:
@ -5,10 +5,9 @@ authors = ["Gorm Casper <gcasper@gmail.com>", "Sven-Hendrik Haase <svenstaro@gma
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "1.0.0"
|
||||
actix-rt = "1.0.0"
|
||||
actix-web = "2.0.0"
|
||||
bytes = "0.5"
|
||||
actix-service = "1"
|
||||
actix-web = "3"
|
||||
|
||||
env_logger = "0.7"
|
||||
futures = "0.3.1"
|
||||
pin-project = "0.4.6"
|
||||
pin-project = "0.4"
|
||||
|
@ -1,6 +1,6 @@
|
||||
# middleware examples
|
||||
|
||||
This example showcases a bunch of different uses of middlewares. See also the [Middleware guide](https://actix.rs/docs/middleware/)..
|
||||
This example showcases a bunch of different uses of middlewares. See also the [Middleware guide](https://actix.rs/docs/middleware/).
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -13,7 +13,7 @@ mod redirect;
|
||||
#[allow(dead_code)]
|
||||
mod simple;
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=debug");
|
||||
env_logger::init();
|
||||
|
@ -4,8 +4,8 @@ use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_web::web::BytesMut;
|
||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage};
|
||||
use bytes::BytesMut;
|
||||
use futures::future::{ok, Future, Ready};
|
||||
use futures::stream::StreamExt;
|
||||
|
||||
|
@ -5,8 +5,8 @@ use std::task::{Context, Poll};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_web::body::{BodySize, MessageBody, ResponseBody};
|
||||
use actix_web::web::{Bytes, BytesMut};
|
||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::future::{ok, Ready};
|
||||
|
||||
pub struct Logging;
|
||||
@ -86,13 +86,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project(PinnedDrop)]
|
||||
pub struct BodyLogger<B> {
|
||||
#[pin]
|
||||
body: ResponseBody<B>,
|
||||
body_accum: BytesMut,
|
||||
}
|
||||
|
||||
impl<B> Drop for BodyLogger<B> {
|
||||
fn drop(&mut self) {
|
||||
#[pin_project::pinned_drop]
|
||||
impl<B> PinnedDrop for BodyLogger<B> {
|
||||
fn drop(self: Pin<&mut Self>) {
|
||||
println!("response body: {:?}", self.body_accum);
|
||||
}
|
||||
}
|
||||
@ -102,10 +105,15 @@ impl<B: MessageBody> MessageBody for BodyLogger<B> {
|
||||
self.body.size()
|
||||
}
|
||||
|
||||
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
|
||||
match self.body.poll_next(cx) {
|
||||
fn poll_next(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||
let this = self.project();
|
||||
|
||||
match this.body.poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(chunk))) => {
|
||||
self.body_accum.extend_from_slice(&chunk);
|
||||
this.body_accum.extend_from_slice(&chunk);
|
||||
Poll::Ready(Some(Ok(chunk)))
|
||||
}
|
||||
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
|
||||
|
Reference in New Issue
Block a user