1
0
mirror of https://github.com/actix/examples synced 2025-06-26 09:17:41 +02:00

Restructure folders (#411)

This commit is contained in:
Daniel T. Rodrigues
2021-02-25 21:57:58 -03:00
committed by GitHub
parent 9db98162b2
commit c3407627d0
334 changed files with 127 additions and 120 deletions

View File

@ -1,10 +0,0 @@
[package]
name = "run-in-thread"
version = "2.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
edition = "2018"
description = "Run actix-web in separate thread"
[dependencies]
actix-web = "3"
env_logger = "0.8"

View File

@ -1,51 +0,0 @@
use std::sync::mpsc;
use std::{thread, time};
use actix_web::{dev::Server, middleware, rt, web, App, HttpRequest, HttpServer};
async fn index(req: HttpRequest) -> &'static str {
println!("REQ: {:?}", req);
"Hello world!"
}
fn run_app(tx: mpsc::Sender<Server>) -> std::io::Result<()> {
let mut sys = rt::System::new("test");
// srv is server controller type, `dev::Server`
let srv = HttpServer::new(|| {
App::new()
// enable logger
.wrap(middleware::Logger::default())
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index))
})
.bind("127.0.0.1:8080")?
.run();
// send server controller to main thread
let _ = tx.send(srv.clone());
// run future
sys.block_on(srv)
}
fn main() {
std::env::set_var("RUST_LOG", "actix_web=info,actix_server=trace");
env_logger::init();
let (tx, rx) = mpsc::channel();
println!("START SERVER");
thread::spawn(move || {
let _ = run_app(tx);
});
let srv = rx.recv().unwrap();
println!("WAITING 10 SECONDS");
thread::sleep(time::Duration::from_secs(10));
println!("STOPPING SERVER");
// init stop server and wait until server gracefully exit
rt::System::new("").block_on(srv.stop(true));
}