1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-26 19:41:12 +00:00

guide update

This commit is contained in:
Nikolay Kim 2017-12-18 18:56:58 -08:00
parent e9a3845e26
commit 2124730e0a
3 changed files with 73 additions and 37 deletions

View file

@ -1,6 +1,6 @@
.PHONY: default build test doc book clean
CARGO_FLAGS := --features "$(FEATURES)"
CARGO_FLAGS := --features "$(FEATURES) alpn"
default: test

View file

@ -1,5 +1,33 @@
# Server
[*HttpServer*](../actix_web/struct.HttpServer.html) type is responsible for
serving http requests. *HttpServer* accept applicaiton factory as a parameter,
Application factory must have `Send` + `Sync` bounderies. More about that in
*multi-threading* section. To bind to specific socket address `bind()` must be used.
This method could be called multiple times. To start http server one of the *start*
methods could be used. `start()` method start simple server, `start_tls()` or `start_ssl()`
starts ssl server. *HttpServer* is an actix actor, it has to be initialized
within properly configured actix system:
```rust
# extern crate actix;
# extern crate actix_web;
use actix::*;
use actix_web::*;
fn main() {
let sys = actix::System::new("guide");
HttpServer::new(
|| Application::new()
.resource("/", |r| r.f(|_| httpcodes::HTTPOk)))
.bind("127.0.0.1:59080").unwrap()
.start().unwrap();
# actix::Arbiter::system().send(actix::msgs::SystemExit(0));
let _ = sys.run();
}
```
## Multi-threading
@ -18,7 +46,7 @@ use actix_web::*;
fn main() {
HttpServer::<TcpStream, SocketAddr, _, _>::new(
|| Application::new()
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)))
.resource("/", |r| r.f(|_| httpcodes::HTTPOk)))
.threads(4); // <- Start 4 threads
}
```

View file

@ -174,41 +174,6 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
self
}
/// Start listening for incomming connections from a stream.
///
/// This method uses only one thread for handling incoming connections.
pub fn start_incoming<S>(mut self, stream: S, secure: bool) -> io::Result<SyncAddress<Self>>
where S: Stream<Item=(T, A), Error=io::Error> + 'static
{
if !self.sockets.is_empty() {
let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect();
let settings = ServerSettings::new(Some(addrs[0].0), false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
// start acceptors threads
for (addr, sock) in addrs {
info!("Starting http server on {}", addr);
start_accept_thread(sock, addr, workers.clone());
}
}
// set server settings
let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
let settings = ServerSettings::new(Some(addr), secure);
let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect();
for app in &mut apps {
app.server_settings(settings.clone());
}
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
// start server
Ok(HttpServer::create(move |ctx| {
ctx.add_stream(stream.map(
move |(t, _)| IoStream{io: t, peer: None, http2: false}));
self
}))
}
/// The socket address to bind
///
/// To mind multiple addresses this method can be call multiple times.
@ -391,6 +356,49 @@ impl<H: HttpHandler, U, V> HttpServer<SslStream<TcpStream>, net::SocketAddr, H,
}
}
impl<T, A, H, U, V> HttpServer<T, A, H, U>
where A: 'static,
T: AsyncRead + AsyncWrite + 'static,
H: HttpHandler,
U: IntoIterator<Item=V> + 'static,
V: IntoHttpHandler<Handler=H>,
{
/// Start listening for incomming connections from a stream.
///
/// This method uses only one thread for handling incoming connections.
pub fn start_incoming<S>(mut self, stream: S, secure: bool) -> io::Result<SyncAddress<Self>>
where S: Stream<Item=(T, A), Error=io::Error> + 'static
{
if !self.sockets.is_empty() {
let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect();
let settings = ServerSettings::new(Some(addrs[0].0), false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
// start acceptors threads
for (addr, sock) in addrs {
info!("Starting http server on {}", addr);
start_accept_thread(sock, addr, workers.clone());
}
}
// set server settings
let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
let settings = ServerSettings::new(Some(addr), secure);
let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect();
for app in &mut apps {
app.server_settings(settings.clone());
}
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
// start server
Ok(HttpServer::create(move |ctx| {
ctx.add_stream(stream.map(
move |(t, _)| IoStream{io: t, peer: None, http2: false}));
self
}))
}
}
struct IoStream<T> {
io: T,
peer: Option<net::SocketAddr>,