2019-03-09 23:06:24 +01:00
|
|
|
use std::cell::{Ref, RefCell};
|
2019-03-07 00:47:15 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
use actix_http::Extensions;
|
2019-03-07 00:47:15 +01:00
|
|
|
use actix_router::ResourceDef;
|
|
|
|
use actix_service::{boxed, IntoNewService, NewService};
|
|
|
|
|
|
|
|
use crate::guard::Guard;
|
2019-03-09 16:39:34 +01:00
|
|
|
use crate::rmap::ResourceMap;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
|
|
|
|
|
|
|
type Guards = Vec<Box<Guard>>;
|
|
|
|
type HttpNewService<P> =
|
|
|
|
boxed::BoxedNewService<(), ServiceRequest<P>, ServiceResponse, (), ()>;
|
|
|
|
|
|
|
|
/// Application configuration
|
2019-03-09 23:06:24 +01:00
|
|
|
pub struct ServiceConfig<P> {
|
|
|
|
config: AppConfig,
|
2019-03-07 00:47:15 +01:00
|
|
|
root: bool,
|
|
|
|
default: Rc<HttpNewService<P>>,
|
2019-03-09 16:39:34 +01:00
|
|
|
services: Vec<(
|
|
|
|
ResourceDef,
|
|
|
|
HttpNewService<P>,
|
|
|
|
Option<Guards>,
|
|
|
|
Option<Rc<ResourceMap>>,
|
|
|
|
)>,
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
impl<P: 'static> ServiceConfig<P> {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Crate server settings instance
|
2019-03-09 23:06:24 +01:00
|
|
|
pub(crate) fn new(config: AppConfig, default: Rc<HttpNewService<P>>) -> Self {
|
|
|
|
ServiceConfig {
|
|
|
|
config,
|
2019-03-07 00:47:15 +01:00
|
|
|
default,
|
|
|
|
root: true,
|
|
|
|
services: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if root is beeing configured
|
|
|
|
pub fn is_root(&self) -> bool {
|
|
|
|
self.root
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn into_services(
|
|
|
|
self,
|
2019-03-09 16:39:34 +01:00
|
|
|
) -> Vec<(
|
|
|
|
ResourceDef,
|
|
|
|
HttpNewService<P>,
|
|
|
|
Option<Guards>,
|
|
|
|
Option<Rc<ResourceMap>>,
|
|
|
|
)> {
|
2019-03-07 00:47:15 +01:00
|
|
|
self.services
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn clone_config(&self) -> Self {
|
2019-03-09 23:06:24 +01:00
|
|
|
ServiceConfig {
|
|
|
|
config: self.config.clone(),
|
2019-03-07 00:47:15 +01:00
|
|
|
default: self.default.clone(),
|
|
|
|
services: Vec::new(),
|
|
|
|
root: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
/// Service configuration
|
|
|
|
pub fn config(&self) -> &AppConfig {
|
|
|
|
&self.config
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
/// Default resource
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn default_service(&self) -> Rc<HttpNewService<P>> {
|
|
|
|
self.default.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_service<F, S>(
|
|
|
|
&mut self,
|
|
|
|
rdef: ResourceDef,
|
|
|
|
guards: Option<Vec<Box<Guard>>>,
|
|
|
|
service: F,
|
2019-03-09 16:39:34 +01:00
|
|
|
nested: Option<Rc<ResourceMap>>,
|
2019-03-07 00:47:15 +01:00
|
|
|
) where
|
2019-03-09 18:49:11 +01:00
|
|
|
F: IntoNewService<S>,
|
2019-03-07 00:47:15 +01:00
|
|
|
S: NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
2019-03-07 00:47:15 +01:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
|
|
|
self.services.push((
|
|
|
|
rdef,
|
|
|
|
boxed::new_service(service.into_new_service()),
|
|
|
|
guards,
|
2019-03-09 16:39:34 +01:00
|
|
|
nested,
|
2019-03-07 00:47:15 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 23:06:24 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppConfig(pub(crate) Rc<AppConfigInner>);
|
|
|
|
|
|
|
|
impl AppConfig {
|
|
|
|
pub(crate) fn new(inner: AppConfigInner) -> Self {
|
|
|
|
AppConfig(Rc::new(inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set server host name.
|
|
|
|
///
|
|
|
|
/// Host name is used by application router aa a hostname for url
|
|
|
|
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
|
|
|
/// html#method.host) documentation for more information.
|
|
|
|
///
|
|
|
|
/// By default host name is set to a "localhost" value.
|
|
|
|
pub fn host(&self) -> &str {
|
|
|
|
&self.0.host
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if connection is secure(https)
|
|
|
|
pub fn secure(&self) -> bool {
|
|
|
|
self.0.secure
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the socket address of the local half of this TCP connection
|
|
|
|
pub fn local_addr(&self) -> SocketAddr {
|
|
|
|
self.0.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resource map
|
|
|
|
pub fn rmap(&self) -> &ResourceMap {
|
|
|
|
&self.0.rmap
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Application extensions
|
|
|
|
pub fn extensions(&self) -> Ref<Extensions> {
|
|
|
|
self.0.extensions.borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct AppConfigInner {
|
|
|
|
pub(crate) secure: bool,
|
|
|
|
pub(crate) host: String,
|
|
|
|
pub(crate) addr: SocketAddr,
|
|
|
|
pub(crate) rmap: ResourceMap,
|
|
|
|
pub(crate) extensions: RefCell<Extensions>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AppConfigInner {
|
|
|
|
fn default() -> AppConfigInner {
|
|
|
|
AppConfigInner {
|
|
|
|
secure: false,
|
|
|
|
addr: "127.0.0.1:8080".parse().unwrap(),
|
|
|
|
host: "localhost:8080".to_owned(),
|
|
|
|
rmap: ResourceMap::new(ResourceDef::new("")),
|
|
|
|
extensions: RefCell::new(Extensions::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|