1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +01:00

use ServerConfig for system configuration

This commit is contained in:
Nikolay Kim 2019-03-12 14:14:21 -07:00
parent f6f292a678
commit 39356690b0
2 changed files with 16 additions and 14 deletions

View File

@ -150,7 +150,7 @@ impl ServerBuilder {
let mut srv = ConfiguredService::new(apply); let mut srv = ConfiguredService::new(apply);
for (name, lst) in cfg.services { for (name, lst) in cfg.services {
let token = self.token.next(); let token = self.token.next();
srv.stream(token, name); srv.stream(token, name, lst.local_addr()?);
self.sockets.push((token, lst)); self.sockets.push((token, lst));
} }
self.services.push(Box::new(srv)); self.services.push(Box::new(srv));

View File

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::{fmt, io, net}; use std::{fmt, io, net};
use actix_server_config::Io; use actix_server_config::{Io, ServerConfig};
use actix_service::{IntoNewService, NewService}; use actix_service::{IntoNewService, NewService};
use futures::future::{join_all, Future}; use futures::future::{join_all, Future};
use log::error; use log::error;
@ -76,7 +76,7 @@ impl ServiceConfig {
pub(super) struct ConfiguredService { pub(super) struct ConfiguredService {
rt: Box<ServiceRuntimeConfiguration>, rt: Box<ServiceRuntimeConfiguration>,
names: HashMap<Token, String>, names: HashMap<Token, (String, net::SocketAddr)>,
services: HashMap<String, Token>, services: HashMap<String, Token>,
} }
@ -89,15 +89,15 @@ impl ConfiguredService {
} }
} }
pub(super) fn stream(&mut self, token: Token, name: String) { pub(super) fn stream(&mut self, token: Token, name: String, addr: net::SocketAddr) {
self.names.insert(token, name.clone()); self.names.insert(token, (name.clone(), addr));
self.services.insert(name, token); self.services.insert(name, token);
} }
} }
impl InternalServiceFactory for ConfiguredService { impl InternalServiceFactory for ConfiguredService {
fn name(&self, token: Token) -> &str { fn name(&self, token: Token) -> &str {
&self.names[&token] &self.names[&token].0
} }
fn clone_factory(&self) -> Box<InternalServiceFactory> { fn clone_factory(&self) -> Box<InternalServiceFactory> {
@ -117,7 +117,8 @@ impl InternalServiceFactory for ConfiguredService {
// construct services // construct services
let mut fut = Vec::new(); let mut fut = Vec::new();
for (token, ns) in rt.services { for (token, ns) in rt.services {
fut.push(ns.new_service(&()).map(move |service| (token, service))); let config = ServerConfig::new(self.names[&token].1);
fut.push(ns.new_service(&config).map(move |service| (token, service)));
} }
Box::new(join_all(fut).map_err(|e| { Box::new(join_all(fut).map_err(|e| {
@ -172,8 +173,8 @@ impl ServiceRuntime {
pub fn service<T, F>(&mut self, name: &str, service: F) pub fn service<T, F>(&mut self, name: &str, service: F)
where where
F: IntoNewService<T>, F: IntoNewService<T, ServerConfig>,
T: NewService<Request = Io<TcpStream>> + 'static, T: NewService<ServerConfig, Request = Io<TcpStream>> + 'static,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::InitError: fmt::Debug, T::InitError: fmt::Debug,
@ -183,7 +184,7 @@ impl ServiceRuntime {
self.services.insert( self.services.insert(
token.clone(), token.clone(),
Box::new(ServiceFactory { Box::new(ServiceFactory {
inner: service.into_new_service().map(|_| ()), inner: service.into_new_service(),
}), }),
); );
} else { } else {
@ -194,6 +195,7 @@ impl ServiceRuntime {
type BoxedNewService = Box< type BoxedNewService = Box<
NewService< NewService<
ServerConfig,
Request = (Option<CounterGuard>, ServerMessage), Request = (Option<CounterGuard>, ServerMessage),
Response = (), Response = (),
Error = (), Error = (),
@ -207,9 +209,9 @@ struct ServiceFactory<T> {
inner: T, inner: T,
} }
impl<T> NewService for ServiceFactory<T> impl<T> NewService<ServerConfig> for ServiceFactory<T>
where where
T: NewService<Request = Io<TcpStream>, Response = ()>, T: NewService<ServerConfig, Request = Io<TcpStream>>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::Error: 'static, T::Error: 'static,
@ -222,8 +224,8 @@ where
type Service = BoxedServerService; type Service = BoxedServerService;
type Future = Box<Future<Item = BoxedServerService, Error = ()>>; type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
fn new_service(&self, _: &()) -> Self::Future { fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
Box::new(self.inner.new_service(&()).map_err(|_| ()).map(|s| { Box::new(self.inner.new_service(cfg).map_err(|_| ()).map(|s| {
let service: BoxedServerService = Box::new(StreamService::new(s)); let service: BoxedServerService = Box::new(StreamService::new(s));
service service
})) }))