1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 11:12:57 +01:00

Update server

This commit is contained in:
Yuki Okushi 2019-12-29 03:32:56 +09:00
parent a255f4aea6
commit e21219c1da
8 changed files with 38 additions and 48 deletions

View File

@ -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. 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 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 [`bind_rustls()`][bindrusttls] should be used. To start the http server, use one of the
start methods. start methods.
@ -117,13 +117,13 @@ 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.
[httpserverstruct]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html [httpserverstruct]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html
[bindmethod]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.bind [bindmethod]: https://docs.rs/actix-web/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 [bindopensslmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.bind_openssl
[bindrusttls]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.bind_rustls [bindrusttls]: https://docs.rs/actix-web/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 [startmethod]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.start
[workers]: https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpServer.html#method.workers [workers]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.workers
[tlsalpn]: https://tools.ietf.org/html/rfc7301 [tlsalpn]: https://tools.ietf.org/html/rfc7301
[exampletls]: https://github.com/actix/examples/tree/master/tls [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 [shutdowntimeout]: https://docs.rs/actix-web/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 [disablesignals]: https://docs.rs/actix-web/2/actix_web/struct.HttpServer.html#method.disable_signals

View File

@ -5,8 +5,8 @@ workspace = "../"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
actix-rt = "0.2" actix-rt = "1.0"
actix-web = { version = "1.0", features = ["ssl"] } actix-web = { version = "2.0", features = ["openssl"] }
futures = "0.1" futures = "0.3.1"
openssl = "0.10" openssl = "0.10"
actix-http = "0.2" actix-http = "1.0"

View File

@ -1,7 +1,8 @@
// <keep-alive> // <keep-alive>
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
pub fn main() { #[actix_rt::main]
async fn main() -> std::io::Result<()> {
let one = HttpServer::new(|| { let one = HttpServer::new(|| {
App::new().route("/", web::get().to(|| HttpResponse::Ok())) App::new().route("/", web::get().to(|| HttpResponse::Ok()))
}) })
@ -17,6 +18,6 @@ pub fn main() {
}) })
.keep_alive(None); // <- Disable keep-alive .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
} }
// </keep-alive> // </keep-alive>

View File

@ -1,7 +1,7 @@
// <example> // <example>
use actix_web::{http, HttpRequest, HttpResponse}; use actix_web::{http, HttpRequest, HttpResponse};
pub fn index(req: HttpRequest) -> HttpResponse { async 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

View File

@ -7,16 +7,13 @@ pub mod workers;
// <main> // <main>
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
fn main() { #[actix_rt::main]
let sys = actix_rt::System::new("example"); async fn main() -> std::io::Result<()> {
HttpServer::new(|| { 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() .run()
.start(); .await
let _ = sys.run();
} }
// </main> // </main>

View File

@ -1,7 +1,5 @@
use actix_rt;
use futures::Future;
// <signals> // <signals>
use actix_rt::System;
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
use std::sync::mpsc; use std::sync::mpsc;
use std::thread; use std::thread;
@ -10,7 +8,7 @@ pub fn main() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
thread::spawn(move || { thread::spawn(move || {
let sys = actix_rt::System::new("http-server"); let sys = System::new("http-server");
let addr = HttpServer::new(|| { let addr = HttpServer::new(|| {
App::new().route("/", web::get().to(|| HttpResponse::Ok())) App::new().route("/", web::get().to(|| HttpResponse::Ok()))
@ -18,24 +16,17 @@ pub fn main() {
.bind("127.0.0.1:8088") .bind("127.0.0.1:8088")
.unwrap() .unwrap()
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds .shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.start(); .run();
let _ = tx.send(addr); let _ = tx.send(addr);
let _ = sys.run();
}); });
let addr = rx.recv().unwrap(); let addr = rx.recv().unwrap();
let _ = addr let _ = System::new("`actix_server::ServerCommand::Pause`")
.pause() .block_on(addr.pause());
.wait() let _ = System::new("`actix_server::ServerCommand::Resume`")
.map(|_| println!("`actix_server::ServerCommand::Pause`")); .block_on(addr.resume());
let _ = addr let _ = System::new("`actix_server::ServerCommand::Stop`")
.resume() .block_on(addr.stop(true));
.wait()
.map(|_| println!("`actix_server::ServerCommand::Resume`"));
let _ = addr
.stop(true)
.wait()
.map(|_| println!("`actix_server::ServerCommand::Stop`"));
} }
// </signals> // </signals>

View File

@ -2,11 +2,12 @@
use actix_web::{web, App, HttpRequest, HttpServer, 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 { async fn index(_req: HttpRequest) -> impl Responder {
"Welcome!" "Welcome!"
} }
pub fn main() { #[actix_rt::main]
async fn main() -> std::io::Result<()> {
// load ssl keys // load ssl keys
// to create a self-signed temporary cert for testing: // 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'` // `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(); builder.set_certificate_chain_file("cert.pem").unwrap();
HttpServer::new(|| App::new().route("/", web::get().to(index))) HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind_ssl("127.0.0.1:8088", builder) .bind_openssl("127.0.0.1:8088", builder)?
.unwrap()
.run() .run()
.unwrap(); .await
} }
// </ssl> // </ssl>
// //

View File

@ -1,7 +1,8 @@
// <workers> // <workers>
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
pub fn main() { #[actix_rt::main]
async fn main() {
HttpServer::new(|| { HttpServer::new(|| {
App::new().route("/", web::get().to(|| HttpResponse::Ok())) App::new().route("/", web::get().to(|| HttpResponse::Ok()))
}) })