-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (32 loc) · 991 Bytes
/
server.js
File metadata and controls
40 lines (32 loc) · 991 Bytes
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
'use strict'
// Read the .env file.
require('dotenv').config()
// Require the framework
const Fastify = require('fastify')
// Require library to exit fastify process, gracefully (if possible)
const closeWithGrace = require('close-with-grace')
// Instantiate Fastify with some config
const server = Fastify({
logger: true
})
// Register your application as a normal plugin.
const appService = require('./server.js')
server.register(appService)
// delay is the number of milliseconds for the graceful close to finish
const closeListeners = closeWithGrace({ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 }, async function ({ signal, err, manual }) {
if (err) {
server.log.error(err)
}
await server.close()
})
server.addHook('onClose', async (instance, done) => {
closeListeners.uninstall()
done()
})
// Start listening.
server.listen({ port: process.env.PORT || 3000, host: '0.0.0.0' }, (err) => {
if (err) {
server.log.error(err)
process.exit(1)
}
})