-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver-external.js
116 lines (94 loc) · 3.18 KB
/
server-external.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
require('dotenv').config()
require('app-module-path').addPath(require('path').join(__dirname, 'src/'))
const { createPlugins, createCache } = require('./server-common')
// -------------- Require vendor code -----------------
const Hapi = require('@hapi/hapi')
const H2o2 = require('@hapi/h2o2')
// -------------- Require project code -----------------
const config = require('./src/external/config')
const plugins = require('./src/external/lib/hapi-plugins')
const routes = require('./src/external/modules/routes')
const viewEngine = require('./src/external/lib/view-engine/')
const { logger } = require('./src/external/logger')
const connectors = require('./src/external/lib/connectors/services')
const common = createPlugins(config, connectors)
// Configure auth plugin
const AuthConfig = require('external/lib/AuthConfig')
const authConfig = new AuthConfig(config, connectors)
const authPlugin = {
plugin: require('shared/plugins/auth'),
options: authConfig
}
// Define server with REST API cache mechanism
const server = Hapi.server({
...config.server,
cache: createCache(config)
})
// Add service connectors to request
server.decorate('request', 'services', connectors)
/**
* Async function to start HAPI server
*/
async function start () {
try {
await server.register(require('hapi-auth-cookie'))
// Set up auth strategies
server.auth.strategy('standard', 'cookie', {
...config.hapiAuthCookie,
validateFunc: (request, data) => authConfig.validateFunc(request, data)
})
server.auth.default('standard')
await server.register([...common, ...Object.values(plugins)])
await server.register([{
plugin: require('shared/plugins/returns'),
options: {
getDocumentHeader: connectors.crm.documents.getWaterLicence.bind(connectors.crm.documents),
checkAccess: true,
includeExpired: false
}
}, {
plugin: require('shared/plugins/licence-data'),
options: require('external/lib/licence-data-config')
}, {
plugin: require('shared/plugins/view-licence'),
options: require('external/lib/view-licence-config')
},
{
plugin: require('shared/plugins/flow')
}, {
plugin: require('shared/plugins/reauth'),
options: {
reauthenticate: connectors.idm.users.reauthenticate.bind(connectors.idm.users)
}
}, {
plugin: require('shared/plugins/error'),
options: { logger }
}])
// Set up Nunjucks view engine
server.views(viewEngine)
// Auth plugin
await server.register(authPlugin)
// Proxy plugin
await server.register(H2o2)
server.route(routes)
await server.start()
server.log(['info'], `Server started on ${server.info.uri} port ${server.info.port}`)
} catch (err) {
logger.error('Failed to start server', err.stack)
}
return server
}
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 external ui')
await server.stop()
return process.exit(0)
})
module.exports = server
start()