From 2124730e0a03498ec2af54ca5db49309ef8a80f2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 18 Dec 2017 18:56:58 -0800 Subject: [PATCH] guide update --- Makefile | 2 +- guide/src/qs_3_5.md | 30 ++++++++++++++++- src/server.rs | 78 +++++++++++++++++++++++++-------------------- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index fc6071083..fdc3cbbc0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: default build test doc book clean -CARGO_FLAGS := --features "$(FEATURES)" +CARGO_FLAGS := --features "$(FEATURES) alpn" default: test diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md index 4afbff4fb..70eb48095 100644 --- a/guide/src/qs_3_5.md +++ b/guide/src/qs_3_5.md @@ -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::::new( || Application::new() - .resource("/", |r| r.f(|r| httpcodes::HTTPOk))) + .resource("/", |r| r.f(|_| httpcodes::HTTPOk))) .threads(4); // <- Start 4 threads } ``` diff --git a/src/server.rs b/src/server.rs index 5961a40a1..9bec090a2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -174,41 +174,6 @@ impl HttpServer self } - /// Start listening for incomming connections from a stream. - /// - /// This method uses only one thread for handling incoming connections. - pub fn start_incoming(mut self, stream: S, secure: bool) -> io::Result> - where S: Stream + '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 HttpServer, net::SocketAddr, H, } } +impl HttpServer + where A: 'static, + T: AsyncRead + AsyncWrite + 'static, + H: HttpHandler, + U: IntoIterator + 'static, + V: IntoHttpHandler, +{ + /// Start listening for incomming connections from a stream. + /// + /// This method uses only one thread for handling incoming connections. + pub fn start_incoming(mut self, stream: S, secure: bool) -> io::Result> + where S: Stream + '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 { io: T, peer: Option,