From cd832b6b14c279a689bcd95bd1ac6e1046c2f602 Mon Sep 17 00:00:00 2001 From: Myrotvorets Date: Mon, 25 Sep 2023 03:04:18 +0300 Subject: [PATCH] Set signal handlers to stop the server --- src/index.mts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/index.mts b/src/index.mts index d4a8d87..713132a 100644 --- a/src/index.mts +++ b/src/index.mts @@ -1,7 +1,8 @@ import { readFile } from 'node:fs/promises'; import { type Server as HttpServer, type RequestListener, createServer as createHttpServer } from 'node:http'; import { type Server as HttpsServer, type ServerOptions, createServer as createHttpsServer } from 'node:https'; -import { type SecureVersion } from 'node:tls'; +import type { SecureVersion } from 'node:tls'; +import process from 'node:process'; import { bool, cleanEnv, str } from 'envalid'; export interface ServerEnvironment { @@ -47,6 +48,7 @@ function makeEnv(): ServerEnvironment { export async function createServer(requestListener?: RequestListener): Promise { const env = makeEnv(); const isHttps = env.HTTPS; + let server: HttpServer | HttpsServer; if (isHttps) { const options: ServerOptions = { @@ -82,8 +84,16 @@ export async function createServer(requestListener?: RequestListener): Promise server.close(() => process.exit(0)); + process.on('SIGTERM', finish); + process.on('SIGINT', finish); + process.on('SIGQUIT', finish); + process.on('SIGUSR2', finish); + + return server; }