1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

enhanced, renamed shutdown example and modified async_pg readme

This commit is contained in:
dowwie
2020-01-31 07:10:56 -05:00
parent 78a753a06a
commit da72aef69c
7 changed files with 42 additions and 13 deletions

View File

@ -3,6 +3,7 @@ name = "async_pg"
version = "0.1.0"
authors = ["dowwie <dkcdkg@gmail.com>"]
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"

View File

@ -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

View File

@ -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
}