From d57cc27e19e68c13ac08af223c3721a9c45fafd1 Mon Sep 17 00:00:00 2001 From: "Max P." Date: Sun, 11 May 2025 12:23:38 +0200 Subject: [PATCH] feat(server): add graceful shutdown handling - Introduce signal listeners for SIGINT and SIGTERM to handle shutdown - Use AbortController to terminate the server gracefully - Improve server reliability and resource cleanup during termination --- src/main.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.ts b/src/main.ts index 1d839b0..1bace33 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { Env } from './env.ts'; import { ltProxyAuth } from './ltProxyAuth.ts'; import { ltProxyHandler } from './ltProxyHandler.ts'; +const ac = new AbortController(); const httpKernel = new HttpKernel(); httpKernel.route({ @@ -11,9 +12,19 @@ httpKernel.route({ }).middleware(ltProxyAuth).handle(ltProxyHandler); Deno.serve({ + signal: ac.signal, port: Env.proxyPort, hostname: Env.proxyHost, onListen: ({ hostname, port }) => { console.info(`lt-auth-proxy listening on ${hostname}:${port}`); }, }, async (req) => await httpKernel.handle(req)); + +const shutdown = () => { + console.info('Shutting down the server...'); + ac.abort(); + console.info('Server shut down successfully.'); +}; + +Deno.addSignalListener('SIGINT', shutdown); +Deno.addSignalListener('SIGTERM', shutdown);