-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (52 loc) · 1.71 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
const serviceLocator = require('service-locator')()
const createLogger = require('@serby/logger')
const env = process.env.NODE_ENV || 'development'
const inDevelopmentMode = env === 'development'
const logLevel = process.env.LOG_LEVEL || (inDevelopmentMode ? 'debug' : 'info')
const createServer = require('./server')
const config = require('con.figure')(require('./config')())
const clustered = require('clustered')
const clusterSize =
process.env.API_PROCESSES ||
process.env.NF_CPU_RESOURCES ||
config.apiProcesses
const port = process.env.PORT || config.http.port
const createBackendFactory = require('./lib/backend-factory-creator')
const logger = createLogger('darkroom', { logLevel })
serviceLocator
.register('name', 'darkroom')
.register('env', env)
.register('config', config)
.register('logger', logger)
createBackendFactory(serviceLocator, (error, factory) => {
if (error) {
logger.fatal(error, 'Error starting darkroom')
return process.exit(1)
}
factory.setup(function (error) {
if (error) {
logger.fatal(error, 'Could not setup backend')
process.exit(1)
}
const app = createServer(serviceLocator, factory)
if (process.env.NODE_ENV === undefined) {
logger.fatal('NODE_ENV (env) not set, process may crash.')
}
if (isNaN(clusterSize)) {
logger.fatal(
`Invalid cluster size: "${clusterSize}", please set/update API_PROCESSES (env) or config.apiProcesses`
)
return process.exit(1)
}
clustered(
function () {
app.listen(port, function () {
logger.info(
`${serviceLocator.name} listening at http://127.0.0.1:${port}`
)
})
},
{ logger, size: clusterSize }
)
})
})