Add graceful shutdown handler for SIGINT and SIGTERM.

This commit is contained in:
Jonathan Bernard
2025-07-26 20:31:43 -05:00
parent 0e5cd036ef
commit 9d1e8da676
+6 -1
View File
@@ -1,4 +1,4 @@
import std/[json, nre, sequtils, strutils] import std/[json, nre, posix, sequtils, strutils]
import cliutils, docopt, mummy, namespaced_logging import cliutils, docopt, mummy, namespaced_logging
import webby/httpheaders import webby/httpheaders
@@ -91,6 +91,11 @@ when isMainModule:
elif args["--debug"]: logSvc.setRootThreshold(lvlDebug) elif args["--debug"]: logSvc.setRootThreshold(lvlDebug)
let server = newServer(makeHandler(cfg.mappings, logSvc)) 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 $#" % logger.info("short_url v$# listening for requests on $#" %
[ VERSION, $cfg.port ]) [ VERSION, $cfg.port ])
server.serve(Port(cfg.port), address = "0.0.0.0") server.serve(Port(cfg.port), address = "0.0.0.0")