mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Working middleware using extensions_mut (#402)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
97
basics/middleware-ext-mut/src/add_msg.rs
Normal file
97
basics/middleware-ext-mut/src/add_msg.rs
Normal file
@ -0,0 +1,97 @@
|
||||
use std::{
|
||||
future::{ready, Future, Ready},
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||
use actix_web::Error;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Msg(pub String);
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AddMsgService<S> {
|
||||
service: S,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl<S, B> Service for AddMsgService<S>
|
||||
where
|
||||
S: Service<
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = actix_web::Error,
|
||||
>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Error>>>>;
|
||||
|
||||
fn poll_ready(&mut 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();
|
||||
|
||||
if self.enabled {
|
||||
// insert data into extensions if enabled
|
||||
request
|
||||
.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(())))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AddMsg {
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl AddMsg {
|
||||
pub fn enabled() -> Self {
|
||||
Self { enabled: true }
|
||||
}
|
||||
|
||||
pub fn disabled() -> Self {
|
||||
Self { enabled: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for AddMsg
|
||||
where
|
||||
S: Service<
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = actix_web::Error,
|
||||
>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
type Transform = AddMsgService<S>;
|
||||
type InitError = ();
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(AddMsgService {
|
||||
service,
|
||||
enabled: self.enabled,
|
||||
}))
|
||||
}
|
||||
}
|
36
basics/middleware-ext-mut/src/main.rs
Normal file
36
basics/middleware-ext-mut/src/main.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use std::{env, io};
|
||||
|
||||
use actix_web::{
|
||||
middleware,
|
||||
web::{self, ReqData},
|
||||
App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
mod add_msg;
|
||||
use crate::add_msg::{AddMsg, Msg};
|
||||
|
||||
// wrap route in our middleware factory
|
||||
async fn index(msg: Option<ReqData<Msg>>) -> HttpResponse {
|
||||
if let Some(msg_data) = msg {
|
||||
let Msg(message) = msg_data.into_inner();
|
||||
HttpResponse::Ok().body(message)
|
||||
} else {
|
||||
HttpResponse::InternalServerError().body("No message found.")
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env::set_var("RUST_LOG", "info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/on").wrap(AddMsg::enabled()).to(index))
|
||||
.service(web::resource("/off").wrap(AddMsg::disabled()).to(index))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user