forked from covidgreen/covid-green-interoperability-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 1.06 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
const startServer = require('./lib/server')
const getConfig = require('./lib/config')
const { getPostgratorInstance } = require('./lib/migrate')
const main = async () => {
process.on('unhandledRejection', err => {
console.error(err)
process.exit(1)
})
const config = await getConfig()
const postgrator = getPostgratorInstance(config)
const expectedVersion = await postgrator.getMaxVersion()
const currentVersion = await postgrator.getDatabaseVersion()
if (currentVersion !== expectedVersion) {
console.error(
`expected version ${expectedVersion}, but db is at version ${currentVersion}`
)
process.exit(1)
}
const server = require('fastify')(config.fastifyInit)
server.register(startServer, config)
const address = await server.listen(config.fastify)
server.log.info(`Server running at: ${address}`)
for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, () =>
server.close().then(err => {
console.log(`close application on ${signal}`)
process.exit(err ? 1 : 0)
})
)
}
}
main()