Skip to content

Commit

Permalink
[ts] use prexit to handle process interruptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhyde committed Aug 23, 2024
1 parent 201823f commit e4f86c7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"cSpell.words": [
"fastify",
"openapi"
"openapi",
"prexit"
]
}
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions source/server.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
})

0 comments on commit e4f86c7

Please sign in to comment.