mirror of
https://github.com/actix/actix-website
synced 2024-11-27 18: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)
|
[`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,
|
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)
|
[`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.
|
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)
|
- use [`start()`](../../actix-web/actix_web/server/struct.HttpServer.html#method.start)
|
||||||
for a server
|
for a server
|
||||||
|
|
||||||
`HttpServer` is an actix actor. It must be initialized within a properly
|
|
||||||
configured actix system:
|
|
||||||
|
|
||||||
{{< include-example example="server" section="main" >}}
|
{{< include-example example="server" section="main" >}}
|
||||||
|
|
||||||
> It is possible to start a server in a separate thread with the `run()` method. In that
|
> 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
|
## SSL
|
||||||
|
|
||||||
There are two features for ssl server: `tls` and `alpn`. The `tls` feature is
|
There are two features for ssl server: `rust-tls` and `ssl`. The `tls` feature is
|
||||||
for `native-tls` integration and `alpn` is for `openssl`.
|
for `rust-tls` integration and `ssl` is for `openssl`.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[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" >}}
|
{{< 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*.
|
- `None` or `KeepAlive::Disabled` - disable *keep alive*.
|
||||||
- `KeepAlive::Tcp(75)` - use `SO_KEEPALIVE` socket option.
|
- `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
|
If the first option is selected, then *keep alive* state is
|
||||||
calculated based on the response's *connection-type*. By default
|
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.
|
*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
|
## Graceful shutdown
|
||||||
|
|
||||||
|
@ -2,9 +2,10 @@
|
|||||||
name = "server"
|
name = "server"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
workspace = "../"
|
workspace = "../"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.7"
|
actix-rt = "0.2"
|
||||||
actix-web = "1.0"
|
actix-web = { version = "1.0", features = ["ssl"] }
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
openssl = "0.10"
|
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>
|
// <example>
|
||||||
use actix_web::{http, HttpRequest, HttpResponse};
|
use actix_web::{http, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> HttpResponse {
|
pub fn index(req: HttpRequest) -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.connection_type(http::ConnectionType::Close) // <- Close connection
|
.connection_type(http::ConnectionType::Close) // <- Close connection
|
||||||
.force_close() // <- Alternative method
|
.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,22 +1,19 @@
|
|||||||
extern crate actix;
|
mod keep_alive;
|
||||||
extern crate actix_web;
|
mod keep_alive_tp;
|
||||||
extern crate futures;
|
|
||||||
extern crate openssl;
|
|
||||||
|
|
||||||
mod ka;
|
|
||||||
mod ka_tp;
|
|
||||||
mod signals;
|
mod signals;
|
||||||
mod ssl;
|
mod ssl;
|
||||||
mod workers;
|
mod workers;
|
||||||
|
|
||||||
// <main>
|
// <main>
|
||||||
use actix_web::{server::HttpServer, App, HttpResponse};
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
|
|
||||||
fn main() {
|
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())))
|
HttpServer::new(|| {
|
||||||
.bind("127.0.0.1:59080")
|
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
|
@ -1,28 +1,41 @@
|
|||||||
use actix;
|
use actix_rt;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
// <signals>
|
// <signals>
|
||||||
use actix_web::{server, App, HttpResponse};
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
fn main() {
|
pub fn main() {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = actix::System::new("http-server");
|
let sys = actix_rt::System::new("http-server");
|
||||||
let addr = server::new(|| {
|
|
||||||
App::new()
|
let addr = HttpServer::new(|| {
|
||||||
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
let _ = tx.send(addr);
|
let _ = tx.send(addr);
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
});
|
});
|
||||||
|
|
||||||
let addr = rx.recv().unwrap();
|
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>
|
// </signals>
|
||||||
|
@ -1,22 +1,26 @@
|
|||||||
// <ssl>
|
// <ssl>
|
||||||
use actix_web::{server, App, HttpRequest, Responder};
|
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
|
|
||||||
fn index(req: &HttpRequest) -> impl Responder {
|
fn index(_req: HttpRequest) -> impl Responder {
|
||||||
"Welcome!"
|
"Welcome!"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
pub fn main() {
|
||||||
// load ssl keys
|
// load ssl keys
|
||||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
let mut builder =
|
||||||
|
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||||
builder
|
builder
|
||||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||||
|
|
||||||
server::new(|| App::new().resource("/index.html", |r| r.f(index)))
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
.bind_ssl("127.0.0.1:8080", builder)
|
.bind_ssl("127.0.0.1:8088", builder)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.run();
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </ssl>
|
// </ssl>
|
||||||
|
//
|
||||||
|
// sssl rust-tls
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
// <workers>
|
// <workers>
|
||||||
use actix_web::{server::HttpServer, App, HttpResponse};
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
|
|
||||||
fn main() {
|
pub fn main() {
|
||||||
HttpServer::new(|| App::new().resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
HttpServer::new(|| {
|
||||||
|
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||||
|
})
|
||||||
.workers(4); // <- Start 4 workers
|
.workers(4); // <- Start 4 workers
|
||||||
}
|
}
|
||||||
// </workers>
|
// </workers>
|
||||||
|
Loading…
Reference in New Issue
Block a user