-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
102 lines (90 loc) · 2.54 KB
/
server.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
require('dotenv').config()
const Hapi = require('@hapi/hapi')
const config = require('getconfig')
const pack = require('./package')
const constants = require('./src/constants')
const IS_DEV = config.getconfig.isDev
const start = async () => {
const hapiConfig = config.hapi
const server = new Hapi.Server(hapiConfig)
await server.register({
plugin: require('hapi-pino'),
options: {
prettyPrint: IS_DEV,
redact: !IS_DEV && ['*.aws.id', '*.aws.secret', '*.db']
}
})
server.log(['start', 'getconfig'], config)
server.log(['start'], hapiConfig)
await server.register(require('./src/ws'))
if (IS_DEV) {
await server.register({
plugin: require('./src/hapi-webpack'),
options: {
dev: {
publicPath: '/'
},
hot: {
name: pack.name
}
}
})
} else {
// Static files are only need in production
// This plugin will register routes and responses to serve all client files
// from the built webpack directory and also serve the index for all 404s
await server.register({
plugin: require('./src/static-plugin'),
options: {
directoryPath: 'dist'
}
})
}
await server.register({
plugin: require('./src/db'),
options: {
db: config.db,
subscriptions: {
[constants.CHARACTER_CHANGE]: (payload) => {
server.plugins.ws.sendStatus(constants.DATABASE_STATUS)
server.plugins.ws.sendCharacterChange(payload)
}
}
}
})
const { submissionTopic, backgroundTopic } = config.kafka
await server.register({
plugin: require('./src/kafka-plugin'),
options: {
subscriptions: {
[submissionTopic]: (messages) => {
for (const message of messages) {
server.plugins.ws.sendStatus(constants.KAFKA_STATUS)
server.plugins.ws.sendSubmission(message)
server.plugins.db.saveSubmission(message)
}
},
[backgroundTopic]: () => {
// Only one change per timeout can occur
server.plugins.ws.sendStatus(constants.KAFKA_STATUS)
server.plugins.ws.sendBackgroundChange()
}
},
kafka: {
idleTimeout: 100,
connectionTimeout: 10 * 1000,
clientId: 'kafka-consumer',
consumer: {
connectionString: config.kafka.url
}
}
}
})
server.route(require('./src/routes'))
server.start()
server.log(['start'], server.info.uri)
}
start().catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
})