1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-30 19:32:53 +01:00

remove server config

This commit is contained in:
Nikolay Kim 2019-03-08 22:38:39 -08:00
parent 70ead175b9
commit a4d4770462
8 changed files with 16 additions and 97 deletions

View File

@ -20,7 +20,6 @@ members = [
"actix-rt", "actix-rt",
"actix-service", "actix-service",
"actix-server", "actix-server",
"actix-server-config",
"actix-test-server", "actix-test-server",
"actix-utils", "actix-utils",
"router", "router",

View File

@ -1,18 +0,0 @@
[package]
name = "actix-server-config"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server config utils"
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
license = "MIT/Apache-2.0"
edition = "2018"
workspace = ".."
[lib]
name = "actix_server_config"
path = "src/lib.rs"
[dependencies]
actix-service = { path="../actix-service" }
futures = "0.1.25"

View File

@ -1,33 +0,0 @@
use std::cell::Cell;
use std::net::SocketAddr;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct ServerConfig {
addr: SocketAddr,
secure: Rc<Cell<bool>>,
}
impl ServerConfig {
pub fn new(addr: SocketAddr) -> Self {
ServerConfig {
addr,
secure: Rc::new(Cell::new(false)),
}
}
/// Returns the address of the local half of this TCP server socket
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
/// Returns true if connection is secure (tls enabled)
pub fn secure(&self) -> bool {
self.secure.as_ref().get()
}
/// Set secure flag
pub fn set_secure(&self) {
self.secure.as_ref().set(true)
}
}

View File

@ -36,7 +36,6 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
actix-rt = "0.2.0" actix-rt = "0.2.0"
#actix-service = "0.3.2" #actix-service = "0.3.2"
actix-service = { path="../actix-service" } actix-service = { path="../actix-service" }
actix-server-config = { path="../actix-server-config" }
log = "0.4" log = "0.4"
num_cpus = "1.0" num_cpus = "1.0"

View File

@ -150,15 +150,14 @@ impl ServerBuilder {
U: net::ToSocketAddrs, U: net::ToSocketAddrs,
{ {
let sockets = bind_addr(addr)?; let sockets = bind_addr(addr)?;
let token = self.token.next();
self.services.push(StreamNewService::create(
name.as_ref().to_string(),
token,
factory.clone(),
));
for lst in sockets { for lst in sockets {
let token = self.token.next();
self.services.push(StreamNewService::create(
name.as_ref().to_string(),
token,
factory.clone(),
lst.local_addr()?,
));
self.sockets.push((token, lst)); self.sockets.push((token, lst));
} }
Ok(self) Ok(self)
@ -179,7 +178,6 @@ impl ServerBuilder {
name.as_ref().to_string(), name.as_ref().to_string(),
token, token,
factory, factory,
lst.local_addr()?,
)); ));
self.sockets.push((token, lst)); self.sockets.push((token, lst));
Ok(self) Ok(self)

View File

@ -10,8 +10,6 @@ mod signals;
pub mod ssl; pub mod ssl;
mod worker; mod worker;
pub use actix_server_config::ServerConfig;
pub use self::builder::ServerBuilder; pub use self::builder::ServerBuilder;
pub use self::server::Server; pub use self::server::Server;
pub use self::service_config::{ServiceConfig, ServiceRuntime}; pub use self::service_config::{ServiceConfig, ServiceRuntime};

View File

@ -1,8 +1,7 @@
use std::net::{SocketAddr, TcpStream}; use std::net::TcpStream;
use std::time::Duration; use std::time::Duration;
use actix_rt::spawn; use actix_rt::spawn;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service}; use actix_service::{NewService, Service};
use futures::future::{err, ok, FutureResult}; use futures::future::{err, ok, FutureResult};
use futures::{Future, Poll}; use futures::{Future, Poll};
@ -24,7 +23,7 @@ pub(crate) enum ServerMessage {
} }
pub trait ServiceFactory: Send + Clone + 'static { pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<TokioTcpStream, ServerConfig>; type NewService: NewService<TokioTcpStream>;
fn create(&self) -> Self::NewService; fn create(&self) -> Self::NewService;
} }
@ -97,25 +96,14 @@ pub(crate) struct StreamNewService<F: ServiceFactory> {
name: String, name: String,
inner: F, inner: F,
token: Token, token: Token,
addr: SocketAddr,
} }
impl<F> StreamNewService<F> impl<F> StreamNewService<F>
where where
F: ServiceFactory, F: ServiceFactory,
{ {
pub(crate) fn create( pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
name: String, Box::new(Self { name, token, inner })
token: Token,
inner: F,
addr: SocketAddr,
) -> Box<InternalServiceFactory> {
Box::new(Self {
name,
token,
inner,
addr,
})
} }
} }
@ -132,17 +120,15 @@ where
name: self.name.clone(), name: self.name.clone(),
inner: self.inner.clone(), inner: self.inner.clone(),
token: self.token, token: self.token,
addr: self.addr,
}) })
} }
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> { fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
let token = self.token; let token = self.token;
let config = ServerConfig::new(self.addr);
Box::new( Box::new(
self.inner self.inner
.create() .create()
.new_service(&config) .new_service(&())
.map_err(|_| ()) .map_err(|_| ())
.map(move |inner| { .map(move |inner| {
let service: BoxedServerService = Box::new(StreamService::new(inner)); let service: BoxedServerService = Box::new(StreamService::new(inner));
@ -169,7 +155,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F impl<F, T> ServiceFactory for F
where where
F: Fn() -> T + Send + Clone + 'static, F: Fn() -> T + Send + Clone + 'static,
T: NewService<TokioTcpStream, ServerConfig>, T: NewService<TokioTcpStream>,
{ {
type NewService = T; type NewService = T;

View File

@ -1,7 +1,7 @@
use std::{net, thread, time}; use std::{net, thread, time};
use actix_server::{Server, ServerConfig}; use actix_server::Server;
use actix_service::{fn_cfg_factory, IntoService}; use actix_service::fn_service;
use net2::TcpBuilder; use net2::TcpBuilder;
fn unused_addr() -> net::SocketAddr { fn unused_addr() -> net::SocketAddr {
@ -19,12 +19,7 @@ fn test_bind() {
thread::spawn(move || { thread::spawn(move || {
Server::build() Server::build()
.bind("test", addr, move || { .bind("test", addr, || fn_service(|_| Ok::<_, ()>(())))
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap() .unwrap()
.run() .run()
}); });
@ -40,12 +35,7 @@ fn test_listen() {
thread::spawn(move || { thread::spawn(move || {
let lst = net::TcpListener::bind(addr).unwrap(); let lst = net::TcpListener::bind(addr).unwrap();
Server::build() Server::build()
.listen("test", lst, move || { .listen("test", lst, move || fn_service(|_| Ok::<_, ()>(())))
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap() .unwrap()
.run() .run()
}); });