1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 15:46:48 +00:00

more HttpServer type simplification

This commit is contained in:
Nikolay Kim 2018-02-10 11:01:54 -08:00
parent 762961b0f4
commit 94c4053cb5
4 changed files with 41 additions and 85 deletions

View file

@ -46,7 +46,7 @@ Multiple applications could be served with one server:
use actix_web::*;
fn main() {
HttpServer::<TcpStream, SocketAddr, _, _>::new(|| vec![
HttpServer::new(|| vec![
Application::new()
.prefix("/app1")
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),

View file

@ -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::<TcpStream, SocketAddr, _, _>::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::<TcpStream, SocketAddr, _, _>::new(||
HttpServer::new(||
Application::new()
.resource("/", |r| r.h(httpcodes::HTTPOk)))
.keep_alive(None); // <- Use `SO_KEEPALIVE` socket option.

View file

@ -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<A, H, U>
where H: HttpHandler + 'static
pub struct HttpServer<H> where H: IntoHttpHandler + 'static
{
h: Option<Rc<WorkerSettings<H>>>,
addr: PhantomData<A>,
h: Option<Rc<WorkerSettings<H::Handler>>>,
threads: usize,
backlog: i32,
host: Option<String>,
keep_alive: Option<u64>,
factory: Arc<Fn() -> U + Send + Sync>,
workers: Vec<SyncAddress<Worker<H>>>,
factory: Arc<Fn() -> Vec<H> + Send + Sync>,
workers: Vec<SyncAddress<Worker<H::Handler>>>,
sockets: HashMap<net::SocketAddr, net::TcpListener>,
accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>,
exit: bool,
@ -54,15 +51,11 @@ pub struct HttpServer<A, H, U>
no_signals: bool,
}
unsafe impl<A, H, U> Sync for HttpServer<A, H, U> where H: HttpHandler + 'static {}
unsafe impl<A, H, U> Send for HttpServer<A, H, U> where H: HttpHandler + 'static {}
unsafe impl<H> Sync for HttpServer<H> where H: IntoHttpHandler {}
unsafe impl<H> Send for HttpServer<H> where H: IntoHttpHandler {}
impl<A, H, U, V> Actor for HttpServer<A, H, U>
where A: 'static,
H: HttpHandler,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H> Actor for HttpServer<H> where H: IntoHttpHandler
{
type Context = Context<Self>;
@ -71,23 +64,23 @@ impl<A, H, U, V> Actor for HttpServer<A, H, U>
}
}
impl<A, H, U, V> HttpServer<A, H, U>
where A: 'static,
H: HttpHandler,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
{
/// Create new http server with application factory
pub fn new<F>(factory: F) -> Self
where F: Sync + Send + 'static + Fn() -> U,
pub fn new<F, U>(factory: F) -> Self
where F: Fn() -> U + Sync + Send + 'static,
U: IntoIterator<Item=H> + '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<A, H, U, V> HttpServer<A, H, U>
}
}
impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
where U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H: IntoHttpHandler> HttpServer<H>
{
/// Start listening for incoming connections.
///
@ -345,9 +336,7 @@ impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
}
#[cfg(feature="tls")]
impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
where U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H: IntoHttpHandler> HttpServer<H>
{
/// Start listening for incoming tls connections.
pub fn start_tls(mut self, pkcs12: ::Pkcs12) -> io::Result<SyncAddress<Self>> {
@ -385,9 +374,7 @@ impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
}
#[cfg(feature="alpn")]
impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
where U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H: IntoHttpHandler> HttpServer<H>
{
/// Start listening for incoming tls connections.
///
@ -430,18 +417,15 @@ impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
}
}
impl<A, H, U, V> HttpServer<A, H, U>
where A: 'static,
H: HttpHandler,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
impl<H: IntoHttpHandler> HttpServer<H>
{
/// Start listening for incoming connections from a stream.
///
/// This method uses only one thread for handling incoming connections.
pub fn start_incoming<T, S>(mut self, stream: S, secure: bool) -> SyncAddress<Self>
pub fn start_incoming<T, A, S>(mut self, stream: S, secure: bool) -> SyncAddress<Self>
where S: Stream<Item=(T, A), Error=io::Error> + 'static,
T: AsyncRead + AsyncWrite + 'static,
A: 'static
{
if !self.sockets.is_empty() {
let addrs: Vec<(net::SocketAddr, net::TcpListener)> =
@ -461,8 +445,7 @@ impl<A, H, U, V> HttpServer<A, H, U>
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<A, H, U, V> HttpServer<A, H, U>
/// Signals support
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
/// message to `System` actor.
impl<A, H, U, V> Handler<signal::Signal> for HttpServer<A, H, U>
where H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
impl<H: IntoHttpHandler> Handler<signal::Signal> for HttpServer<H>
{
type Result = ();
@ -513,12 +492,9 @@ impl<A, H, U, V> Handler<signal::Signal> for HttpServer<A, H, U>
}
}
impl<T, A, H, U, V> Handler<io::Result<Conn<T>>> for HttpServer<A, H, U>
impl<T, H> Handler<io::Result<Conn<T>>> for HttpServer<H>
where T: IoStream,
H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
H: IntoHttpHandler,
{
type Result = ();
@ -534,12 +510,9 @@ impl<T, A, H, U, V> Handler<io::Result<Conn<T>>> for HttpServer<A, H, U>
}
}
impl<T, A, H, U, V> Handler<Conn<T>> for HttpServer<A, H, U>
impl<T, H> Handler<Conn<T>> for HttpServer<H>
where T: IoStream,
H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
H: IntoHttpHandler,
{
type Result = ();
@ -550,11 +523,7 @@ impl<T, A, H, U, V> Handler<Conn<T>> for HttpServer<A, H, U>
}
}
impl<A, H, U, V> Handler<PauseServer> for HttpServer<A, H, U>
where H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
impl<H: IntoHttpHandler> Handler<PauseServer> for HttpServer<H>
{
type Result = ();
@ -567,11 +536,7 @@ impl<A, H, U, V> Handler<PauseServer> for HttpServer<A, H, U>
}
}
impl<A, H, U, V> Handler<ResumeServer> for HttpServer<A, H, U>
where H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
impl<H: IntoHttpHandler> Handler<ResumeServer> for HttpServer<H>
{
type Result = ();
@ -583,11 +548,7 @@ impl<A, H, U, V> Handler<ResumeServer> for HttpServer<A, H, U>
}
}
impl<A, H, U, V> Handler<StopServer> for HttpServer<A, H, U>
where H: HttpHandler + 'static,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
A: 'static,
impl<H: IntoHttpHandler> Handler<StopServer> for HttpServer<H>
{
type Result = actix::Response<Self, StopServer>;

View file

@ -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<H, F, U, V>(factory: F) -> Self
where H: HttpHandler,
F: Sync + Send + 'static + Fn() -> U,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
pub fn with_factory<F, U, H>(factory: F) -> Self
where F: Fn() -> U + Sync + Send + 'static,
U: IntoIterator<Item=H> + '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();