ironbar/services/gateway/index.js

41 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

// Core.
2018-08-31 04:36:26 -05:00
const http = require('http');
// 3rd party.
2018-08-31 04:36:26 -05:00
const debug = require('debug')('truss:gateway');
// 2nd party.
const {createDispatcher} = require('@truss/comm');
// 1st party.
const {loadServiceMap} = require('./service-map');
// Main.
(async () => {
try {
debug(`loading service map...`);
const serviceMap = await loadServiceMap();
const {executors, hooks, listeners} = serviceMap;
debug(`service map: ${JSON.stringify(serviceMap, null, ' ')}`);
const dispatcher = createDispatcher();
dispatcher.setArgs(hooks);
dispatcher.lookupActions(require('./actions'));
if (module.hot) {
module.hot.accept('./actions', () => {
dispatcher.lookupActions(require('./actions'));
});
2018-08-31 04:36:26 -05:00
}
const emitterPaths = process.env.GATEWAY_EMITTERS.split(':');
const emitters = [];
for (const emitterPath of emitterPaths) {
const Class = __non_webpack_require__(emitterPath);
emitters.push(new Class(executors, listeners));
2018-08-31 04:36:26 -05:00
}
const server = dispatcher.connect();
server.on('listening', () => {
for (const emitter of emitters) {
emitter.listen();
}
2018-12-23 07:57:32 -06:00
});
}
catch (error) {
console.error(error);
}
})();