Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jfoclpf committed May 5, 2024
1 parent 3ef0159 commit c824f3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ function startServer (callback) {

shieldsioCounters.setTimers()

let dbPool
if (argvOptions.rateLimit) {
rateLimiter.init({ defaultOrigin })
dbPool = rateLimiter.init({ defaultOrigin })
}

app.get('/', (req, res) => {
Expand Down Expand Up @@ -205,6 +206,6 @@ function startServer (callback) {
})

// gracefully exiting upon CTRL-C or when PM2 stops the process
process.on('SIGINT', (signal) => shutdownServer(signal, server))
process.on('SIGTERM', (signal) => shutdownServer(signal, server))
process.on('SIGINT', (signal) => shutdownServer(signal, server, dbPool))
process.on('SIGTERM', (signal) => shutdownServer(signal, server, dbPool))
}
2 changes: 2 additions & 0 deletions src/server/middlewares/rateLimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module.exports = {
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: `You have reached the limit of requests, please refer to ${defaultOrigin}/self-hosting or ${defaultOrigin}/request-api-key for unlimited use of this API`
})

return dbPool
},
middleware: ({ filename }) =>
(req, res, next) => {
Expand Down
23 changes: 18 additions & 5 deletions src/server/services/shutdownServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// gracefully exiting upon CTRL-C or when PM2 stops the process
module.exports = function (signal, server) {
module.exports = function (signal, server, dbPool) {
if (signal) console.log(`\nReceived signal ${signal}`)
console.log('Gracefully closing http server')
console.log('Gracefully closing HTTP server and DB connections')

// closeAllConnections() is only available after Node v18.02
if (server.closeAllConnections) server.closeAllConnections()
Expand All @@ -10,11 +10,24 @@ module.exports = function (signal, server) {
try {
server.close(function (err) {
if (err) {
console.error('There was an error', err)
console.error('Error closing the HTTP server', err)
process.exit(1)
} else {
console.log('http server closed successfully. Exiting!')
process.exit(0)
console.log('HTTP server closed successfully')
if (dbPool) {
dbPool.end((err) => {
if (err) {
console.error('Error closing DB connections', err)
} else {
console.log('DB connections closed successfully')
}
console.log('Exiting!')
process.exit(0)
})
} else {
console.log('Exiting!')
process.exit(0)
}
}
})
} catch (err) {
Expand Down

0 comments on commit c824f3a

Please sign in to comment.