mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "shutdown-server"
|
||||
version = "2.0.0"
|
||||
authors = ["Rob Ede <robjtede@icloud.com>"]
|
||||
edition = "2018"
|
||||
description = "Send a request to the server to shut it down"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
env_logger = "0.8"
|
||||
futures = "0.3"
|
||||
tokio = { version = "0.2", features = ["signal"] }
|
@ -1,28 +0,0 @@
|
||||
# shutdown-server
|
||||
|
||||
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)
|
||||
- actix-server natively supports SIGINT
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
### Running The Server
|
||||
|
||||
```bash
|
||||
cargo run --bin shutdown-server
|
||||
|
||||
# Starting 8 workers
|
||||
# Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080
|
||||
```
|
||||
|
||||
### Available Routes
|
||||
|
||||
- [GET /hello](http://localhost:8080/hello)
|
||||
- Regular hello world route
|
||||
- [POST /stop](http://localhost:8080/stop)
|
||||
- Calling this will shutdown the server and exit
|
@ -1,52 +0,0 @@
|
||||
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
|
||||
use futures::executor;
|
||||
use std::{sync::mpsc, thread};
|
||||
|
||||
#[get("/hello")]
|
||||
async fn hello() -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[post("/stop")]
|
||||
async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse {
|
||||
// make request that sends message through the Sender
|
||||
stopper.send(()).unwrap();
|
||||
|
||||
HttpResponse::NoContent().finish()
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_server=debug,actix_web=debug");
|
||||
env_logger::init();
|
||||
|
||||
// create a channel
|
||||
let (tx, rx) = mpsc::channel::<()>();
|
||||
|
||||
let bind = "127.0.0.1:8080";
|
||||
|
||||
// start server as normal but don't .await after .run() yet
|
||||
let server = HttpServer::new(move || {
|
||||
// give the server a Sender in .data
|
||||
App::new()
|
||||
.data(tx.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(hello)
|
||||
.service(stop)
|
||||
})
|
||||
.bind(&bind)?
|
||||
.run();
|
||||
|
||||
// clone the Server handle
|
||||
let srv = server.clone();
|
||||
thread::spawn(move || {
|
||||
// wait for shutdown signal
|
||||
rx.recv().unwrap();
|
||||
|
||||
// stop server gracefully
|
||||
executor::block_on(srv.stop(true))
|
||||
});
|
||||
|
||||
// run server
|
||||
server.await
|
||||
}
|
Reference in New Issue
Block a user