From e21219c1daf6f042742acfd579b5a29c67f0091d Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 29 Dec 2019 03:32:56 +0900 Subject: [PATCH] Update server --- content/docs/server.md | 18 +++++++++--------- examples/server/Cargo.toml | 8 ++++---- examples/server/src/keep_alive.rs | 5 +++-- examples/server/src/keep_alive_tp.rs | 2 +- examples/server/src/main.rs | 13 +++++-------- examples/server/src/signals.rs | 27 +++++++++------------------ examples/server/src/ssl.rs | 10 +++++----- examples/server/src/workers.rs | 3 ++- 8 files changed, 38 insertions(+), 48 deletions(-) diff --git a/content/docs/server.md b/content/docs/server.md index 8c0c6f0..8a6da09 100644 --- a/content/docs/server.md +++ b/content/docs/server.md @@ -12,7 +12,7 @@ The [**HttpServer**][httpserverstruct] type is responsible for serving http requ must have `Send` + `Sync` boundaries. More about that in the *multi-threading* section. To bind to a specific socket address, [`bind()`][bindmethod] must be used, and it may be -called multiple times. To bind ssl socket, [`bind_ssl()`][bindsslmethod] 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 start methods. @@ -117,13 +117,13 @@ are available on unix systems. > It is possible to disable signal handling with [`HttpServer::disable_signals()`][disablesignals] method. -[httpserverstruct]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html -[bindmethod]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.bind -[bindsslmethod]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.bind_ssl -[bindrusttls]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.bind_rustls -[startmethod]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.start -[workers]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.workers +[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 +[bindopensslmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_openssl +[bindrusttls]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_rustls +[startmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.start +[workers]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.workers [tlsalpn]: https://tools.ietf.org/html/rfc7301 [exampletls]: https://github.com/actix/examples/tree/master/tls -[shutdowntimeout]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.shutdown_timeout -[disablesignals]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.disable_signals +[shutdowntimeout]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.shutdown_timeout +[disablesignals]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.disable_signals diff --git a/examples/server/Cargo.toml b/examples/server/Cargo.toml index d3bfa22..c620da1 100644 --- a/examples/server/Cargo.toml +++ b/examples/server/Cargo.toml @@ -5,8 +5,8 @@ workspace = "../" edition = "2018" [dependencies] -actix-rt = "0.2" -actix-web = { version = "1.0", features = ["ssl"] } -futures = "0.1" +actix-rt = "1.0" +actix-web = { version = "2.0", features = ["openssl"] } +futures = "0.3.1" openssl = "0.10" -actix-http = "0.2" +actix-http = "1.0" diff --git a/examples/server/src/keep_alive.rs b/examples/server/src/keep_alive.rs index d997286..a59b743 100644 --- a/examples/server/src/keep_alive.rs +++ b/examples/server/src/keep_alive.rs @@ -1,7 +1,8 @@ // use actix_web::{web, App, HttpResponse, HttpServer}; -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { let one = HttpServer::new(|| { App::new().route("/", web::get().to(|| HttpResponse::Ok())) }) @@ -17,6 +18,6 @@ pub fn main() { }) .keep_alive(None); // <- Disable keep-alive - one.bind("127.0.0.1:8088").unwrap().run().unwrap(); + one.bind("127.0.0.1:8088")?.run().await } // diff --git a/examples/server/src/keep_alive_tp.rs b/examples/server/src/keep_alive_tp.rs index 19287ee..36ea2b3 100644 --- a/examples/server/src/keep_alive_tp.rs +++ b/examples/server/src/keep_alive_tp.rs @@ -1,7 +1,7 @@ // use actix_web::{http, HttpRequest, HttpResponse}; -pub fn index(req: HttpRequest) -> HttpResponse { +async fn index(req: HttpRequest) -> HttpResponse { HttpResponse::Ok() .connection_type(http::ConnectionType::Close) // <- Close connection .force_close() // <- Alternative method diff --git a/examples/server/src/main.rs b/examples/server/src/main.rs index 6d43ee1..aeb8543 100644 --- a/examples/server/src/main.rs +++ b/examples/server/src/main.rs @@ -7,16 +7,13 @@ pub mod workers; //
use actix_web::{web, App, HttpResponse, HttpServer}; -fn main() { - let sys = actix_rt::System::new("example"); - +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().route("/", web::get().to(|| HttpResponse::Ok())) }) - .bind("127.0.0.1:8088") - .unwrap() - .start(); - - let _ = sys.run(); + .bind("127.0.0.1:8088")? + .run() + .await } //
diff --git a/examples/server/src/signals.rs b/examples/server/src/signals.rs index f926622..f623e0d 100644 --- a/examples/server/src/signals.rs +++ b/examples/server/src/signals.rs @@ -1,7 +1,5 @@ -use actix_rt; -use futures::Future; - // +use actix_rt::System; use actix_web::{web, App, HttpResponse, HttpServer}; use std::sync::mpsc; use std::thread; @@ -10,7 +8,7 @@ pub fn main() { let (tx, rx) = mpsc::channel(); thread::spawn(move || { - let sys = actix_rt::System::new("http-server"); + let sys = System::new("http-server"); let addr = HttpServer::new(|| { App::new().route("/", web::get().to(|| HttpResponse::Ok())) @@ -18,24 +16,17 @@ pub fn main() { .bind("127.0.0.1:8088") .unwrap() .shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds - .start(); + .run(); let _ = tx.send(addr); - let _ = sys.run(); }); let addr = rx.recv().unwrap(); - 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`")); + let _ = System::new("`actix_server::ServerCommand::Pause`") + .block_on(addr.pause()); + let _ = System::new("`actix_server::ServerCommand::Resume`") + .block_on(addr.resume()); + let _ = System::new("`actix_server::ServerCommand::Stop`") + .block_on(addr.stop(true)); } // diff --git a/examples/server/src/ssl.rs b/examples/server/src/ssl.rs index 4efb8de..a21946a 100644 --- a/examples/server/src/ssl.rs +++ b/examples/server/src/ssl.rs @@ -2,11 +2,12 @@ use actix_web::{web, App, HttpRequest, HttpServer, Responder}; use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; -fn index(_req: HttpRequest) -> impl Responder { +async fn index(_req: HttpRequest) -> impl Responder { "Welcome!" } -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { // load ssl keys // to create a self-signed temporary cert for testing: // `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'` @@ -18,10 +19,9 @@ pub fn main() { builder.set_certificate_chain_file("cert.pem").unwrap(); HttpServer::new(|| App::new().route("/", web::get().to(index))) - .bind_ssl("127.0.0.1:8088", builder) - .unwrap() + .bind_openssl("127.0.0.1:8088", builder)? .run() - .unwrap(); + .await } // // diff --git a/examples/server/src/workers.rs b/examples/server/src/workers.rs index f7eba99..d2b4649 100644 --- a/examples/server/src/workers.rs +++ b/examples/server/src/workers.rs @@ -1,7 +1,8 @@ // use actix_web::{web, App, HttpResponse, HttpServer}; -pub fn main() { +#[actix_rt::main] +async fn main() { HttpServer::new(|| { App::new().route("/", web::get().to(|| HttpResponse::Ok())) })