UNPKG

jetstream-microservice

Version:

Jetstream Node Package for easy implementation of Jetstream microservices

74 lines (59 loc) 2.26 kB
/** Created by David Dyke USED FOR SETTING UP A SERVICE WITH JETSTREAM INC - REGISTERING WITH REGISTRY - CONNECTING TO EVENTS - SECURING THE APP - HEALTH ENDPOINT - DB MIGRATIONS */ var registry = require('./dist/registry'); var events = require('./dist/events/'); var secuity = require('./dist/security'); var health = require('./dist/health'); var errors = require('./dist/errors'); var request = require('./dist/request'); module.exports = { init: function (app, config) { // the aim of this is to get everything ship shape for Jetstream, we're expecting a config but can go for defaults if it's not provided if (!config) config = {}; if(config.amqpDisable === undefined){ config.amqpDisable = false; }; jetStreamConfig = { name: config.name || package.name, location: process.env.DOCKER ? "http://" + (config.name || "new-service") : config.location, documentation: config.documentation, registry_url: process.env.DOCKER ? "http://service-registry" : (config.registry_url || "http://localhost:8001"), jwt_secret: config.jwt_secret || "secret", amqpHost: config.amqpHost || (process.env.DOCKER ? "amqp://rabbitmq" : 'amqp://localhost'), amqpDisable: config.amqpDisable }; global.jetstreamConfig = jetStreamConfig; // then we have a few tasks // set the dependencies up global.dependencies = {}; // REGISTERING WITH REGISTRY registry.registerMe(); // HEALTH ENDPOINT app.get('/health', health); // SECURING THE APP if (!process.env.IGNORE_JWT_SECURITY || process.env.IGNORE_JWT_SECURITY === "0" || process.env.IGNORE_JWT_SECURITY === "false") { app.use(secuity.inbound); } // ERROR HANDLING //app.use(errors); // finally we have to create a global event object which can do some event magic if(config.amqpDisable == false){ events.connect(); } else { console.log("AMQP/RabbitMQ has been disabled in Jetstream settings"); } }, events: { on: events.on, emit: events.emit }, request: request }