mirror of https://github.com/fafhrd91/actix-web
fix
This commit is contained in:
parent
c260fb1c48
commit
cf659dc5b9
|
@ -291,7 +291,7 @@ impl HttpServiceFactory for Files {
|
||||||
ResourceDef::prefix(&self.path)
|
ResourceDef::prefix(&self.path)
|
||||||
};
|
};
|
||||||
|
|
||||||
config.register_service(rdef, guards, self, None)
|
config.register_service(rdef, guards, self, None, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -562,6 +562,7 @@ impl HttpServiceFactory for NamedFile {
|
||||||
None,
|
None,
|
||||||
self,
|
self,
|
||||||
None,
|
None,
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,9 +88,9 @@ where
|
||||||
default,
|
default,
|
||||||
services: services
|
services: services
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(mut rdef, srv, guards, nested)| {
|
.map(|(mut rdef, srv, guards, nested, app_data)| {
|
||||||
rmap.add(&mut rdef, nested);
|
rmap.add(&mut rdef, nested);
|
||||||
(rdef, srv, RefCell::new(guards))
|
(rdef, srv, RefCell::new(guards), app_data)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.into_boxed_slice()
|
.into_boxed_slice()
|
||||||
|
@ -228,7 +228,14 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppRoutingFactory {
|
pub struct AppRoutingFactory {
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
services: Rc<
|
||||||
|
[(
|
||||||
|
ResourceDef,
|
||||||
|
HttpNewService,
|
||||||
|
RefCell<Option<Guards>>,
|
||||||
|
Option<Rc<Extensions>>,
|
||||||
|
)],
|
||||||
|
>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpNewService>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,15 +249,18 @@ impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
fn new_service(&self, _: ()) -> Self::Future {
|
||||||
// construct all services factory future with it's resource def and guards.
|
// construct all services factory future with it's resource def and guards.
|
||||||
let factory_fut = join_all(self.services.iter().map(|(path, factory, guards)| {
|
let factory_fut = join_all(self.services.iter().map(
|
||||||
let path = path.clone();
|
|(path, factory, guards, app_data)| {
|
||||||
let guards = guards.borrow_mut().take();
|
let path = path.clone();
|
||||||
let factory_fut = factory.new_service(());
|
let guards = guards.borrow_mut().take();
|
||||||
async move {
|
let factory_fut = factory.new_service(());
|
||||||
let service = factory_fut.await?;
|
let app_data = app_data.clone();
|
||||||
Ok((path, guards, service))
|
async move {
|
||||||
}
|
let service = factory_fut.await?;
|
||||||
}));
|
Ok((path, guards, service, app_data))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
// construct default service factory future
|
// construct default service factory future
|
||||||
let default_fut = self.default.new_service(());
|
let default_fut = self.default.new_service(());
|
||||||
|
@ -264,10 +274,13 @@ impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, _>>()?
|
.collect::<Result<Vec<_>, _>>()?
|
||||||
.drain(..)
|
.drain(..)
|
||||||
.fold(Router::build(), |mut router, (path, guards, service)| {
|
.fold(
|
||||||
router.rdef(path, service).2 = guards;
|
Router::build(),
|
||||||
router
|
|mut router, (path, guards, service, app_data)| {
|
||||||
})
|
router.rdef(path, (service, app_data)).2 = guards;
|
||||||
|
router
|
||||||
|
},
|
||||||
|
)
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
Ok(AppRouting { router, default })
|
Ok(AppRouting { router, default })
|
||||||
|
@ -276,7 +289,7 @@ impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppRouting {
|
pub struct AppRouting {
|
||||||
router: Router<HttpService, Guards>,
|
router: Router<(HttpService, Option<Rc<Extensions>>), Guards>,
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,7 +312,10 @@ impl Service<ServiceRequest> for AppRouting {
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some((srv, _info)) = res {
|
if let Some(((srv, app_data), _info)) = res {
|
||||||
|
if let Some(ref app_data) = app_data {
|
||||||
|
req.add_data_container(app_data.clone());
|
||||||
|
}
|
||||||
srv.call(req)
|
srv.call(req)
|
||||||
} else {
|
} else {
|
||||||
self.default.call(req)
|
self.default.call(req)
|
||||||
|
|
|
@ -29,6 +29,7 @@ pub struct AppService {
|
||||||
HttpNewService,
|
HttpNewService,
|
||||||
Option<Guards>,
|
Option<Guards>,
|
||||||
Option<Rc<ResourceMap>>,
|
Option<Rc<ResourceMap>>,
|
||||||
|
Option<Rc<Extensions>>,
|
||||||
)>,
|
)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ impl AppService {
|
||||||
HttpNewService,
|
HttpNewService,
|
||||||
Option<Guards>,
|
Option<Guards>,
|
||||||
Option<Rc<ResourceMap>>,
|
Option<Rc<ResourceMap>>,
|
||||||
|
Option<Rc<Extensions>>,
|
||||||
)>,
|
)>,
|
||||||
) {
|
) {
|
||||||
(self.config, self.services)
|
(self.config, self.services)
|
||||||
|
@ -88,6 +90,7 @@ impl AppService {
|
||||||
guards: Option<Vec<Box<dyn Guard>>>,
|
guards: Option<Vec<Box<dyn Guard>>>,
|
||||||
factory: F,
|
factory: F,
|
||||||
nested: Option<Rc<ResourceMap>>,
|
nested: Option<Rc<ResourceMap>>,
|
||||||
|
app_data: Option<Rc<Extensions>>,
|
||||||
) where
|
) where
|
||||||
F: IntoServiceFactory<S, ServiceRequest>,
|
F: IntoServiceFactory<S, ServiceRequest>,
|
||||||
S: ServiceFactory<
|
S: ServiceFactory<
|
||||||
|
@ -98,8 +101,13 @@ impl AppService {
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + 'static,
|
> + 'static,
|
||||||
{
|
{
|
||||||
self.services
|
self.services.push((
|
||||||
.push((rdef, boxed::factory(factory.into_factory()), guards, nested));
|
rdef,
|
||||||
|
boxed::factory(factory.into_factory()),
|
||||||
|
guards,
|
||||||
|
nested,
|
||||||
|
app_data,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -395,7 +395,9 @@ where
|
||||||
*rdef.name_mut() = name.clone();
|
*rdef.name_mut() = name.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
config.register_service(rdef, guards, self, None)
|
let app_data = self.app_data.take().map(Rc::new);
|
||||||
|
|
||||||
|
config.register_service(rdef, guards, self, None, app_data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -412,7 +414,6 @@ where
|
||||||
fn into_factory(self) -> T {
|
fn into_factory(self) -> T {
|
||||||
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
||||||
routes: self.routes,
|
routes: self.routes,
|
||||||
app_data: self.app_data.map(Rc::new),
|
|
||||||
default: self.default,
|
default: self.default,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -422,7 +423,6 @@ where
|
||||||
|
|
||||||
pub struct ResourceFactory {
|
pub struct ResourceFactory {
|
||||||
routes: Vec<Route>,
|
routes: Vec<Route>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpNewService,
|
default: HttpNewService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,8 +441,6 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||||
// construct route service factory futures
|
// construct route service factory futures
|
||||||
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
||||||
|
|
||||||
let app_data = self.app_data.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
let routes = factory_fut
|
let routes = factory_fut
|
||||||
|
@ -450,18 +448,13 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
Ok(ResourceService {
|
Ok(ResourceService { routes, default })
|
||||||
routes,
|
|
||||||
app_data,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResourceService {
|
pub struct ResourceService {
|
||||||
routes: Vec<RouteService>,
|
routes: Vec<RouteService>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -475,18 +468,10 @@ impl Service<ServiceRequest> for ResourceService {
|
||||||
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
for route in self.routes.iter() {
|
for route in self.routes.iter() {
|
||||||
if route.check(&mut req) {
|
if route.check(&mut req) {
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
return route.call(req);
|
return route.call(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.default.call(req)
|
self.default.call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
68
src/scope.rs
68
src/scope.rs
|
@ -424,15 +424,14 @@ where
|
||||||
|
|
||||||
// complete scope pipeline creation
|
// complete scope pipeline creation
|
||||||
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
||||||
app_data: self.app_data.take().map(Rc::new),
|
|
||||||
default,
|
default,
|
||||||
services: cfg
|
services: cfg
|
||||||
.into_services()
|
.into_services()
|
||||||
.1
|
.1
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(mut rdef, srv, guards, nested)| {
|
.map(|(mut rdef, srv, guards, nested, app_data)| {
|
||||||
rmap.add(&mut rdef, nested);
|
rmap.add(&mut rdef, nested);
|
||||||
(rdef, srv, RefCell::new(guards))
|
(rdef, srv, RefCell::new(guards), app_data)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.into_boxed_slice()
|
.into_boxed_slice()
|
||||||
|
@ -452,13 +451,20 @@ where
|
||||||
guards,
|
guards,
|
||||||
self.endpoint,
|
self.endpoint,
|
||||||
Some(Rc::new(rmap)),
|
Some(Rc::new(rmap)),
|
||||||
|
self.app_data.take().map(Rc::new),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeFactory {
|
pub struct ScopeFactory {
|
||||||
app_data: Option<Rc<Extensions>>,
|
services: Rc<
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
[(
|
||||||
|
ResourceDef,
|
||||||
|
HttpNewService,
|
||||||
|
RefCell<Option<Guards>>,
|
||||||
|
Option<Rc<Extensions>>,
|
||||||
|
)],
|
||||||
|
>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpNewService>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -475,17 +481,18 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||||
let default_fut = self.default.new_service(());
|
let default_fut = self.default.new_service(());
|
||||||
|
|
||||||
// construct all services factory future with it's resource def and guards.
|
// construct all services factory future with it's resource def and guards.
|
||||||
let factory_fut = join_all(self.services.iter().map(|(path, factory, guards)| {
|
let factory_fut = join_all(self.services.iter().map(
|
||||||
let path = path.clone();
|
|(path, factory, guards, app_data)| {
|
||||||
let guards = guards.borrow_mut().take();
|
let path = path.clone();
|
||||||
let factory_fut = factory.new_service(());
|
let guards = guards.borrow_mut().take();
|
||||||
async move {
|
let factory_fut = factory.new_service(());
|
||||||
let service = factory_fut.await?;
|
let app_data = app_data.clone();
|
||||||
Ok((path, guards, service))
|
async move {
|
||||||
}
|
let service = factory_fut.await?;
|
||||||
}));
|
Ok((path, guards, service, app_data))
|
||||||
|
}
|
||||||
let app_data = self.app_data.clone();
|
},
|
||||||
|
));
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
|
@ -496,24 +503,22 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, _>>()?
|
.collect::<Result<Vec<_>, _>>()?
|
||||||
.drain(..)
|
.drain(..)
|
||||||
.fold(Router::build(), |mut router, (path, guards, service)| {
|
.fold(
|
||||||
router.rdef(path, service).2 = guards;
|
Router::build(),
|
||||||
router
|
|mut router, (path, guards, service, app_data)| {
|
||||||
})
|
router.rdef(path, (service, app_data)).2 = guards;
|
||||||
|
router
|
||||||
|
},
|
||||||
|
)
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
Ok(ScopeService {
|
Ok(ScopeService { router, default })
|
||||||
app_data,
|
|
||||||
router,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeService {
|
pub struct ScopeService {
|
||||||
app_data: Option<Rc<Extensions>>,
|
router: Router<(HttpService, Option<Rc<Extensions>>), Vec<Box<dyn Guard>>>,
|
||||||
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -536,11 +541,10 @@ impl Service<ServiceRequest> for ScopeService {
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
if let Some(((srv, app_data), _info)) = res {
|
||||||
req.add_data_container(app_data.clone());
|
if let Some(ref app_data) = app_data {
|
||||||
}
|
req.add_data_container(app_data.clone());
|
||||||
|
}
|
||||||
if let Some((srv, _info)) = res {
|
|
||||||
srv.call(req)
|
srv.call(req)
|
||||||
} else {
|
} else {
|
||||||
self.default.call(req)
|
self.default.call(req)
|
||||||
|
|
|
@ -562,7 +562,7 @@ where
|
||||||
if let Some(ref name) = self.name {
|
if let Some(ref name) = self.name {
|
||||||
*rdef.name_mut() = name.clone();
|
*rdef.name_mut() = name.clone();
|
||||||
}
|
}
|
||||||
config.register_service(rdef, guards, self.srv, None)
|
config.register_service(rdef, guards, self.srv, None, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue