-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (39 loc) · 1.03 KB
/
index.js
File metadata and controls
48 lines (39 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const path = require('path')
const filename = path.basename(__filename)
const logger = require('./lib/log.js')
const DB = require('./lib/db.js')
const fastify = require('fastify')({
loggerInstance: logger.child({ module: filename })
})
// Register routes
fastify.register(require('./routes/index'))
process.on('SIGTERM', onSignal)
process.on('SIGINT', onSignal)
fastify.addHook('onClose', async(instance) => {
logger.info('Fastify server is shutting down...')
const db = await DB.getClient()
await db.end()
})
// Start the server
const start = async() => {
try {
const db = await DB.getClient()
await Promise.all([
fastify.listen({ port: 3000, host: '0.0.0.0' })
, db.connect()
])
logger.info('Server is running on http://localhost:3000')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
async function close() {
await fastify.close()
process.exit(0)
}
function onSignal(signal) {
logger.warn({ signal }, 'Received shutdown signal, shutting down gracefully...')
close()
}
start()