-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (29 loc) · 1.03 KB
/
index.js
File metadata and controls
37 lines (29 loc) · 1.03 KB
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
'use strict'
require('dotenv').config()
const pino = require('pino')
const initServer = require('./setup/server')
const { connectDb, disconnectDb } = require('./setup/db')
const { initTransporter } = require('./lib/mail')
const { HTTP_PORT, LOG_LEVEL } = process.env
const logger = pino({ level: LOG_LEVEL })
async function start() {
const app = await initServer(logger, LOG_LEVEL)
await initTransporter(logger)
await connectDb(logger)
const serverProcess = app.listen(HTTP_PORT, () => logger.info(`[setup] Server ready at http://localhost:${HTTP_PORT}`))
onClose(serverProcess)
}
function onClose(serverProcess) {
const stopSignals = ['SIGINT', 'SIGTERM']
for (const signal of stopSignals) {
process.on(signal, () => {
serverProcess.close(async() => {
logger.info(`[setup] Server shut down`)
await disconnectDb(logger)
// eslint-disable-next-line no-process-exit
process.exit(0)
})
})
}
}
start().catch(error => logger.error({ err: error }, '[setup] Error starting the server'))