diff --git a/.vscode/settings.json b/.vscode/settings.json index cb3be07..8d617e7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ ], "cSpell.words": [ "fastify", - "openapi" + "openapi", + "prexit" ] } diff --git a/package-lock.json b/package-lock.json index 5bbb3c6..af95475 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "fastify": "4.28.1" + "fastify": "4.28.1", + "prexit": "2.3.0" }, "devDependencies": { "@2bad/tsconfig": "3.0.1", @@ -6421,6 +6422,15 @@ "node": ">= 0.8.0" } }, + "node_modules/prexit": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prexit/-/prexit-2.3.0.tgz", + "integrity": "sha512-mX+LIbtS0anKtl2PykYabxninwloblUQMRO6CubeSmjxb+kKlATuJoH9UeN8NLE4TgIEFWfBXw7V3GkWbBrSmg==", + "license": "WTFPL", + "engines": { + "node": ">=12" + } + }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index 84f2eda..7f0b328 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "test:unit": "vitest run unit --coverage" }, "dependencies": { - "fastify": "4.28.1" + "fastify": "4.28.1", + "prexit": "2.3.0" }, "devDependencies": { "@2bad/tsconfig": "3.0.1", diff --git a/source/server.ts b/source/server.ts index 61db603..f5e32a1 100644 --- a/source/server.ts +++ b/source/server.ts @@ -1,10 +1,13 @@ /* eslint-disable @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/strict-boolean-expressions */ import fastify from 'fastify' +import prexit from 'prexit' import routes from './routes/index.ts' const DEFAULT_PORT = 3000 const DEFAULT_HOST = '127.0.0.1' +// biome-ignore lint/complexity/useLiteralKeys: tsc > biome const PORT = Number.parseInt(process.env['PORT'] || DEFAULT_PORT.toString(), 10) +// biome-ignore lint/complexity/useLiteralKeys: tsc > biome const HOST = process.env['HOST'] || DEFAULT_HOST const server = fastify() @@ -18,3 +21,8 @@ server.listen({ port: PORT, host: HOST }, (err, address) => { } console.log(`Server listening at ${address}`) }) + +prexit(async () => { + console.info('Shutting down...') + await server.close() +})