-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (126 loc) · 4.24 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Klayrhq/klayrservice
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const path = require('path');
const {
DB: {
MySQL: {
KVStore: { configureKeyValueTable },
},
},
Microservice,
Logger,
LoggerConfig,
Signals,
} = require('klayr-service-framework');
const config = require('./config');
const MYSQL_ENDPOINT = config.endpoints.mysql;
configureKeyValueTable(MYSQL_ENDPOINT);
LoggerConfig(config.log);
const packageJson = require('./package.json');
const { MODULE } = require('./shared/constants');
const { initDatabase } = require('./shared/database/init');
const { setAppContext } = require('./shared/utils/request');
const { init } = require('./shared/init');
const { setFeeEstimates } = require('./shared/dataService/business');
const logger = Logger();
const defaultBrokerConfig = {
name: 'indexer',
transporter: config.transporter,
brokerTimeout: config.brokerTimeout, // in seconds
logger: config.log,
events: {
chainNewBlock: async () => {
logger.debug("Received a 'chainNewBlock' moleculer event from connecter.");
Signals.get('chainNewBlock').dispatch();
},
systemNodeInfo: async payload => {
logger.debug("Received a 'systemNodeInfo' moleculer event from connecter.");
Signals.get('nodeInfo').dispatch(payload);
},
'update.fee_estimates': async payload => {
logger.debug("Received a 'update.fee_estimates' moleculer event from fee-estimator.");
await setFeeEstimates(payload);
},
},
dependencies: ['connector'],
};
// Add routes, events & jobs
const reportErrorAndExitProcess = err => {
logger.fatal(`Failed to start service ${packageJson.name} due to: ${err.message}`);
logger.fatal(err.stack);
process.exit(1);
};
initDatabase()
.then(async () => {
const registeredModules = [];
if (config.operations.isDataRetrievalModeEnabled) {
// Start a temporary broker to query for SDK module names
// To be used for dynamically registering the available module specific endpoints
const tempApp = Microservice({
...defaultBrokerConfig,
name: 'temp_service_indexer',
events: {},
});
setAppContext(tempApp);
await tempApp.run();
const { getRegisteredModules } = require('./shared/constants');
registeredModules.push(...(await getRegisteredModules()));
// Stop the temporary node before app definition to avoid context (logger) overwriting issue
await tempApp.getBroker().stop();
}
const app = Microservice(defaultBrokerConfig);
setAppContext(app);
app.addMethods(path.join(__dirname, 'methods'));
if (config.operations.isDataRetrievalModeEnabled) {
app.addJobs(path.join(__dirname, 'jobs', 'dataService'));
// First register all the default methods followed by app specific module methods
app.addMethods(path.join(__dirname, 'methods', 'dataService'));
registeredModules.forEach(module => {
// Map 'reward' module to the 'dynamicReward' module endpoints
if (module === MODULE.REWARD) module = MODULE.DYNAMIC_REWARD;
const methodsFilePath = path.join(
__dirname,
'methods',
'dataService',
'modules',
`${module}.js`,
);
try {
// eslint-disable-next-line import/no-dynamic-require
const methods = require(methodsFilePath);
methods.forEach(method => app.addMethod(method));
} catch (err) {
logger.warn(
`Moleculer method definitions missing for module: ${module}. Is this expected?\nWas expected at: ${methodsFilePath}.`,
);
}
});
}
if (config.operations.isIndexingModeEnabled) {
app.addMethods(path.join(__dirname, 'methods', 'indexer'));
app.addEvents(path.join(__dirname, 'events'));
app.addJobs(path.join(__dirname, 'jobs', 'indexer'));
}
// Start the application
app
.run()
.then(async () => {
logger.info(`Service started ${packageJson.name}.`);
await init();
})
.catch(reportErrorAndExitProcess);
})
.catch(reportErrorAndExitProcess);