From da72aef69c2e05b433ed1d34b9682e3472677148 Mon Sep 17 00:00:00 2001 From: dowwie Date: Fri, 31 Jan 2020 07:10:56 -0500 Subject: [PATCH 1/2] enhanced, renamed shutdown example and modified async_pg readme --- Cargo.toml | 2 +- async_pg/Cargo.toml | 2 ++ async_pg/README.md | 2 +- async_pg/src/main.rs | 15 ++++++++++++++- .../Cargo.toml | 5 +++-- .../README.md | 13 ++++++++----- .../src/main.rs | 16 +++++++++++++--- 7 files changed, 42 insertions(+), 13 deletions(-) rename {self-shutdown-route => shutdown-server}/Cargo.toml (75%) rename {self-shutdown-route => shutdown-server}/README.md (51%) rename {self-shutdown-route => shutdown-server}/src/main.rs (79%) diff --git a/Cargo.toml b/Cargo.toml index f6b62232..d9217891 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ members = [ "redis-session", "run-in-thread", "rustls", - "self-shutdown-route", + "shutdown-server", "server-sent-events", "simple-auth-server", "state", diff --git a/async_pg/Cargo.toml b/async_pg/Cargo.toml index 20706a55..5ac11a7c 100644 --- a/async_pg/Cargo.toml +++ b/async_pg/Cargo.toml @@ -3,6 +3,7 @@ name = "async_pg" version = "0.1.0" authors = ["dowwie "] edition = "2018" +workspace = ".." [dependencies] actix-rt = "1.0.0" @@ -12,6 +13,7 @@ deadpool-postgres = "0.5.0" derive_more = "0.99.2" dotenv = "0.15.0" serde = { version = "1.0.104", features = ["derive"] } +tokio = { version = "0.2.11", features = ["signal"] } tokio-pg-mapper = "0.1.4" tokio-pg-mapper-derive = "0.1.4" tokio-postgres = "0.5.1" diff --git a/async_pg/README.md b/async_pg/README.md index 5552fb6e..77ce5e6f 100644 --- a/async_pg/README.md +++ b/async_pg/README.md @@ -43,7 +43,7 @@ ```ini SERVER_ADDR=127.0.0.1:8080 PG.USER=test_user - PG.PASSWD=testing + PG.PASSWORD=testing PG.HOST=127.0.0.1 PG.PORT=5432 PG.DBNAME=testing_db diff --git a/async_pg/src/main.rs b/async_pg/src/main.rs index 09bfb665..c830b200 100644 --- a/async_pg/src/main.rs +++ b/async_pg/src/main.rs @@ -49,6 +49,7 @@ mod errors { fn error_response(&self) -> HttpResponse { match *self { MyError::NotFound => HttpResponse::NotFound().finish(), + MyError::PoolError(ref err) => HttpResponse::InternalServerError().body(err.to_string()), _ => HttpResponse::InternalServerError().finish(), } } @@ -97,7 +98,7 @@ mod handlers { let client: Client = db_pool.get().await.map_err(|err| MyError::PoolError(err))?; - + let new_user = db::add_user(&client, user_info).await?; Ok(HttpResponse::Ok().json(new_user)) @@ -108,6 +109,8 @@ use actix_web::{web, App, HttpServer}; use dotenv::dotenv; use handlers::add_user; use tokio_postgres::NoTls; +use tokio::signal::unix::{signal, SignalKind}; + #[actix_rt::main] async fn main() -> std::io::Result<()> { @@ -125,5 +128,15 @@ async fn main() -> std::io::Result<()> { .run(); println!("Server running at http://{}/", config.server_addr); + let srv = server.clone(); + let mut stream = signal(SignalKind::interrupt())?; + actix_rt::spawn(async move { + loop { + stream.recv().await; + println!("\nSIGINT Received. Stopping server.\n"); + srv.stop(true).await; + } + }); + server.await } diff --git a/self-shutdown-route/Cargo.toml b/shutdown-server/Cargo.toml similarity index 75% rename from self-shutdown-route/Cargo.toml rename to shutdown-server/Cargo.toml index 4b3a1210..4eb27415 100644 --- a/self-shutdown-route/Cargo.toml +++ b/shutdown-server/Cargo.toml @@ -1,12 +1,13 @@ [package] -name = "self-shutdown-route" +name = "shutdown-server" version = "2.0.0" authors = ["Rob Ede "] edition = "2018" description = "Send a request to the server to shut it down" [dependencies] -actix-web = "2.0.0" actix-rt = "1.0.0" +actix-web = "2.0.0" env_logger = "0.7" futures = "0.3" +tokio = { version = "0.2.11", features = ["signal"] } diff --git a/self-shutdown-route/README.md b/shutdown-server/README.md similarity index 51% rename from self-shutdown-route/README.md rename to shutdown-server/README.md index 709069e2..cf061407 100644 --- a/self-shutdown-route/README.md +++ b/shutdown-server/README.md @@ -1,16 +1,19 @@ -# self-shutdown-route +# shutdown-server -> Demonstrates how to shutdown the web server using a route hosted by the server itself using channels. +Demonstrates how to shutdown the web server in a couple of ways: + +1. remotely, via http request + - Created in response to actix/actix-web#1315 + +2. sending a SIGINT signal to the server (control-c) -This technique can be easily modified to support shutting down the server using other kinds of external events. -Created in response to actix/actix-web#1315. ## Usage ### Running The Server ```bash -cargo run --bin self-shutdown-route +cargo run --bin shutdown-server # Starting 8 workers # Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080 diff --git a/self-shutdown-route/src/main.rs b/shutdown-server/src/main.rs similarity index 79% rename from self-shutdown-route/src/main.rs rename to shutdown-server/src/main.rs index a55feb9d..962b5c20 100644 --- a/self-shutdown-route/src/main.rs +++ b/shutdown-server/src/main.rs @@ -1,8 +1,8 @@ use std::{sync::mpsc, thread}; - -use futures::executor; - use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer}; +use futures::executor; +use tokio::signal::unix::{signal, SignalKind}; + #[get("/hello")] async fn hello() -> &'static str { @@ -24,6 +24,7 @@ async fn main() -> std::io::Result<()> { // create a channel let (tx, rx) = mpsc::channel::<()>(); + let stopper = tx.clone(); let bind = "127.0.0.1:8080"; @@ -51,6 +52,15 @@ async fn main() -> std::io::Result<()> { executor::block_on(srv.stop(true)) }); + let mut stream = signal(SignalKind::interrupt())?; + actix_rt::spawn(async move { + loop { + stream.recv().await; + println!("\n*** SIGINT received. Stopping server, gracefully. ***\n"); + stopper.send(()).unwrap(); + } + }); + // run server server.await } From 04cebf6899cb62c1bd207361ba166bd05705ec61 Mon Sep 17 00:00:00 2001 From: dowwie Date: Fri, 31 Jan 2020 07:21:39 -0500 Subject: [PATCH 2/2] resolved formatting --- async_pg/src/main.rs | 15 ++++++++------- basics/src/main.rs | 12 +++++------- middleware/src/main.rs | 10 ++++------ shutdown-server/src/main.rs | 5 ++--- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/async_pg/src/main.rs b/async_pg/src/main.rs index c830b200..80d154aa 100644 --- a/async_pg/src/main.rs +++ b/async_pg/src/main.rs @@ -1,6 +1,6 @@ mod config { - use serde::Deserialize; pub use ::config::ConfigError; + use serde::Deserialize; #[derive(Deserialize)] pub struct Config { pub server_addr: String, @@ -49,7 +49,9 @@ mod errors { fn error_response(&self) -> HttpResponse { match *self { MyError::NotFound => HttpResponse::NotFound().finish(), - MyError::PoolError(ref err) => HttpResponse::InternalServerError().body(err.to_string()), + MyError::PoolError(ref err) => { + HttpResponse::InternalServerError().body(err.to_string()) + } _ => HttpResponse::InternalServerError().finish(), } } @@ -98,7 +100,7 @@ mod handlers { let client: Client = db_pool.get().await.map_err(|err| MyError::PoolError(err))?; - + let new_user = db::add_user(&client, user_info).await?; Ok(HttpResponse::Ok().json(new_user)) @@ -108,9 +110,8 @@ mod handlers { use actix_web::{web, App, HttpServer}; use dotenv::dotenv; use handlers::add_user; -use tokio_postgres::NoTls; use tokio::signal::unix::{signal, SignalKind}; - +use tokio_postgres::NoTls; #[actix_rt::main] async fn main() -> std::io::Result<()> { @@ -129,14 +130,14 @@ async fn main() -> std::io::Result<()> { println!("Server running at http://{}/", config.server_addr); let srv = server.clone(); - let mut stream = signal(SignalKind::interrupt())?; + let mut stream = signal(SignalKind::interrupt())?; actix_rt::spawn(async move { loop { stream.recv().await; println!("\nSIGINT Received. Stopping server.\n"); srv.stop(true).await; } - }); + }); server.await } diff --git a/basics/src/main.rs b/basics/src/main.rs index efd57f24..2fee54f5 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -92,13 +92,11 @@ async fn main() -> io::Result<()> { _ => HttpResponse::NotFound(), }), ) - .service(web::resource("/error").to(|| { - async { - error::InternalError::new( - io::Error::new(io::ErrorKind::Other, "test"), - StatusCode::INTERNAL_SERVER_ERROR, - ) - } + .service(web::resource("/error").to(|| async { + error::InternalError::new( + io::Error::new(io::ErrorKind::Other, "test"), + StatusCode::INTERNAL_SERVER_ERROR, + ) })) // static files .service(fs::Files::new("/static", "static").show_files_listing()) diff --git a/middleware/src/main.rs b/middleware/src/main.rs index 1598a2a0..c2237beb 100644 --- a/middleware/src/main.rs +++ b/middleware/src/main.rs @@ -30,13 +30,11 @@ async fn main() -> std::io::Result<()> { res }) }) - .service(web::resource("/login").to(|| { - async { - "You are on /login. Go to src/redirect.rs to change this behavior." - } + .service(web::resource("/login").to(|| async { + "You are on /login. Go to src/redirect.rs to change this behavior." })) - .service(web::resource("/").to(|| { - async { "Hello, middleware! Check the console where the server is run." } + .service(web::resource("/").to(|| async { + "Hello, middleware! Check the console where the server is run." })) }) .bind("127.0.0.1:8080")? diff --git a/shutdown-server/src/main.rs b/shutdown-server/src/main.rs index 962b5c20..72cb0f7e 100644 --- a/shutdown-server/src/main.rs +++ b/shutdown-server/src/main.rs @@ -1,9 +1,8 @@ -use std::{sync::mpsc, thread}; use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer}; use futures::executor; +use std::{sync::mpsc, thread}; use tokio::signal::unix::{signal, SignalKind}; - #[get("/hello")] async fn hello() -> &'static str { "Hello world!" @@ -59,7 +58,7 @@ async fn main() -> std::io::Result<()> { println!("\n*** SIGINT received. Stopping server, gracefully. ***\n"); stopper.send(()).unwrap(); } - }); + }); // run server server.await