1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41: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"
version = "0.1.0"
dependencies = [
"actix-web 3.3.3",
"actix-web 4.0.0-beta.21",
"env_logger 0.9.0",
"log",
]
[[package]]

View File

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

View File

@ -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

View File

@ -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<S> {
enabled: bool,
}
impl<S, B> Service for AddMsgService<S>
impl<S, B> Service<ServiceRequest> for AddMsgService<S>
where
S: Service<
Request = ServiceRequest,
Response = ServiceResponse<B>,
Error = actix_web::Error,
>,
S::Future: 'static,
B: 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
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)
}
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<S, B> Transform<S> for AddMsg
impl<S, B> Transform<S, ServiceRequest> for AddMsg
where
S: Service<
Request = ServiceRequest,
Response = ServiceResponse<B>,
Error = actix_web::Error,
>,
S::Future: 'static,
B: 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Ready<Result<Self::Transform, Self::InitError>>;