From 0e03496d36044dda9a1aae3a71e8abd180198a48 Mon Sep 17 00:00:00 2001 From: Luca Palmieri Date: Sat, 29 Jan 2022 14:42:30 +0000 Subject: [PATCH] Update basics/middleware-ext-mut to v4 (#484) --- Cargo.lock | 3 +- basics/middleware-ext-mut/Cargo.toml | 4 +- basics/middleware-ext-mut/README.md | 2 +- basics/middleware-ext-mut/src/add_msg.rs | 50 +++++++----------------- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 572f13a..e8498b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3553,8 +3553,9 @@ dependencies = [ name = "middleware-ext-mut" version = "0.1.0" dependencies = [ - "actix-web 3.3.3", + "actix-web 4.0.0-beta.21", "env_logger 0.9.0", + "log", ] [[package]] diff --git a/basics/middleware-ext-mut/Cargo.toml b/basics/middleware-ext-mut/Cargo.toml index 9cfd3a8..fdd58a8 100644 --- a/basics/middleware-ext-mut/Cargo.toml +++ b/basics/middleware-ext-mut/Cargo.toml @@ -5,6 +5,6 @@ authors = ["Eric McCarthy "] edition = "2018" [dependencies] -actix-web = "3" - +actix-web = "4.0.0-beta.21" +log = "0.4" env_logger = "0.9" diff --git a/basics/middleware-ext-mut/README.md b/basics/middleware-ext-mut/README.md index f16a1f2..66bdbcf 100644 --- a/basics/middleware-ext-mut/README.md +++ b/basics/middleware-ext-mut/README.md @@ -1,6 +1,6 @@ # middleware examples -This example showcases a middleware that adds and retreives request-local data. See also the [Middleware guide](https://actix.rs/docs/middleware/). +This example showcases a middleware that adds and retrieves request-local data. See also the [Middleware guide](https://actix.rs/docs/middleware/). ## Usage diff --git a/basics/middleware-ext-mut/src/add_msg.rs b/basics/middleware-ext-mut/src/add_msg.rs index cc0a03d..e8e36e3 100644 --- a/basics/middleware-ext-mut/src/add_msg.rs +++ b/basics/middleware-ext-mut/src/add_msg.rs @@ -1,11 +1,13 @@ use std::{ - future::{ready, Future, Ready}, - pin::Pin, + future::{ready, Ready}, task::{Context, Poll}, }; -use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use actix_web::Error; +use actix_web::{ + dev::{Service, ServiceRequest, ServiceResponse, Transform}, + HttpMessage, +}; #[derive(Debug, Clone)] pub struct Msg(pub String); @@ -16,43 +18,28 @@ pub struct AddMsgService { enabled: bool, } -impl Service for AddMsgService +impl Service for AddMsgService where - S: Service< - Request = ServiceRequest, - Response = ServiceResponse, - Error = actix_web::Error, - >, - S::Future: 'static, - B: 'static, + S: Service, Error = actix_web::Error>, { - type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; - type Future = Pin>>>; + type Future = S::Future; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx) } - fn call(&mut self, req: Self::Request) -> Self::Future { - println!("request is passing through the AddMsg middleware"); - - // get mut HttpRequest from ServiceRequest - let (request, pl) = req.into_parts(); + fn call(&self, req: ServiceRequest) -> Self::Future { + log::info!("request is passing through the AddMsg middleware"); if self.enabled { // insert data into extensions if enabled - request - .extensions_mut() + req.extensions_mut() .insert(Msg("Hello from Middleware!".to_owned())); } - // construct a new service response - match ServiceRequest::from_parts(request, pl) { - Ok(req) => Box::pin(self.service.call(req)), - Err(_) => Box::pin(ready(Err(Error::from(())))), - } + self.service.call(req) } } @@ -71,17 +58,10 @@ impl AddMsg { } } -impl Transform for AddMsg +impl Transform for AddMsg where - S: Service< - Request = ServiceRequest, - Response = ServiceResponse, - Error = actix_web::Error, - >, - S::Future: 'static, - B: 'static, + S: Service, Error = actix_web::Error>, { - type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; type Future = Ready>;