mirror of
https://github.com/actix/examples
synced 2025-02-17 15:23:31 +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"
|
name = "run-in-thread"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web 4.0.0-beta.21",
|
"actix-web 4.0.0-rc.1",
|
||||||
"env_logger 0.9.0",
|
"env_logger 0.9.0",
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
@ -9,7 +9,6 @@ members = [
|
|||||||
"basics/middleware-http-to-https",
|
"basics/middleware-http-to-https",
|
||||||
"basics/middleware",
|
"basics/middleware",
|
||||||
"basics/nested-routing",
|
"basics/nested-routing",
|
||||||
"basics/run-in-thread",
|
|
||||||
"basics/shutdown-server",
|
"basics/shutdown-server",
|
||||||
"basics/state",
|
"basics/state",
|
||||||
"basics/static_index",
|
"basics/static_index",
|
||||||
@ -35,6 +34,7 @@ members = [
|
|||||||
"other/data_factory",
|
"other/data_factory",
|
||||||
"other/http-proxy",
|
"other/http-proxy",
|
||||||
"other/protobuf",
|
"other/protobuf",
|
||||||
|
"other/run-in-thread",
|
||||||
"other/server-sent-events",
|
"other/server-sent-events",
|
||||||
"other/unix-socket",
|
"other/unix-socket",
|
||||||
"security/awc_https",
|
"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::{sync::mpsc, thread, time};
|
||||||
use std::{thread, time};
|
|
||||||
|
|
||||||
use actix_web::dev::ServerHandle;
|
use actix_web::{dev::ServerHandle, middleware, rt, web, App, HttpRequest, HttpServer};
|
||||||
use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer};
|
|
||||||
|
|
||||||
async fn index(req: HttpRequest) -> &'static str {
|
async fn index(req: HttpRequest) -> &'static str {
|
||||||
log::info!("REQ: {:?}", req);
|
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<()> {
|
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`
|
// srv is server controller type, `dev::Server`
|
||||||
let server = HttpServer::new(|| {
|
let server = HttpServer::new(|| {
|
||||||
App::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("/index.html").to(|| async { "Hello world!" }))
|
||||||
.service(web::resource("/").to(index))
|
.service(web::resource("/").to(index))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind(("127.0.0.1", 8080))?
|
||||||
|
.workers(2)
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
// Send server handle back to the main thread
|
// Send server handle back to the main thread
|
||||||
let _ = tx.send(server.handle());
|
let _ = tx.send(server.handle());
|
||||||
|
|
||||||
server.await
|
server.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info,actix_server=trace");
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
env_logger::init();
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
log::info!("START SERVER");
|
log::info!("spawning thread for server");
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let future = run_app(tx);
|
let server_future = run_app(tx);
|
||||||
rt::System::new().block_on(future)
|
rt::System::new().block_on(server_future)
|
||||||
});
|
});
|
||||||
|
|
||||||
let server_handle = rx.recv().unwrap();
|
let server_handle = rx.recv().unwrap();
|
||||||
|
|
||||||
log::info!("WAITING 10 SECONDS");
|
log::info!("waiting 10 seconds");
|
||||||
thread::sleep(time::Duration::from_secs(10));
|
thread::sleep(time::Duration::from_secs(10));
|
||||||
|
|
||||||
log::info!("STOPPING SERVER");
|
|
||||||
// Send a stop signal to the server, waiting for it to exit gracefully
|
// 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));
|
rt::System::new().block_on(server_handle.stop(true));
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user