mirror of
https://github.com/actix/examples
synced 2024-11-30 17:14:35 +01:00
Update basics/run-in-thread to v4 (#485)
This commit is contained in:
parent
ffb20a1b20
commit
ca703d9cec
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -4883,8 +4883,9 @@ dependencies = [
|
|||||||
name = "run-in-thread"
|
name = "run-in-thread"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web 3.3.3",
|
"actix-web 4.0.0-beta.21",
|
||||||
"env_logger 0.8.4",
|
"env_logger 0.9.0",
|
||||||
|
"log",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -5,5 +5,6 @@ edition = "2021"
|
|||||||
description = "Run actix-web in separate thread"
|
description = "Run actix-web in separate thread"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "4.0.0-beta.21"
|
||||||
env_logger = "0.8"
|
log = "0.4"
|
||||||
|
env_logger = "0.9.0"
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
use actix_web::{dev::Server, middleware, rt, web, App, HttpRequest, HttpServer};
|
use actix_web::dev::ServerHandle;
|
||||||
|
use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer};
|
||||||
|
|
||||||
async fn index(req: HttpRequest) -> &'static str {
|
async fn index(req: HttpRequest) -> &'static str {
|
||||||
println!("REQ: {:?}", req);
|
log::info!("REQ: {:?}", req);
|
||||||
"Hello world!"
|
"Hello world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_app(tx: mpsc::Sender<Server>) -> std::io::Result<()> {
|
async fn run_app(tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
|
||||||
let mut sys = rt::System::new("test");
|
|
||||||
|
|
||||||
// srv is server controller type, `dev::Server`
|
// srv is server controller type, `dev::Server`
|
||||||
let srv = HttpServer::new(|| {
|
let server = HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// enable logger
|
// enable logger
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
@ -22,11 +21,9 @@ fn run_app(tx: mpsc::Sender<Server>) -> std::io::Result<()> {
|
|||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
// send server controller to main thread
|
// Send server handle back to the main thread
|
||||||
let _ = tx.send(srv.clone());
|
let _ = tx.send(server.handle());
|
||||||
|
server.await
|
||||||
// run future
|
|
||||||
sys.block_on(srv)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -35,17 +32,18 @@ fn main() {
|
|||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
println!("START SERVER");
|
log::info!("START SERVER");
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let _ = run_app(tx);
|
let future = run_app(tx);
|
||||||
|
rt::System::new().block_on(future)
|
||||||
});
|
});
|
||||||
|
|
||||||
let srv = rx.recv().unwrap();
|
let server_handle = rx.recv().unwrap();
|
||||||
|
|
||||||
println!("WAITING 10 SECONDS");
|
log::info!("WAITING 10 SECONDS");
|
||||||
thread::sleep(time::Duration::from_secs(10));
|
thread::sleep(time::Duration::from_secs(10));
|
||||||
|
|
||||||
println!("STOPPING SERVER");
|
log::info!("STOPPING SERVER");
|
||||||
// init stop server and wait until server gracefully exit
|
// Send a stop signal to the server, waiting for it to exit gracefully
|
||||||
rt::System::new("").block_on(srv.stop(true));
|
rt::System::new().block_on(server_handle.stop(true));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user