about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-04-15 07:42:39 +0300
committerVika <vika@fireburn.ru>2022-04-15 07:42:39 +0300
commit0d58e14876ed8e4b2b4fc10b3ef67cd703dee1a1 (patch)
treed94862d6b4e8ece9739004060f19f6facda7b6f7 /src
parent54914782c7632e041919746e80d3f802f6601a63 (diff)
downloadkittybox-0d58e14876ed8e4b2b4fc10b3ef67cd703dee1a1.tar.zst
feat: graceful shutdown
Now Kittybox can gracefully shutdown on SIGTERM. Nice!

TODO: consider shutting down on multiple signals
Diffstat (limited to 'src')
-rw-r--r--src/main.rs24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 866fcf3..8e610e1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,6 @@
 use log::{debug, error, info};
 use std::{convert::Infallible, env, time::Duration};
-use http_types::Url;
+use url::Url;
 use hyper::client::{HttpConnector,connect::dns::GaiResolver};
 use hyper_rustls::HttpsConnector;
 use warp::{Filter, host::Authority};
@@ -212,7 +212,7 @@ async fn main() {
         tcp_listener.set_nonblocking(true).unwrap();
 
         info!("Listening on {}", tcp_listener.local_addr().unwrap());
-        let server: hyper::server::Server<_, _> = hyper::server::Server::from_tcp(tcp_listener)
+        let server = hyper::server::Server::from_tcp(tcp_listener)
             .unwrap()
             .tcp_keepalive(Some(Duration::from_secs(30 * 60)))
             .serve(hyper::service::make_service_fn(move |_| {
@@ -220,8 +220,24 @@ async fn main() {
                 async move {
                     Ok::<_, Infallible>(service)
                 }
-            }));
-            
+            }))
+            .with_graceful_shutdown(async move {
+                // Defer to C-c handler whenever we're not on Unix
+                // TODO consider using a diverging future here
+                #[cfg(not(unix))]
+                return tokio::signal::ctrl_c().await.unwrap();
+                #[cfg(unix)]
+                {
+                    use tokio::signal::unix::{signal, SignalKind};
+
+                    signal(SignalKind::terminate())
+                        .unwrap()
+                        .recv()
+                        .await
+                        .unwrap()
+                }
+            });
+
         if let Err(err) = server.await {
             error!("Error serving requests: {}", err);
             std::process::exit(1);