mirror of
https://github.com/actix/examples
synced 2025-02-02 09:39:03 +01:00
Merge pull request #246 from Dowwie/master
enhanced, renamed shutdown example and modified async_pg (readme, plus shutdown)
This commit is contained in:
commit
741a530963
@ -27,7 +27,7 @@ members = [
|
|||||||
"redis-session",
|
"redis-session",
|
||||||
"run-in-thread",
|
"run-in-thread",
|
||||||
"rustls",
|
"rustls",
|
||||||
"self-shutdown-route",
|
"shutdown-server",
|
||||||
"server-sent-events",
|
"server-sent-events",
|
||||||
"simple-auth-server",
|
"simple-auth-server",
|
||||||
"state",
|
"state",
|
||||||
|
@ -3,6 +3,7 @@ name = "async_pg"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
@ -12,6 +13,7 @@ deadpool-postgres = "0.5.0"
|
|||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
serde = { version = "1.0.104", features = ["derive"] }
|
serde = { version = "1.0.104", features = ["derive"] }
|
||||||
|
tokio = { version = "0.2.11", features = ["signal"] }
|
||||||
tokio-pg-mapper = "0.1.4"
|
tokio-pg-mapper = "0.1.4"
|
||||||
tokio-pg-mapper-derive = "0.1.4"
|
tokio-pg-mapper-derive = "0.1.4"
|
||||||
tokio-postgres = "0.5.1"
|
tokio-postgres = "0.5.1"
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
```ini
|
```ini
|
||||||
SERVER_ADDR=127.0.0.1:8080
|
SERVER_ADDR=127.0.0.1:8080
|
||||||
PG.USER=test_user
|
PG.USER=test_user
|
||||||
PG.PASSWD=testing
|
PG.PASSWORD=testing
|
||||||
PG.HOST=127.0.0.1
|
PG.HOST=127.0.0.1
|
||||||
PG.PORT=5432
|
PG.PORT=5432
|
||||||
PG.DBNAME=testing_db
|
PG.DBNAME=testing_db
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
mod config {
|
mod config {
|
||||||
use serde::Deserialize;
|
|
||||||
pub use ::config::ConfigError;
|
pub use ::config::ConfigError;
|
||||||
|
use serde::Deserialize;
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub server_addr: String,
|
pub server_addr: String,
|
||||||
@ -49,6 +49,9 @@ mod errors {
|
|||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
match *self {
|
match *self {
|
||||||
MyError::NotFound => HttpResponse::NotFound().finish(),
|
MyError::NotFound => HttpResponse::NotFound().finish(),
|
||||||
|
MyError::PoolError(ref err) => {
|
||||||
|
HttpResponse::InternalServerError().body(err.to_string())
|
||||||
|
}
|
||||||
_ => HttpResponse::InternalServerError().finish(),
|
_ => HttpResponse::InternalServerError().finish(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,6 +110,7 @@ mod handlers {
|
|||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use handlers::add_user;
|
use handlers::add_user;
|
||||||
|
use tokio::signal::unix::{signal, SignalKind};
|
||||||
use tokio_postgres::NoTls;
|
use tokio_postgres::NoTls;
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
@ -125,5 +129,15 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.run();
|
.run();
|
||||||
println!("Server running at http://{}/", config.server_addr);
|
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
|
server.await
|
||||||
}
|
}
|
||||||
|
@ -92,13 +92,11 @@ async fn main() -> io::Result<()> {
|
|||||||
_ => HttpResponse::NotFound(),
|
_ => HttpResponse::NotFound(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.service(web::resource("/error").to(|| {
|
.service(web::resource("/error").to(|| async {
|
||||||
async {
|
|
||||||
error::InternalError::new(
|
error::InternalError::new(
|
||||||
io::Error::new(io::ErrorKind::Other, "test"),
|
io::Error::new(io::ErrorKind::Other, "test"),
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
// static files
|
// static files
|
||||||
.service(fs::Files::new("/static", "static").show_files_listing())
|
.service(fs::Files::new("/static", "static").show_files_listing())
|
||||||
|
@ -30,13 +30,11 @@ async fn main() -> std::io::Result<()> {
|
|||||||
res
|
res
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.service(web::resource("/login").to(|| {
|
.service(web::resource("/login").to(|| async {
|
||||||
async {
|
|
||||||
"You are on /login. Go to src/redirect.rs to change this behavior."
|
"You are on /login. Go to src/redirect.rs to change this behavior."
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
.service(web::resource("/").to(|| {
|
.service(web::resource("/").to(|| async {
|
||||||
async { "Hello, middleware! Check the console where the server is run." }
|
"Hello, middleware! Check the console where the server is run."
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "self-shutdown-route"
|
name = "shutdown-server"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
authors = ["Rob Ede <robjtede@icloud.com>"]
|
authors = ["Rob Ede <robjtede@icloud.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Send a request to the server to shut it down"
|
description = "Send a request to the server to shut it down"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "2.0.0"
|
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
|
actix-web = "2.0.0"
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
tokio = { version = "0.2.11", features = ["signal"] }
|
@ -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
|
## Usage
|
||||||
|
|
||||||
### Running The Server
|
### Running The Server
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo run --bin self-shutdown-route
|
cargo run --bin shutdown-server
|
||||||
|
|
||||||
# Starting 8 workers
|
# Starting 8 workers
|
||||||
# Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080
|
# Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080
|
@ -1,8 +1,7 @@
|
|||||||
use std::{sync::mpsc, thread};
|
|
||||||
|
|
||||||
use futures::executor;
|
|
||||||
|
|
||||||
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
|
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")]
|
#[get("/hello")]
|
||||||
async fn hello() -> &'static str {
|
async fn hello() -> &'static str {
|
||||||
@ -24,6 +23,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
// create a channel
|
// create a channel
|
||||||
let (tx, rx) = mpsc::channel::<()>();
|
let (tx, rx) = mpsc::channel::<()>();
|
||||||
|
let stopper = tx.clone();
|
||||||
|
|
||||||
let bind = "127.0.0.1:8080";
|
let bind = "127.0.0.1:8080";
|
||||||
|
|
||||||
@ -51,6 +51,15 @@ async fn main() -> std::io::Result<()> {
|
|||||||
executor::block_on(srv.stop(true))
|
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
|
// run server
|
||||||
server.await
|
server.await
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user