-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
97 lines (76 loc) · 2.89 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// provides all API services consumed by VML and VML Admin front ends
require('dotenv').config()
// -------------- Require vendor code -----------------
const Blipp = require('blipp')
const HapiAuthJwt2 = require('hapi-auth-jwt2')
const moment = require('moment')
moment.locale('en-gb')
// -------------- Require project code -----------------
const config = require('./config')
const routes = require('./src/routes.js')
const AirbrakePlugin = require('./src/plugins/airbrake.plugin.js')
const GlobalNotifierPlugin = require('./src/plugins/global-notifier.plugin.js')
const HapiPinoPlugin = require('./src/plugins/hapi-pino.plugin.js')
// Define server
const server = require('./server')
const plugins = [
{
plugin: Blipp,
options: config.blipp
},
HapiAuthJwt2,
require('./src/plugins/pg-boss.plugin'),
require('./src/modules/licence-import/plugin'),
require('./src/modules/charging-import/plugin'),
require('./src/modules/mod-logs/plugin'),
require('./src/modules/points/plugin'),
require('./src/modules/return-versions/plugin.js'),
require('./src/modules/nald-import/plugin'),
require('./src/modules/bill-runs-import/plugin'),
require('./src/modules/core/plugin')
]
const configureServerAuthStrategy = (server) => {
server.auth.strategy('jwt', 'jwt', {
...config.jwt,
validate: async (decoded) => ({ isValid: !!decoded.id })
})
server.auth.default('jwt')
}
const start = async function () {
await server.register(plugins)
// The order is important here. We need the Hapi pino logger as a base. Then Airbrake needs to be added to the
// server.app property. Then the GlobalNotifier can be setup as it needs access to both
await server.register(HapiPinoPlugin())
await server.register(AirbrakePlugin)
await server.register(GlobalNotifierPlugin)
server.validator(require('@hapi/joi'))
configureServerAuthStrategy(server)
server.route(routes)
if (!module.parent) {
await server.start()
}
}
function processError (error) {
console.error(error)
process.exit(1)
}
process
.on('unhandledRejection', (error) => processError(error))
.on('uncaughtException', (error) => processError(error))
.on('SIGINT', async () => {
// The timeout is set to 25 seconds (it has to be passed to Hapi in milliseconds) based on AWS ECS. When it sends a
// stop request it allows an container 30 seconds before it sends a `SIGKILL`. We know we are not containerised
// (yet!) but it's a reasonable convention to use
const options = {
timeout: 25 * 1000
}
// If there are no in-flight requests Hapi will immediately stop. If there are they get 25 seconds to finish
// before Hapi terminates them
await server.stop(options)
await server.messageQueue.stop()
// Log we're shut down using the same log format as the rest of our log output
server.logger.info("That's all folks!")
process.exit(0)
})
start()
module.exports = server