-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
78 lines (64 loc) · 2.37 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
/**
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Egor Zuev <[email protected]>
*/
/**
* Expose an express web server
* @module middleware-eth-rest
*/
const config = require('./config'),
mongoose = require('mongoose'),
Promise = require('bluebird'),
path = require('path'),
bunyan = require('bunyan'),
AmqpService = require('middleware_common_infrastructure/AmqpService'),
InfrastructureInfo = require('middleware_common_infrastructure/InfrastructureInfo'),
InfrastructureService = require('middleware_common_infrastructure/InfrastructureService'),
migrator = require('middleware_service.sdk').migrator,
_ = require('lodash'),
log = bunyan.createLogger({name: 'core.rest', level: config.nodered.logging.console.level}),
models = require('./models'),
redInitter = require('middleware_service.sdk').init;
mongoose.Promise = Promise;
mongoose.accounts = mongoose.createConnection(config.mongo.accounts.uri, {useMongoClient: true});
mongoose.profile = mongoose.createConnection(config.mongo.profile.uri, {useMongoClient: true});
mongoose.data = mongoose.createConnection(config.mongo.data.uri, {useMongoClient: true});
const runSystem = async function () {
const rabbit = new AmqpService(
config.systemRabbit.url,
config.systemRabbit.exchange,
config.systemRabbit.serviceName
);
const info = new InfrastructureInfo(require('./package.json'));
const system = new InfrastructureService(info, rabbit, {checkInterval: 10000});
await system.start();
system.on(system.REQUIREMENT_ERROR, (requirement, version) => {
log.error(`Not found requirement with name ${requirement.name} version=${requirement.version}.` +
` Last version of this middleware=${version}`);
process.exit(1);
});
await system.checkRequirements();
system.periodicallyCheck();
};
const init = async () => {
if (config.checkSystem)
await runSystem();
_.chain([mongoose.accounts, mongoose.data, mongoose.profile])
.compact().forEach(connection =>
connection.on('disconnected', function () {
throw new Error('mongo disconnected!');
})
).value();
models.init();
if (config.nodered.autoSyncMigrations)
await migrator.run(
config,
path.join(__dirname, 'migrations')
);
redInitter(config);
};
module.exports = init().catch((e) => {
log.error(e);
process.exit(1);
});