-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
130 lines (106 loc) · 3.34 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict'
require('dotenv').config()
const Blipp = require('blipp')
const Hapi = require('@hapi/hapi')
const HapiAuthJwt2 = require('hapi-auth-jwt2')
const Vision = require('@hapi/vision')
const moment = require('moment')
const Nunjucks = require('nunjucks')
const config = require('./config')
const db = require('./src/lib/connectors/db')
const HapiPinoPlugin = require('./src/plugins/hapi-pino.plugin.js')
const { JobRegistrationService } = require('./src/lib/queue-manager/job-registration-service')
const { logger } = require('./src/logger')
const routes = require('./src/routes/water.js')
const { validate } = require('./src/lib/validate')
// This changes the locale for moment globally to English (United Kingdom). Any new instances of moment will use this
// rather than the default 'en-US'
moment.locale('en-gb')
// Define server
const server = Hapi.server({
...config.server
})
const plugins = [
require('./src/lib/queue-manager').plugin
]
const _registerServerPlugins = async (server) => {
// Service plugins
await server.register(HapiPinoPlugin())
await server.register(plugins)
await server.register({
plugin: Blipp,
options: config.blipp
})
// JWT token auth
await server.register(HapiAuthJwt2)
await server.register(Vision)
}
const _configureServerAuthStrategy = (server) => {
server.auth.strategy('jwt', 'jwt', {
...config.jwt,
validate
})
server.auth.default('jwt')
}
const _configureNunjucks = async (server) => {
server.views({
engines: {
html: {
compile: (src, options) => {
const template = Nunjucks.compile(src, options.environment)
return (context) => {
return template.render(context)
}
},
prepare: (options, next) => {
options.compileOptions.environment = Nunjucks.configure(options.path, { watch: false })
return next()
}
}
},
relativeTo: __dirname,
path: 'src/views'
})
}
const start = async function () {
try {
await _registerServerPlugins(server)
_configureServerAuthStrategy(server)
server.route(routes)
await _configureNunjucks(server)
// TODO: Ideally we wouldn't bother registering all the Queues and workers when running unit tests. So, we would
// move this into the `if (!module.parent)` block. But when we do the tests only run the first few and then exit.
// We should look into what's happening and why we can't move it there.
JobRegistrationService.go(server.queueManager)
if (!module.parent) {
await server.start()
const name = process.env.SERVICE_NAME
const uri = server.info.uri
server.log('info', `Service ${name} running at: ${uri}`)
}
} catch (err) {
logger.error('Failed to start server', err.stack)
}
}
const _processError = message => err => {
logger.error(message, err.stack)
process.exit(1)
}
process
.on('unhandledRejection', _processError('unhandledRejection'))
.on('uncaughtException', _processError('uncaughtException'))
.on('SIGINT', async () => {
logger.info('Stopping hapi server: existing requests have 25 seconds to complete')
await server.stop({ timeout: 25 * 1000 })
logger.info('Closing connection pool')
await db.pool.end()
logger.info("That's all folks!")
return process.exit(0)
})
if (!module.parent) {
start()
}
module.exports = {
server,
start
}