mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
more HttpServer type simplification
This commit is contained in:
parent
762961b0f4
commit
94c4053cb5
@ -46,7 +46,7 @@ Multiple applications could be served with one server:
|
|||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
HttpServer::<TcpStream, SocketAddr, _, _>::new(|| vec![
|
HttpServer::new(|| vec![
|
||||||
Application::new()
|
Application::new()
|
||||||
.prefix("/app1")
|
.prefix("/app1")
|
||||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),
|
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),
|
||||||
|
@ -80,12 +80,10 @@ could be overridden with `HttpServer::threads()` method.
|
|||||||
```rust
|
```rust
|
||||||
# extern crate actix_web;
|
# extern crate actix_web;
|
||||||
# extern crate tokio_core;
|
# extern crate tokio_core;
|
||||||
# use tokio_core::net::TcpStream;
|
|
||||||
# use std::net::SocketAddr;
|
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
HttpServer::<TcpStream, SocketAddr, _, _>::new(
|
HttpServer::new(
|
||||||
|| Application::new()
|
|| Application::new()
|
||||||
.resource("/", |r| r.h(httpcodes::HTTPOk)))
|
.resource("/", |r| r.h(httpcodes::HTTPOk)))
|
||||||
.threads(4); // <- Start 4 workers
|
.threads(4); // <- Start 4 workers
|
||||||
@ -143,12 +141,10 @@ connection behavior is defined by server settings.
|
|||||||
```rust
|
```rust
|
||||||
# extern crate actix_web;
|
# extern crate actix_web;
|
||||||
# extern crate tokio_core;
|
# extern crate tokio_core;
|
||||||
# use tokio_core::net::TcpStream;
|
|
||||||
# use std::net::SocketAddr;
|
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
HttpServer::<TcpStream, SocketAddr, _, _>::new(||
|
HttpServer::new(||
|
||||||
Application::new()
|
Application::new()
|
||||||
.resource("/", |r| r.h(httpcodes::HTTPOk)))
|
.resource("/", |r| r.h(httpcodes::HTTPOk)))
|
||||||
.keep_alive(None); // <- Use `SO_KEEPALIVE` socket option.
|
.keep_alive(None); // <- Use `SO_KEEPALIVE` socket option.
|
||||||
|
@ -2,7 +2,6 @@ use std::{io, net, thread};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::{Arc, mpsc as sync_mpsc};
|
use std::sync::{Arc, mpsc as sync_mpsc};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
@ -21,7 +20,7 @@ use native_tls::TlsAcceptor;
|
|||||||
use openssl::ssl::{AlpnError, SslAcceptorBuilder};
|
use openssl::ssl::{AlpnError, SslAcceptorBuilder};
|
||||||
|
|
||||||
use helpers;
|
use helpers;
|
||||||
use super::{HttpHandler, IntoHttpHandler, IoStream};
|
use super::{IntoHttpHandler, IoStream};
|
||||||
use super::{PauseServer, ResumeServer, StopServer};
|
use super::{PauseServer, ResumeServer, StopServer};
|
||||||
use super::channel::{HttpChannel, WrapperStream};
|
use super::channel::{HttpChannel, WrapperStream};
|
||||||
use super::worker::{Conn, Worker, StreamHandlerType, StopWorker};
|
use super::worker::{Conn, Worker, StreamHandlerType, StopWorker};
|
||||||
@ -35,17 +34,15 @@ use super::settings::{ServerSettings, WorkerSettings};
|
|||||||
/// `A` - peer address
|
/// `A` - peer address
|
||||||
///
|
///
|
||||||
/// `H` - request handler
|
/// `H` - request handler
|
||||||
pub struct HttpServer<A, H, U>
|
pub struct HttpServer<H> where H: IntoHttpHandler + 'static
|
||||||
where H: HttpHandler + 'static
|
|
||||||
{
|
{
|
||||||
h: Option<Rc<WorkerSettings<H>>>,
|
h: Option<Rc<WorkerSettings<H::Handler>>>,
|
||||||
addr: PhantomData<A>,
|
|
||||||
threads: usize,
|
threads: usize,
|
||||||
backlog: i32,
|
backlog: i32,
|
||||||
host: Option<String>,
|
host: Option<String>,
|
||||||
keep_alive: Option<u64>,
|
keep_alive: Option<u64>,
|
||||||
factory: Arc<Fn() -> U + Send + Sync>,
|
factory: Arc<Fn() -> Vec<H> + Send + Sync>,
|
||||||
workers: Vec<SyncAddress<Worker<H>>>,
|
workers: Vec<SyncAddress<Worker<H::Handler>>>,
|
||||||
sockets: HashMap<net::SocketAddr, net::TcpListener>,
|
sockets: HashMap<net::SocketAddr, net::TcpListener>,
|
||||||
accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>,
|
accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>,
|
||||||
exit: bool,
|
exit: bool,
|
||||||
@ -54,15 +51,11 @@ pub struct HttpServer<A, H, U>
|
|||||||
no_signals: bool,
|
no_signals: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<A, H, U> Sync for HttpServer<A, H, U> where H: HttpHandler + 'static {}
|
unsafe impl<H> Sync for HttpServer<H> where H: IntoHttpHandler {}
|
||||||
unsafe impl<A, H, U> Send for HttpServer<A, H, U> where H: HttpHandler + 'static {}
|
unsafe impl<H> Send for HttpServer<H> where H: IntoHttpHandler {}
|
||||||
|
|
||||||
|
|
||||||
impl<A, H, U, V> Actor for HttpServer<A, H, U>
|
impl<H> Actor for HttpServer<H> where H: IntoHttpHandler
|
||||||
where A: 'static,
|
|
||||||
H: HttpHandler,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
type Context = Context<Self>;
|
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>
|
impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
|
||||||
where A: 'static,
|
|
||||||
H: HttpHandler,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
/// Create new http server with application factory
|
/// Create new http server with application factory
|
||||||
pub fn new<F>(factory: F) -> Self
|
pub fn new<F, U>(factory: F) -> Self
|
||||||
where F: Sync + Send + 'static + Fn() -> U,
|
where F: Fn() -> U + Sync + Send + 'static,
|
||||||
|
U: IntoIterator<Item=H> + 'static,
|
||||||
{
|
{
|
||||||
|
let f = move || {
|
||||||
|
(factory)().into_iter().collect()
|
||||||
|
};
|
||||||
|
|
||||||
HttpServer{ h: None,
|
HttpServer{ h: None,
|
||||||
addr: PhantomData,
|
|
||||||
threads: num_cpus::get(),
|
threads: num_cpus::get(),
|
||||||
backlog: 2048,
|
backlog: 2048,
|
||||||
host: None,
|
host: None,
|
||||||
keep_alive: None,
|
keep_alive: None,
|
||||||
factory: Arc::new(factory),
|
factory: Arc::new(f),
|
||||||
workers: Vec::new(),
|
workers: Vec::new(),
|
||||||
sockets: HashMap::new(),
|
sockets: HashMap::new(),
|
||||||
accept: Vec::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>
|
impl<H: IntoHttpHandler> HttpServer<H>
|
||||||
where U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
/// Start listening for incoming connections.
|
/// Start listening for incoming connections.
|
||||||
///
|
///
|
||||||
@ -345,9 +336,7 @@ impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="tls")]
|
#[cfg(feature="tls")]
|
||||||
impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
|
impl<H: IntoHttpHandler> HttpServer<H>
|
||||||
where U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
/// Start listening for incoming tls connections.
|
/// Start listening for incoming tls connections.
|
||||||
pub fn start_tls(mut self, pkcs12: ::Pkcs12) -> io::Result<SyncAddress<Self>> {
|
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")]
|
#[cfg(feature="alpn")]
|
||||||
impl<H: HttpHandler, U, V> HttpServer<net::SocketAddr, H, U>
|
impl<H: IntoHttpHandler> HttpServer<H>
|
||||||
where U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
/// Start listening for incoming tls connections.
|
/// 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>
|
impl<H: IntoHttpHandler> HttpServer<H>
|
||||||
where A: 'static,
|
|
||||||
H: HttpHandler,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
/// Start listening for incoming connections from a stream.
|
/// Start listening for incoming connections from a stream.
|
||||||
///
|
///
|
||||||
/// This method uses only one thread for handling incoming connections.
|
/// 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,
|
where S: Stream<Item=(T, A), Error=io::Error> + 'static,
|
||||||
T: AsyncRead + AsyncWrite + 'static,
|
T: AsyncRead + AsyncWrite + 'static,
|
||||||
|
A: 'static
|
||||||
{
|
{
|
||||||
if !self.sockets.is_empty() {
|
if !self.sockets.is_empty() {
|
||||||
let addrs: Vec<(net::SocketAddr, net::TcpListener)> =
|
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 addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
||||||
let settings = ServerSettings::new(Some(addr), &self.host, secure);
|
let settings = ServerSettings::new(Some(addr), &self.host, secure);
|
||||||
let apps: Vec<_> = (*self.factory)()
|
let apps: Vec<_> = (*self.factory)()
|
||||||
.into_iter()
|
.into_iter().map(|h| h.into_handler(settings.clone())).collect();
|
||||||
.map(|h| h.into_handler(settings.clone())).collect();
|
|
||||||
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
|
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
@ -483,11 +466,7 @@ impl<A, H, U, V> HttpServer<A, H, U>
|
|||||||
/// Signals support
|
/// Signals support
|
||||||
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
|
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
|
||||||
/// message to `System` actor.
|
/// message to `System` actor.
|
||||||
impl<A, H, U, V> Handler<signal::Signal> for HttpServer<A, H, U>
|
impl<H: IntoHttpHandler> Handler<signal::Signal> for HttpServer<H>
|
||||||
where H: HttpHandler + 'static,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = ();
|
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,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: IntoHttpHandler,
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = ();
|
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,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: IntoHttpHandler,
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = ();
|
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>
|
impl<H: IntoHttpHandler> Handler<PauseServer> for HttpServer<H>
|
||||||
where H: HttpHandler + 'static,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = ();
|
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>
|
impl<H: IntoHttpHandler> Handler<ResumeServer> for HttpServer<H>
|
||||||
where H: HttpHandler + 'static,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = ();
|
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>
|
impl<H: IntoHttpHandler> Handler<StopServer> for HttpServer<H>
|
||||||
where H: HttpHandler + 'static,
|
|
||||||
U: IntoIterator<Item=V> + 'static,
|
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
A: 'static,
|
|
||||||
{
|
{
|
||||||
type Result = actix::Response<Self, StopServer>;
|
type Result = actix::Response<Self, StopServer>;
|
||||||
|
|
||||||
|
13
src/test.rs
13
src/test.rs
@ -24,7 +24,7 @@ use router::Router;
|
|||||||
use payload::Payload;
|
use payload::Payload;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use server::{HttpServer, HttpHandler, IntoHttpHandler, ServerSettings};
|
use server::{HttpServer, IntoHttpHandler, ServerSettings};
|
||||||
use ws::{WsClient, WsClientError, WsClientReader, WsClientWriter};
|
use ws::{WsClient, WsClientError, WsClientReader, WsClientWriter};
|
||||||
|
|
||||||
/// The `TestServer` type.
|
/// The `TestServer` type.
|
||||||
@ -72,11 +72,10 @@ impl TestServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Start new test server with application factory
|
/// Start new test server with application factory
|
||||||
pub fn with_factory<H, F, U, V>(factory: F) -> Self
|
pub fn with_factory<F, U, H>(factory: F) -> Self
|
||||||
where H: HttpHandler,
|
where F: Fn() -> U + Sync + Send + 'static,
|
||||||
F: Sync + Send + 'static + Fn() -> U,
|
U: IntoIterator<Item=H> + 'static,
|
||||||
U: IntoIterator<Item=V> + 'static,
|
H: IntoHttpHandler + 'static,
|
||||||
V: IntoHttpHandler<Handler=H>,
|
|
||||||
{
|
{
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
@ -123,7 +122,7 @@ impl TestServer {
|
|||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let mut app = TestApp::new(state());
|
let mut app = TestApp::new(state());
|
||||||
config(&mut app);
|
config(&mut app);
|
||||||
app}
|
vec![app]}
|
||||||
).disable_signals().start_incoming(tcp.incoming(), false);
|
).disable_signals().start_incoming(tcp.incoming(), false);
|
||||||
|
|
||||||
tx.send((Arbiter::system(), local_addr)).unwrap();
|
tx.send((Arbiter::system(), local_addr)).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user