mirror of https://github.com/fafhrd91/actix-web
add `Route::wrap`
This commit is contained in:
parent
de9e41484a
commit
783b3f7a8f
|
@ -1,15 +1,17 @@
|
|||
use std::{mem, rc::Rc};
|
||||
|
||||
use actix_http::Method;
|
||||
use actix_http::{body::MessageBody, Method};
|
||||
use actix_service::{
|
||||
apply,
|
||||
boxed::{self, BoxService},
|
||||
fn_service, Service, ServiceFactory, ServiceFactoryExt,
|
||||
fn_service, Service, ServiceFactory, ServiceFactoryExt, Transform,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::{
|
||||
guard::{self, Guard},
|
||||
handler::{handler_service, Handler},
|
||||
middleware::Compat,
|
||||
service::{BoxedHttpServiceFactory, ServiceRequest, ServiceResponse},
|
||||
Error, FromRequest, HttpResponse, Responder,
|
||||
};
|
||||
|
@ -35,6 +37,25 @@ impl Route {
|
|||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "middleware")]
|
||||
#[doc(alias = "use")] // nodejs terminology
|
||||
pub fn wrap<M, B>(self, mw: M) -> Route
|
||||
where
|
||||
M: Transform<
|
||||
BoxService<ServiceRequest, ServiceResponse, Error>,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
> + 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
Route {
|
||||
service: boxed::factory(apply(Compat::new(mw), self.service)),
|
||||
guards: self.guards,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn take_guards(&mut self) -> Vec<Box<dyn Guard>> {
|
||||
mem::take(Rc::get_mut(&mut self.guards).unwrap())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue