-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·66 lines (58 loc) · 2.03 KB
/
app.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
'use strict'
require('dotenv').config()
// eslint-disable-next-line import/newline-after-import
const { KthAppinsights } = require('@kth/appinsights')
KthAppinsights.init({ name: 'node-web' })
// eslint-disable-next-line import/order
const fs = require('fs')
const log = require('@kth/log')
const config = require('./server/configuration').server
const server = require('./server/server')
const packageFile = require('./package.json')
// catches uncaught exceptions
process.on('uncaughtException', (err, origin) => {
log.error('APPLICATION EXIT - uncaught exception in ', packageFile.name)
log.error(`Uncaught Exception, origin (${origin})`, { err })
process.exit(1)
})
// catches unhandled promise rejections
process.on('unhandledRejection', reason => {
// This line below provokes an uncaughtException and will be caught few lines
// above
log.error(`unhandledRejection ${packageFile.name}`, reason)
// throw reason
})
function checkEnvironment() {
try {
log.info(`Checking environment variables from .env.ini file.`)
const lines = fs
.readFileSync('./.env.ini')
.toString()
.split('\n')
.filter(l => l.trim() && !l.startsWith('#'))
.filter(l => l)
const isDevelopment = process.env.NODE_ENV !== 'production'
lines.forEach(l => {
const name = l.substring(0, l.indexOf('=')).trim()
if (name && process.env[name] !== undefined) {
log.debug(` Environment variable '${name}' found`)
} else if (isDevelopment) {
log.debug(` Environment variable '${name}' is missing, most likely there is a default value.`)
} else {
log.warn(` Environment variable '${name}' is missing.`)
}
})
log.info(`Checking environment variables completed.`)
} catch (err) {
log.error({ err }, `Failed to check environment variables`)
}
}
checkEnvironment()
/* ****************************
* ******* SERVER START *******
* ****************************
*/
const { port } = config
server.listen(port, () => {
log.info(`Http server listening on port ${port}`)
})