2019-12-25 04:44:33 +01:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::{thread, time};
|
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
use actix_web::dev::ServerHandle;
|
|
|
|
use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer};
|
2019-12-25 04:44:33 +01:00
|
|
|
|
|
|
|
async fn index(req: HttpRequest) -> &'static str {
|
2022-02-02 03:04:37 +01:00
|
|
|
log::info!("REQ: {:?}", req);
|
2019-12-25 04:44:33 +01:00
|
|
|
"Hello world!"
|
|
|
|
}
|
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
async fn run_app(tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
|
2019-12-25 04:44:33 +01:00
|
|
|
// srv is server controller type, `dev::Server`
|
2022-02-02 03:04:37 +01:00
|
|
|
let server = HttpServer::new(|| {
|
2019-12-25 04:44:33 +01:00
|
|
|
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")?
|
2019-12-25 17:48:33 +01:00
|
|
|
.run();
|
2019-12-25 04:44:33 +01:00
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
// Send server handle back to the main thread
|
|
|
|
let _ = tx.send(server.handle());
|
|
|
|
server.await
|
2019-12-25 04:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info,actix_server=trace");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
log::info!("START SERVER");
|
2019-12-25 04:44:33 +01:00
|
|
|
thread::spawn(move || {
|
2022-02-02 03:04:37 +01:00
|
|
|
let future = run_app(tx);
|
|
|
|
rt::System::new().block_on(future)
|
2019-12-25 04:44:33 +01:00
|
|
|
});
|
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
let server_handle = rx.recv().unwrap();
|
2019-12-25 04:44:33 +01:00
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
log::info!("WAITING 10 SECONDS");
|
2019-12-25 04:44:33 +01:00
|
|
|
thread::sleep(time::Duration::from_secs(10));
|
|
|
|
|
2022-02-02 03:04:37 +01:00
|
|
|
log::info!("STOPPING SERVER");
|
|
|
|
// Send a stop signal to the server, waiting for it to exit gracefully
|
|
|
|
rt::System::new().block_on(server_handle.stop(true));
|
2019-12-25 04:44:33 +01:00
|
|
|
}
|