From 94c4053cb5ef5d1f20c7a5ed08934d2cf31d4de7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 10 Feb 2018 11:01:54 -0800 Subject: [PATCH] more HttpServer type simplification --- guide/src/qs_3.md | 2 +- guide/src/qs_3_5.md | 8 +--- src/server/srv.rs | 103 ++++++++++++++------------------------------ src/test.rs | 13 +++--- 4 files changed, 41 insertions(+), 85 deletions(-) diff --git a/guide/src/qs_3.md b/guide/src/qs_3.md index 7a90c3b5d..6d9c1a426 100644 --- a/guide/src/qs_3.md +++ b/guide/src/qs_3.md @@ -46,7 +46,7 @@ Multiple applications could be served with one server: use actix_web::*; fn main() { - HttpServer::::new(|| vec![ + HttpServer::new(|| vec![ Application::new() .prefix("/app1") .resource("/", |r| r.f(|r| httpcodes::HTTPOk)), diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md index 7982cd272..62f21b61b 100644 --- a/guide/src/qs_3_5.md +++ b/guide/src/qs_3_5.md @@ -80,12 +80,10 @@ could be overridden with `HttpServer::threads()` method. ```rust # extern crate actix_web; # extern crate tokio_core; -# use tokio_core::net::TcpStream; -# use std::net::SocketAddr; use actix_web::*; fn main() { - HttpServer::::new( + HttpServer::new( || Application::new() .resource("/", |r| r.h(httpcodes::HTTPOk))) .threads(4); // <- Start 4 workers @@ -143,12 +141,10 @@ connection behavior is defined by server settings. ```rust # extern crate actix_web; # extern crate tokio_core; -# use tokio_core::net::TcpStream; -# use std::net::SocketAddr; use actix_web::*; fn main() { - HttpServer::::new(|| + HttpServer::new(|| Application::new() .resource("/", |r| r.h(httpcodes::HTTPOk))) .keep_alive(None); // <- Use `SO_KEEPALIVE` socket option. diff --git a/src/server/srv.rs b/src/server/srv.rs index 3d4a2fd02..d0f73cd90 100644 --- a/src/server/srv.rs +++ b/src/server/srv.rs @@ -2,7 +2,6 @@ use std::{io, net, thread}; use std::rc::Rc; use std::sync::{Arc, mpsc as sync_mpsc}; use std::time::Duration; -use std::marker::PhantomData; use std::collections::HashMap; use actix::prelude::*; @@ -21,7 +20,7 @@ use native_tls::TlsAcceptor; use openssl::ssl::{AlpnError, SslAcceptorBuilder}; use helpers; -use super::{HttpHandler, IntoHttpHandler, IoStream}; +use super::{IntoHttpHandler, IoStream}; use super::{PauseServer, ResumeServer, StopServer}; use super::channel::{HttpChannel, WrapperStream}; use super::worker::{Conn, Worker, StreamHandlerType, StopWorker}; @@ -35,17 +34,15 @@ use super::settings::{ServerSettings, WorkerSettings}; /// `A` - peer address /// /// `H` - request handler -pub struct HttpServer - where H: HttpHandler + 'static +pub struct HttpServer where H: IntoHttpHandler + 'static { - h: Option>>, - addr: PhantomData, + h: Option>>, threads: usize, backlog: i32, host: Option, keep_alive: Option, - factory: Arc U + Send + Sync>, - workers: Vec>>, + factory: Arc Vec + Send + Sync>, + workers: Vec>>, sockets: HashMap, accept: Vec<(mio::SetReadiness, sync_mpsc::Sender)>, exit: bool, @@ -54,15 +51,11 @@ pub struct HttpServer no_signals: bool, } -unsafe impl Sync for HttpServer where H: HttpHandler + 'static {} -unsafe impl Send for HttpServer where H: HttpHandler + 'static {} +unsafe impl Sync for HttpServer where H: IntoHttpHandler {} +unsafe impl Send for HttpServer where H: IntoHttpHandler {} -impl Actor for HttpServer - where A: 'static, - H: HttpHandler, - U: IntoIterator + 'static, - V: IntoHttpHandler, +impl Actor for HttpServer where H: IntoHttpHandler { type Context = Context; @@ -71,23 +64,23 @@ impl Actor for HttpServer } } -impl HttpServer - where A: 'static, - H: HttpHandler, - U: IntoIterator + 'static, - V: IntoHttpHandler, +impl HttpServer where H: IntoHttpHandler + 'static { /// Create new http server with application factory - pub fn new(factory: F) -> Self - where F: Sync + Send + 'static + Fn() -> U, + pub fn new(factory: F) -> Self + where F: Fn() -> U + Sync + Send + 'static, + U: IntoIterator + 'static, { + let f = move || { + (factory)().into_iter().collect() + }; + HttpServer{ h: None, - addr: PhantomData, threads: num_cpus::get(), backlog: 2048, host: None, keep_alive: None, - factory: Arc::new(factory), + factory: Arc::new(f), workers: Vec::new(), sockets: HashMap::new(), accept: Vec::new(), @@ -253,9 +246,7 @@ impl HttpServer } } -impl HttpServer - where U: IntoIterator + 'static, - V: IntoHttpHandler, +impl HttpServer { /// Start listening for incoming connections. /// @@ -345,9 +336,7 @@ impl HttpServer } #[cfg(feature="tls")] -impl HttpServer - where U: IntoIterator + 'static, - V: IntoHttpHandler, +impl HttpServer { /// Start listening for incoming tls connections. pub fn start_tls(mut self, pkcs12: ::Pkcs12) -> io::Result> { @@ -385,9 +374,7 @@ impl HttpServer } #[cfg(feature="alpn")] -impl HttpServer - where U: IntoIterator + 'static, - V: IntoHttpHandler, +impl HttpServer { /// Start listening for incoming tls connections. /// @@ -430,18 +417,15 @@ impl HttpServer } } -impl HttpServer - where A: 'static, - H: HttpHandler, - U: IntoIterator + 'static, - V: IntoHttpHandler, +impl HttpServer { /// Start listening for incoming connections from a stream. /// /// This method uses only one thread for handling incoming connections. - pub fn start_incoming(mut self, stream: S, secure: bool) -> SyncAddress + pub fn start_incoming(mut self, stream: S, secure: bool) -> SyncAddress where S: Stream + 'static, T: AsyncRead + AsyncWrite + 'static, + A: 'static { if !self.sockets.is_empty() { let addrs: Vec<(net::SocketAddr, net::TcpListener)> = @@ -461,8 +445,7 @@ impl HttpServer let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); let settings = ServerSettings::new(Some(addr), &self.host, secure); let apps: Vec<_> = (*self.factory)() - .into_iter() - .map(|h| h.into_handler(settings.clone())).collect(); + .into_iter().map(|h| h.into_handler(settings.clone())).collect(); self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive))); // start server @@ -483,11 +466,7 @@ impl HttpServer /// Signals support /// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)` /// message to `System` actor. -impl Handler for HttpServer - where H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, +impl Handler for HttpServer { type Result = (); @@ -513,12 +492,9 @@ impl Handler for HttpServer } } -impl Handler>> for HttpServer +impl Handler>> for HttpServer where T: IoStream, - H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, + H: IntoHttpHandler, { type Result = (); @@ -534,12 +510,9 @@ impl Handler>> for HttpServer } } -impl Handler> for HttpServer +impl Handler> for HttpServer where T: IoStream, - H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, + H: IntoHttpHandler, { type Result = (); @@ -550,11 +523,7 @@ impl Handler> for HttpServer } } -impl Handler for HttpServer - where H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, +impl Handler for HttpServer { type Result = (); @@ -567,11 +536,7 @@ impl Handler for HttpServer } } -impl Handler for HttpServer - where H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, +impl Handler for HttpServer { type Result = (); @@ -583,11 +548,7 @@ impl Handler for HttpServer } } -impl Handler for HttpServer - where H: HttpHandler + 'static, - U: IntoIterator + 'static, - V: IntoHttpHandler, - A: 'static, +impl Handler for HttpServer { type Result = actix::Response; diff --git a/src/test.rs b/src/test.rs index 1440ba9df..bc8da075a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -24,7 +24,7 @@ use router::Router; use payload::Payload; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use server::{HttpServer, HttpHandler, IntoHttpHandler, ServerSettings}; +use server::{HttpServer, IntoHttpHandler, ServerSettings}; use ws::{WsClient, WsClientError, WsClientReader, WsClientWriter}; /// The `TestServer` type. @@ -72,11 +72,10 @@ impl TestServer { } /// Start new test server with application factory - pub fn with_factory(factory: F) -> Self - where H: HttpHandler, - F: Sync + Send + 'static + Fn() -> U, - U: IntoIterator + 'static, - V: IntoHttpHandler, + pub fn with_factory(factory: F) -> Self + where F: Fn() -> U + Sync + Send + 'static, + U: IntoIterator + 'static, + H: IntoHttpHandler + 'static, { let (tx, rx) = mpsc::channel(); @@ -123,7 +122,7 @@ impl TestServer { HttpServer::new(move || { let mut app = TestApp::new(state()); config(&mut app); - app} + vec![app]} ).disable_signals().start_incoming(tcp.incoming(), false); tx.send((Arbiter::system(), local_addr)).unwrap();