1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

update remaining examples to v4

This commit is contained in:
Rob Ede
2022-02-26 05:22:21 +00:00
parent 447edcc9d7
commit b8431bef6f
6 changed files with 67 additions and 46 deletions

View File

@ -5,6 +5,7 @@ workspace = "../"
edition = "2018"
[dependencies]
actix-web = { version = "3", features = ["openssl"] }
actix-web = { version = "4", features = ["openssl"] }
futures = "0.3"
openssl = "0.10"
tokio = { version = "1.16.1", features = ["full"] }

View File

@ -1,19 +1,37 @@
use actix_web::{
body::MessageBody,
dev::{ServiceFactory, ServiceRequest, ServiceResponse},
App, Error,
};
#[allow(dead_code)]
fn app() -> App<
impl ServiceFactory<
ServiceRequest,
Response = ServiceResponse<impl MessageBody>,
Config = (),
InitError = (),
Error = Error,
>,
> {
App::new()
}
// <keep-alive>
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web::{http::KeepAlive, HttpServer};
use std::time::Duration;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let one = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
.keep_alive(75); // <- Set keep-alive to 75 seconds
// Set keep-alive to 75 seconds
let _one = HttpServer::new(app).keep_alive(Duration::from_secs(75));
// let _two = HttpServer::new(|| {
// App::new().route("/", web::get().to(|| HttpResponse::Ok()))
// })
// .keep_alive(); // <- Use `SO_KEEPALIVE` socket option.
// Use OS's keep-alive (usually quite long)
let _two = HttpServer::new(app).keep_alive(KeepAlive::Os);
let _three = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
.keep_alive(None); // <- Disable keep-alive
// Disable keep-alive
let _three = HttpServer::new(app).keep_alive(None);
one.bind(("127.0.0.1", 8080))?.run().await
Ok(())
}
// </keep-alive>

View File

@ -1,31 +1,34 @@
// <signals>
use actix_web::{rt::System, web, App, HttpResponse, HttpServer};
use std::sync::mpsc;
use std::thread;
use actix_web::{web, App, HttpResponse, HttpServer};
use std::io;
#[actix_web::main]
async fn main() {
let (tx, rx) = mpsc::channel();
#[tokio::main]
async fn main() -> io::Result<()> {
let srv = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
.bind(("127.0.0.1", 8080))?
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.run();
thread::spawn(move || {
let sys = System::new("http-server");
// obtain handle to server
let srv_handle = srv.handle();
let srv = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
.bind(("127.0.0.1", 8080))?
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.run();
let _ = tx.send(srv);
sys.run()
});
let srv = rx.recv().unwrap();
// spawn server as Tokio task to start processing connections
tokio::spawn(srv);
// pause accepting new connections
srv.pause().await;
srv_handle.pause().await;
// resume accepting new connections
srv.resume().await;
// stop server
srv.stop(true).await;
srv_handle.resume().await;
// stop server gracefully
srv_handle.stop(true).await;
Ok(())
}
// </signals>
#[allow(dead_code)]
fn run_main() {
let _ = main();
}