mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
clean up run-in-thread
This commit is contained in:
parent
ca703d9cec
commit
d36c2f0f1f
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -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",
|
||||
]
|
||||
|
@ -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",
|
||||
|
@ -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"
|
11
other/run-in-thread/Cargo.toml
Normal file
11
other/run-in-thread/Cargo.toml
Normal file
@ -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"
|
@ -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<ServerHandle>) -> 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<ServerHandle>) -> 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));
|
||||
}
|
Loading…
Reference in New Issue
Block a user