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:
parent
cebd96588c
commit
0e03496d36
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -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]]
|
||||||
|
@ -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"
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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>>;
|
||||||
|
Loading…
Reference in New Issue
Block a user