From 8e972851f377c0ceb4ac5ac031042c6525c6bebc Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 26 Jul 2025 20:31:43 -0500 Subject: [PATCH] Add graceful shutdown handler for SIGINT and SIGTERM. --- src/short_url.nim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/short_url.nim b/src/short_url.nim index 4b611ed..4509e02 100644 --- a/src/short_url.nim +++ b/src/short_url.nim @@ -1,4 +1,4 @@ -import std/[json, nre, sequtils, strutils] +import std/[json, nre, posix, sequtils, strutils] import cliutils, docopt, mummy, namespaced_logging import webby/httpheaders @@ -91,6 +91,11 @@ when isMainModule: elif args["--debug"]: logSvc.setRootThreshold(lvlDebug) let server = newServer(makeHandler(cfg.mappings, logSvc)) + + onSignal(SIGINT, SIGTERM): + logger.info("short_url shutting down") + server.close() + logger.info("short_url v$# listening for requests on $#" % [ VERSION, $cfg.port ]) server.serve(Port(cfg.port), address = "0.0.0.0")