From d36c2f0f1f48bd760268c49785d9f2ed87d3a9ed Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 2 Feb 2022 02:09:06 +0000 Subject: [PATCH] clean up run-in-thread --- Cargo.lock | 2 +- Cargo.toml | 2 +- basics/run-in-thread/Cargo.toml | 10 --------- other/run-in-thread/Cargo.toml | 11 +++++++++ {basics => other}/run-in-thread/src/main.rs | 25 +++++++++++---------- 5 files changed, 26 insertions(+), 24 deletions(-) delete mode 100644 basics/run-in-thread/Cargo.toml create mode 100644 other/run-in-thread/Cargo.toml rename {basics => other}/run-in-thread/src/main.rs (64%) diff --git a/Cargo.lock b/Cargo.lock index f865e830..caadf4ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4883,7 +4883,7 @@ dependencies = [ name = "run-in-thread" version = "1.0.0" dependencies = [ - "actix-web 4.0.0-beta.21", + "actix-web 4.0.0-rc.1", "env_logger 0.9.0", "log", ] diff --git a/Cargo.toml b/Cargo.toml index 094e01b3..1c8bb569 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "basics/middleware-http-to-https", "basics/middleware", "basics/nested-routing", - "basics/run-in-thread", "basics/shutdown-server", "basics/state", "basics/static_index", @@ -35,6 +34,7 @@ members = [ "other/data_factory", "other/http-proxy", "other/protobuf", + "other/run-in-thread", "other/server-sent-events", "other/unix-socket", "security/awc_https", diff --git a/basics/run-in-thread/Cargo.toml b/basics/run-in-thread/Cargo.toml deleted file mode 100644 index 86b1750b..00000000 --- a/basics/run-in-thread/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "run-in-thread" -version = "1.0.0" -edition = "2021" -description = "Run actix-web in separate thread" - -[dependencies] -actix-web = "4.0.0-beta.21" -log = "0.4" -env_logger = "0.9.0" diff --git a/other/run-in-thread/Cargo.toml b/other/run-in-thread/Cargo.toml new file mode 100644 index 00000000..1ae3df46 --- /dev/null +++ b/other/run-in-thread/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "run-in-thread" +version = "1.0.0" +edition = "2021" +description = "Run Actix Web in separate thread" + +[dependencies] +actix-web = "4.0.0-rc.1" + +env_logger = "0.9" +log = "0.4" diff --git a/basics/run-in-thread/src/main.rs b/other/run-in-thread/src/main.rs similarity index 64% rename from basics/run-in-thread/src/main.rs rename to other/run-in-thread/src/main.rs index bd41ed7d..a00a7db7 100644 --- a/basics/run-in-thread/src/main.rs +++ b/other/run-in-thread/src/main.rs @@ -1,8 +1,6 @@ -use std::sync::mpsc; -use std::{thread, time}; +use std::{sync::mpsc, thread, time}; -use actix_web::dev::ServerHandle; -use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer}; +use actix_web::{dev::ServerHandle, middleware, rt, web, App, HttpRequest, HttpServer}; async fn index(req: HttpRequest) -> &'static str { log::info!("REQ: {:?}", req); @@ -10,6 +8,8 @@ async fn index(req: HttpRequest) -> &'static str { } async fn run_app(tx: mpsc::Sender) -> std::io::Result<()> { + log::info!("starting HTTP serer at http://localhost:8080"); + // srv is server controller type, `dev::Server` let server = HttpServer::new(|| { App::new() @@ -18,32 +18,33 @@ async fn run_app(tx: mpsc::Sender) -> std::io::Result<()> { .service(web::resource("/index.html").to(|| async { "Hello world!" })) .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? + .workers(2) .run(); // Send server handle back to the main thread let _ = tx.send(server.handle()); + server.await } fn main() { - std::env::set_var("RUST_LOG", "actix_web=info,actix_server=trace"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); let (tx, rx) = mpsc::channel(); - log::info!("START SERVER"); + log::info!("spawning thread for server"); thread::spawn(move || { - let future = run_app(tx); - rt::System::new().block_on(future) + let server_future = run_app(tx); + rt::System::new().block_on(server_future) }); let server_handle = rx.recv().unwrap(); - log::info!("WAITING 10 SECONDS"); + log::info!("waiting 10 seconds"); thread::sleep(time::Duration::from_secs(10)); - log::info!("STOPPING SERVER"); // Send a stop signal to the server, waiting for it to exit gracefully + log::info!("stopping server"); rt::System::new().block_on(server_handle.stop(true)); }