mirror of
https://github.com/actix/actix-website
synced 2024-11-30 11:12:57 +01:00
First pass at Server section.
This commit is contained in:
parent
992d182910
commit
8ab3e35851
@ -17,15 +17,12 @@ To bind to a specific socket address,
|
||||
[`bind()`](../../actix-web/actix_web/server/struct.HttpServer.html#method.bind)
|
||||
must be used, and it may be called multiple times. To bind ssl socket,
|
||||
[`bind_ssl()`](../../actix-web/actix_web/server/struct.HttpServer.html#method.bind_ssl)
|
||||
or [`bind_tls()`](../../actix-web/actix_web/server/struct.HttpServer.html#method.bind_tls)
|
||||
or [`bind_rustls()`](../../actix-web/1.0.0/actix_web/struct.HttpServer.html#method.bind_rustls)
|
||||
should be used. To start the http server, use one of the start methods.
|
||||
|
||||
- use [`start()`](../../actix-web/actix_web/server/struct.HttpServer.html#method.start)
|
||||
for a server
|
||||
|
||||
`HttpServer` is an actix actor. It must be initialized within a properly
|
||||
configured actix system:
|
||||
|
||||
{{< include-example example="server" section="main" >}}
|
||||
|
||||
> It is possible to start a server in a separate thread with the `run()` method. In that
|
||||
@ -59,12 +56,12 @@ is not shared between threads. To share state, `Arc` could be used.
|
||||
|
||||
## SSL
|
||||
|
||||
There are two features for ssl server: `tls` and `alpn`. The `tls` feature is
|
||||
for `native-tls` integration and `alpn` is for `openssl`.
|
||||
There are two features for ssl server: `rust-tls` and `ssl`. The `tls` feature is
|
||||
for `rust-tls` integration and `ssl` is for `openssl`.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
actix-web = { version = "{{< actix-version "actix-web" >}}", features = ["alpn"] }
|
||||
actix-web = { version = "{{< actix-version "actix-web" >}}", features = ["ssl"] }
|
||||
```
|
||||
|
||||
{{< include-example example="server" file="ssl.rs" section="ssl" >}}
|
||||
@ -95,7 +92,7 @@ Actix can wait for requests on a keep-alive connection.
|
||||
- `None` or `KeepAlive::Disabled` - disable *keep alive*.
|
||||
- `KeepAlive::Tcp(75)` - use `SO_KEEPALIVE` socket option.
|
||||
|
||||
{{< include-example example="server" file="ka.rs" section="ka" >}}
|
||||
{{< include-example example="server" file="keep_alive.rs" section="keep-alive" >}}
|
||||
|
||||
If the first option is selected, then *keep alive* state is
|
||||
calculated based on the response's *connection-type*. By default
|
||||
@ -106,7 +103,7 @@ defined by the request's http version.
|
||||
|
||||
*Connection type* can be changed with `HttpResponseBuilder::connection_type()` method.
|
||||
|
||||
{{< include-example example="server" file="ka_tp.rs" section="example" >}}
|
||||
{{< include-example example="server" file="keep_alive_tp.rs" section="example" >}}
|
||||
|
||||
## Graceful shutdown
|
||||
|
||||
|
@ -2,9 +2,10 @@
|
||||
name = "server"
|
||||
version = "0.7.0"
|
||||
workspace = "../"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.7"
|
||||
actix-web = "1.0"
|
||||
actix-rt = "0.2"
|
||||
actix-web = { version = "1.0", features = ["ssl"] }
|
||||
futures = "0.1"
|
||||
openssl = "0.10"
|
||||
|
1
examples/server/rustfmt.toml
Normal file
1
examples/server/rustfmt.toml
Normal file
@ -0,0 +1 @@
|
||||
max_width = 80
|
@ -1,14 +0,0 @@
|
||||
// <ka>
|
||||
use actix_web::{server, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
server::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(75); // <- Set keep-alive to 75 seconds
|
||||
|
||||
server::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option.
|
||||
|
||||
server::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
}
|
||||
// </ka>
|
@ -1,7 +1,7 @@
|
||||
// <example>
|
||||
use actix_web::{http, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
pub fn index(req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.connection_type(http::ConnectionType::Close) // <- Close connection
|
||||
.force_close() // <- Alternative method
|
||||
|
20
examples/server/src/keep_alive.rs
Normal file
20
examples/server/src/keep_alive.rs
Normal file
@ -0,0 +1,20 @@
|
||||
// <keep-alive>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.keep_alive(75); // <- Set keep-alive to 75 seconds
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option.
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
}
|
||||
// </keep-alive>
|
10
examples/server/src/keep_alive_tp.rs
Normal file
10
examples/server/src/keep_alive_tp.rs
Normal file
@ -0,0 +1,10 @@
|
||||
// <example>
|
||||
use actix_web::{http, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.connection_type(http::ConnectionType::Close) // <- Close connection
|
||||
.force_close() // <- Alternative method
|
||||
.finish()
|
||||
}
|
||||
// </example>
|
@ -1,24 +1,21 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate futures;
|
||||
extern crate openssl;
|
||||
|
||||
mod ka;
|
||||
mod ka_tp;
|
||||
mod keep_alive;
|
||||
mod keep_alive_tp;
|
||||
mod signals;
|
||||
mod ssl;
|
||||
mod workers;
|
||||
|
||||
// <main>
|
||||
use actix_web::{server::HttpServer, App, HttpResponse};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("guide");
|
||||
let sys = actix_rt::System::new("example");
|
||||
|
||||
HttpServer::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.bind("127.0.0.1:59080")
|
||||
.unwrap()
|
||||
.start();
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.start();
|
||||
|
||||
let _ = sys.run();
|
||||
}
|
||||
|
@ -1,28 +1,41 @@
|
||||
use actix;
|
||||
use actix_rt;
|
||||
use futures::Future;
|
||||
|
||||
// <signals>
|
||||
use actix_web::{server, App, HttpResponse};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
pub fn main() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
let sys = actix::System::new("http-server");
|
||||
let addr = server::new(|| {
|
||||
App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
||||
let sys = actix_rt::System::new("http-server");
|
||||
|
||||
let addr = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
.start();
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
.start();
|
||||
|
||||
let _ = tx.send(addr);
|
||||
let _ = sys.run();
|
||||
});
|
||||
|
||||
let addr = rx.recv().unwrap();
|
||||
let _ = addr.send(server::StopServer { graceful: true }).wait(); // <- Send `StopServer` message to server.
|
||||
let _ = addr
|
||||
.pause()
|
||||
.wait()
|
||||
.map(|_| println!("`actix_server::ServerCommand::Pause`"));
|
||||
let _ = addr
|
||||
.resume()
|
||||
.wait()
|
||||
.map(|_| println!("`actix_server::ServerCommand::Resume`"));
|
||||
let _ = addr
|
||||
.stop(true)
|
||||
.wait()
|
||||
.map(|_| println!("`actix_server::ServerCommand::Stop`"));
|
||||
}
|
||||
// </signals>
|
||||
|
@ -1,22 +1,26 @@
|
||||
// <ssl>
|
||||
use actix_web::{server, App, HttpRequest, Responder};
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||
|
||||
fn index(req: &HttpRequest) -> impl Responder {
|
||||
fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Welcome!"
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pub fn main() {
|
||||
// load ssl keys
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
let mut builder =
|
||||
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||
|
||||
server::new(|| App::new().resource("/index.html", |r| r.f(index)))
|
||||
.bind_ssl("127.0.0.1:8080", builder)
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind_ssl("127.0.0.1:8088", builder)
|
||||
.unwrap()
|
||||
.run();
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </ssl>
|
||||
//
|
||||
// sssl rust-tls
|
||||
|
@ -1,8 +1,10 @@
|
||||
// <workers>
|
||||
use actix_web::{server::HttpServer, App, HttpResponse};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.workers(4); // <- Start 4 workers
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.workers(4); // <- Start 4 workers
|
||||
}
|
||||
// </workers>
|
||||
|
Loading…
Reference in New Issue
Block a user