1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

Update basics/middleware-ext-mut to v4 (#484)

This commit is contained in:
Luca Palmieri 2022-01-29 14:42:30 +00:00 committed by GitHub
parent cebd96588c
commit 0e03496d36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 39 deletions

3
Cargo.lock generated
View File

@ -3553,8 +3553,9 @@ dependencies = [
name = "middleware-ext-mut" name = "middleware-ext-mut"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web 3.3.3", "actix-web 4.0.0-beta.21",
"env_logger 0.9.0", "env_logger 0.9.0",
"log",
] ]
[[package]] [[package]]

View File

@ -5,6 +5,6 @@ authors = ["Eric McCarthy <ericmccarthy7@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
actix-web = "3" actix-web = "4.0.0-beta.21"
log = "0.4"
env_logger = "0.9" env_logger = "0.9"

View File

@ -1,6 +1,6 @@
# middleware examples # 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 ## Usage

View File

@ -1,11 +1,13 @@
use std::{ use std::{
future::{ready, Future, Ready}, future::{ready, Ready},
pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
}; };
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::Error; use actix_web::Error;
use actix_web::{
dev::{Service, ServiceRequest, ServiceResponse, Transform},
HttpMessage,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Msg(pub String); pub struct Msg(pub String);
@ -16,43 +18,28 @@ pub struct AddMsgService<S> {
enabled: bool, enabled: bool,
} }
impl<S, B> Service for AddMsgService<S> impl<S, B> Service<ServiceRequest> for AddMsgService<S>
where where
S: Service< S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
Request = ServiceRequest,
Response = ServiceResponse<B>,
Error = actix_web::Error,
>,
S::Future: 'static,
B: 'static,
{ {
type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = Error; type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Error>>>>; type Future = S::Future;
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx) self.service.poll_ready(ctx)
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&self, req: ServiceRequest) -> Self::Future {
println!("request is passing through the AddMsg middleware"); log::info!("request is passing through the AddMsg middleware");
// get mut HttpRequest from ServiceRequest
let (request, pl) = req.into_parts();
if self.enabled { if self.enabled {
// insert data into extensions if enabled // insert data into extensions if enabled
request req.extensions_mut()
.extensions_mut()
.insert(Msg("Hello from Middleware!".to_owned())); .insert(Msg("Hello from Middleware!".to_owned()));
} }
// construct a new service response self.service.call(req)
match ServiceRequest::from_parts(request, pl) {
Ok(req) => Box::pin(self.service.call(req)),
Err(_) => Box::pin(ready(Err(Error::from(())))),
}
} }
} }
@ -71,17 +58,10 @@ impl AddMsg {
} }
} }
impl<S, B> Transform<S> for AddMsg impl<S, B> Transform<S, ServiceRequest> for AddMsg
where where
S: Service< S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
Request = ServiceRequest,
Response = ServiceResponse<B>,
Error = actix_web::Error,
>,
S::Future: 'static,
B: 'static,
{ {
type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = Error; type Error = Error;
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = Ready<Result<Self::Transform, Self::InitError>>;