mirror of
https://github.com/actix/actix-website
synced 2025-02-02 12:19:04 +01:00
review server section
This commit is contained in:
parent
e71c712d16
commit
e30c20016e
@ -13,24 +13,19 @@ must have `Send` + `Sync` boundaries. More about that in the *multi-threading* s
|
|||||||
|
|
||||||
To bind to a specific socket address, [`bind()`][bindmethod] must be used, and it may be
|
To bind to a specific socket address, [`bind()`][bindmethod] must be used, and it may be
|
||||||
called multiple times. To bind ssl socket, [`bind_openssl()`][bindopensslmethod] or
|
called multiple times. To bind ssl socket, [`bind_openssl()`][bindopensslmethod] or
|
||||||
[`bind_rustls()`][bindrusttls] should be used. To start the http server, use one of the
|
[`bind_rustls()`][bindrusttls] should be used. To run the http server, use `HttpServer::run()`
|
||||||
start methods.
|
method.
|
||||||
|
|
||||||
- use [`start()`] for a server
|
|
||||||
|
|
||||||
{{< 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
|
`run()` method returns instance of [`Server`][server] type. Methods of server type
|
||||||
> case the server spawns a new thread and creates a new actix system in it. To stop
|
could be used for managing http server
|
||||||
> this server, send a `StopServer` message.
|
|
||||||
|
|
||||||
`HttpServer` is implemented as an actix actor. It is possible to communicate with the server
|
- `pause()` - Pause accepting incoming connections
|
||||||
via a messaging system. Start method, e.g. `start()`, returns the address of the started
|
- `resume()` - Resume accepting incoming connections
|
||||||
http server. It accepts several messages:
|
- `stop()` - Stop incoming connection processing, stop all workers and exit
|
||||||
|
|
||||||
- `PauseServer` - Pause accepting incoming connections
|
Following example shows how to start http server in separate thread.
|
||||||
- `ResumeServer` - Resume accepting incoming connections
|
|
||||||
- `StopServer` - Stop incoming connection processing, stop all workers and exit
|
|
||||||
|
|
||||||
{{< include-example example="server" file="signals.rs" section="signals" >}}
|
{{< include-example example="server" file="signals.rs" section="signals" >}}
|
||||||
|
|
||||||
@ -45,16 +40,17 @@ equal to number of logical CPUs in the system. This number can be overridden wit
|
|||||||
The server creates a separate application instance for each created worker. Application state
|
The server creates a separate application instance for each created worker. Application state
|
||||||
is not shared between threads. To share state, `Arc` could be used.
|
is not shared between threads. To share state, `Arc` could be used.
|
||||||
|
|
||||||
> Application state does not need to be `Send` and `Sync`, but factories must be `Send` + `Sync`.
|
> Application state does not need to be `Send` or `Sync`, but application
|
||||||
|
factory must be `Send` + `Sync`.
|
||||||
|
|
||||||
## SSL
|
## SSL
|
||||||
|
|
||||||
There are two features for the ssl server: `rust-tls` and `ssl`. The `rust-tls` feature is for
|
There are two features for the ssl server: `rustls` and `openssl`. The `rustls` feature is for
|
||||||
`rustls` integration and `ssl` is for `openssl`.
|
`rustls` integration and `openssl` is for `openssl`.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "{{< actix-version "actix-web" >}}", features = ["ssl"] }
|
actix-web = { version = "{{< actix-version "actix-web" >}}", features = ["openssl"] }
|
||||||
openssl = { version="0.10" }
|
openssl = { version="0.10" }
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -117,6 +113,7 @@ are available on unix systems.
|
|||||||
> It is possible to disable signal handling with
|
> It is possible to disable signal handling with
|
||||||
[`HttpServer::disable_signals()`][disablesignals] method.
|
[`HttpServer::disable_signals()`][disablesignals] method.
|
||||||
|
|
||||||
|
[server]: https://docs.rs/actix-web/2/actix_web/dev/struct.Server.html
|
||||||
[httpserverstruct]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html
|
[httpserverstruct]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html
|
||||||
[bindmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind
|
[bindmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind
|
||||||
[bindopensslmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_openssl
|
[bindopensslmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_openssl
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// <setup>
|
// <setup>
|
||||||
use actix_web::{web, App, Responder};
|
use actix_web::{web, App, Responder, HttpServer};
|
||||||
|
|
||||||
async fn index() -> impl Responder {
|
async fn index() -> impl Responder {
|
||||||
"Hello world!"
|
"Hello world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app").route("/index.html", web::get().to(index)),
|
web::scope("/app").route("/index.html", web::get().to(index)),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_web::{web, App, HttpResponse};
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
|
|
||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod combine;
|
pub mod combine;
|
||||||
@ -10,7 +10,7 @@ pub mod vh;
|
|||||||
|
|
||||||
// <multi>
|
// <multi>
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
|
@ -4,29 +4,31 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
pub fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = System::new("http-server");
|
let sys = System::new("http-server");
|
||||||
|
|
||||||
let addr = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.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
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
let _ = tx.send(addr);
|
let _ = tx.send(srv);
|
||||||
|
sys.run()
|
||||||
});
|
});
|
||||||
|
|
||||||
let addr = rx.recv().unwrap();
|
let srv = rx.recv().unwrap();
|
||||||
let _ = System::new("`actix_server::ServerCommand::Pause`")
|
|
||||||
.block_on(addr.pause());
|
// pause accepting new connections
|
||||||
let _ = System::new("`actix_server::ServerCommand::Resume`")
|
srv.pause().await;
|
||||||
.block_on(addr.resume());
|
// resume accepting new connections
|
||||||
let _ = System::new("`actix_server::ServerCommand::Stop`")
|
srv.resume().await;
|
||||||
.block_on(addr.stop(true));
|
// stop server
|
||||||
|
srv.stop(true).await;
|
||||||
}
|
}
|
||||||
// </signals>
|
// </signals>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user