mirror of https://github.com/fafhrd91/actix-web
Scope Config: Use ServiceConfig instead
This commit is contained in:
parent
8c2688c1f5
commit
53d254464a
|
@ -15,7 +15,6 @@ use crate::service::{
|
||||||
HttpServiceFactory, ServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
HttpServiceFactory, ServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
};
|
};
|
||||||
use core::borrow::{Borrow, BorrowMut};
|
|
||||||
|
|
||||||
type Guards = Vec<Box<Guard>>;
|
type Guards = Vec<Box<Guard>>;
|
||||||
type HttpNewService =
|
type HttpNewService =
|
||||||
|
@ -240,58 +239,6 @@ impl ServiceConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeConfig {
|
|
||||||
pub(crate) services: Vec<Box<ServiceFactory>>,
|
|
||||||
pub(crate) guards: Vec<Box<Guard>>,
|
|
||||||
pub(crate) data: Option<Extensions>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ScopeConfig {
|
|
||||||
pub(crate) fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
services: Vec::new(),
|
|
||||||
guards: Vec::new(),
|
|
||||||
data: Some(Extensions::new()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set application data. Application data could be accessed
|
|
||||||
/// by using `Data<T>` extractor where `T` is data type.
|
|
||||||
///
|
|
||||||
/// This is same as `App::data()` method.
|
|
||||||
pub fn data<S: 'static>(&mut self, data: S) -> &mut Self {
|
|
||||||
if self.data.is_none() {
|
|
||||||
self.data = Some(Extensions::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.data.as_mut().unwrap().insert(data);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Configure route for a specific path.
|
|
||||||
///
|
|
||||||
/// This is same as `App::route()` method.
|
|
||||||
pub fn route(&mut self, path: &str, mut route: Route) -> &mut Self {
|
|
||||||
self.service(
|
|
||||||
Resource::new(path)
|
|
||||||
.add_guards(route.take_guards())
|
|
||||||
.route(route),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register http service.
|
|
||||||
///
|
|
||||||
/// This is same as `App::service()` method.
|
|
||||||
pub fn service<F>(&mut self, factory: F) -> &mut Self
|
|
||||||
where
|
|
||||||
F: HttpServiceFactory + 'static,
|
|
||||||
{
|
|
||||||
self.services
|
|
||||||
.push(Box::new(ServiceFactoryWrapper::new(factory)));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
|
19
src/scope.rs
19
src/scope.rs
|
@ -21,7 +21,7 @@ use crate::route::Route;
|
||||||
use crate::service::{
|
use crate::service::{
|
||||||
ServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
ServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
||||||
};
|
};
|
||||||
use crate::config::ScopeConfig;
|
use crate::config::ServiceConfig;
|
||||||
|
|
||||||
type Guards = Vec<Box<Guard>>;
|
type Guards = Vec<Box<Guard>>;
|
||||||
type HttpService = BoxedService<ServiceRequest, ServiceResponse, Error>;
|
type HttpService = BoxedService<ServiceRequest, ServiceResponse, Error>;
|
||||||
|
@ -117,25 +117,22 @@ impl Scope {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn configure<F>(mut self, f: F) -> Self
|
pub fn configure<F>(mut self, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut ScopeConfig),
|
F: FnOnce(&mut ServiceConfig),
|
||||||
{
|
{
|
||||||
let mut cfg = ScopeConfig::new();
|
let mut cfg = ServiceConfig::new();
|
||||||
f(&mut cfg);
|
f(&mut cfg);
|
||||||
self.services.extend(cfg.services);
|
self.services.extend(cfg.services);
|
||||||
self.guards.extend(cfg.guards);
|
|
||||||
//self.data.extend(cfg.data);
|
|
||||||
|
|
||||||
if cfg.data.is_some() {
|
if !cfg.data.is_empty() {
|
||||||
if (&self).data.is_some() {
|
if (&self).data.is_some() {
|
||||||
|
|
||||||
let mut new_data = Extensions::from(cfg.data.unwrap());
|
let mut data = self.data.unwrap();
|
||||||
|
|
||||||
let old_data = Extensions::from(self.data.take().unwrap());
|
for value in cfg.data.iter() {
|
||||||
for value in old_data.into_iter() {
|
value.create(&mut data);
|
||||||
new_data.insert(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.data = Some(new_data);
|
self.data = Some(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
|
|
|
@ -14,7 +14,6 @@ use crate::route::Route;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use crate::service::WebService;
|
use crate::service::WebService;
|
||||||
|
|
||||||
pub use crate::config::ScopeConfig;
|
|
||||||
pub use crate::config::ServiceConfig;
|
pub use crate::config::ServiceConfig;
|
||||||
pub use crate::data::Data;
|
pub use crate::data::Data;
|
||||||
pub use crate::request::HttpRequest;
|
pub use crate::request::HttpRequest;
|
||||||
|
|
Loading…
Reference in New Issue