-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
63 lines (54 loc) · 2.02 KB
/
server.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
const dblogger = require('./core/common/loggers').get('DB');
const logger = require('./core/common/loggers').get('SERVER');
const mongoose = require('mongoose');
const util = require('util');
const app = require('./app');
const blueprint = require('./blueprints/env');
const validations = require('./core/common/validations');
// Validate the environment.
const { error, value: _env } = validations.validate(process.env, blueprint);
if (error) {
logger.error(`The environment is invalid (cause: ${ error.details[0].message }).`);
process.exit(23);
}
// Load all models.
require('./core/models/index');
// Load all loaders.
require('./core/loaders/index');
// Load all workers.
require('./core/workers/index');
// Connect to the database.
const auth = process.env.MONGODB_USER || process.env.MONGODB_PASSWORD ?
`${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@` : '';
const host = process.env.MONGODB_HOST;
const port = process.env.MONGODB_PORT;
const name = process.env.MONGODB_NAME;
const uriwa = `mongodb://${host}:${port}/${name}`;
const uri = `mongodb://${auth}${host}:${port}/${name}`;
logger.info(`System connects to the database @ ${uriwa}.`);
mongoose.connect(uri, {
keepAlive: 1,
poolSize: process.env.MONGODB_POOL_SIZE,
useCreateIndex: true,
useNewUrlParser: true
}).then(() => {
logger.info(`System connected to the database @ ${uriwa}.`);
}).catch((error) => {
logger.error(`System failed to connect to the database @ ${uriwa}.`, error);
process.exit(42);
});
// Log more details when the log level is debug.
if (process.env.LOG_LEVEL === 'debug') {
mongoose.set('debug', (collection, method, query, doc) => {
const iquery = util.inspect(query, false, 30);
const idoc = util.inspect(doc, false, 30);
dblogger.debug(`${collection}.${method} ${iquery} ${idoc}`);
});
}
// Bind and listen for connections on the given port and host.
if (!module.parent) {
app.listen(8888, '0.0.0.0', () => {
logger.info(`Server started in ${process.env.NODE_ENV} mode on port 8888.`);
});
}
module.exports = app;