ironbar/services/gateway/index.js

41 lines
1.2 KiB
JavaScript

// Core.
const http = require('http');
// 3rd party.
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'));
});
}
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));
}
const server = dispatcher.connect();
server.on('listening', () => {
for (const emitter of emitters) {
emitter.listen();
}
});
}
catch (error) {
console.error(error);
}
})();