forked from bitfinexcom/bfx-report-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (68 loc) · 1.73 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
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
'use strict'
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production'
const express = require('express')
const app = express()
const config = require('config')
const morgan = require('morgan')
const fs = require('fs')
module.exports = { app }
const {
headersMiddleware,
errorsMiddleware,
notFoundMiddleware
} = require('./src/middlewares')
const {
corsService,
logDebugService,
logService,
initWebSocketService
} = require('./src/services')
const { logger } = logService
const routes = require('./src/routes')
const port = config.get('app.port')
const host = config.get('app.host')
const unixSocket = (
process.env.UNIX_SOCKET ||
(
config.has('app.unixSocket') &&
config.get('app.unixSocket')
)
)
app.use(corsService.corsBase())
app.use(headersMiddleware)
app.use(express.json())
if (
config.has('enableLogDebug') &&
config.get('enableLogDebug')
) {
app.use(
morgan('combined', {
stream: { write: msg => logDebugService.debug(msg) }
})
)
}
app.use('/api/', routes)
app.use(notFoundMiddleware)
app.use(errorsMiddleware)
const args = unixSocket && typeof unixSocket === 'string'
? [unixSocket.trim()]
: [port, host]
if (args.length === 1) {
try {
fs.accessSync(unixSocket, fs.constants.R_OK | fs.constants.W_OK)
fs.unlinkSync(unixSocket)
} catch (err) {}
}
const server = app.listen(...args, () => {
if (args.length === 1) {
const address = server.address()
fs.chmodSync(address, '777')
logger.info(`Server listening on unix socket ${address}`)
} else {
const host = server.address().address
const port = server.address().port
logger.info(`Server listening on host ${host} port ${port}`)
}
initWebSocketService(server)
app.emit('listened', server)
})