-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
80 lines (66 loc) · 2.89 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
79
80
'user strict';
var pkgInfo = require('./package.json');
var path = require('path');
var caller = require('caller');
var konfig = require('konfig');
var express = require('express');
var enrouten = require('express-enrouten');
var bootstrap = require('./lib/bootstrap');
var zoologist = require('./lib/zoologist');
var log = require('./lib/logger');
/**
* Create an instance of a microservice and mount upon the parent
* Express application.
*
* @public
*/
module.exports = function (options) {
var app = express();
options = buildOptions(options);
if (options.discoverable) {
zoologist.initialise(options);
}
app.once('mount', function onmount(parent) {
bootstrap(parent, options);
if (options.debug) {
log.info(options, 'bootstrapped microservice');
}
});
return app;
};
/**
* Construct an options instance.
*/
var buildOptions = function(options) {
options = options || {};
var config = konfig({ path: options.configPath || process.cwd() + '/config' });
// Default options
options.server = options.server || { port: process.env.PORT };
options.debug = options.debug || false;
options.discoverable = options.discoverable || false;
options.monitorsPath = options.monitorsPath || 'monitors';
options.controllersPath = options.controllersPath || 'controllers';
options.callerPath = options.callerPath || path.dirname(caller(2));
options.serviceName = pkgInfo.name;
options.serviceBasePath = 'services';
options.zookeeper = { connectionString: 'localhost:2181', retry: { count: 5 } };
options.partialResponseQuery = options.partialResponseQuery || 'fields';
options.correlationHeaderName = options.correlationHeaderName || 'X-CorrelationID';
options.validatorOptions = options.validatorOptions || null;
// Feature Flags
options.enableBodyParsing = options.enableBodyParsing || true;
options.enableEtag = options.enableEtag || false;
options.enableRequestTracing = options.enableRequestTracing || false;
// Return now if we have no config
if (!config.app) {
return options;
}
// Overlay config file options
options.server = config.app.server || { port: process.env.PORT };
options.serviceName = config.app.microservice.server.name || pkgInfo.name;
options.serviceBasePath = config.app.microservice.basePath || 'services';
options.networkInterfaces = (config.app.microservice.server.registrationNetworkInterfacePriority) ? config.app.microservice.server.registrationNetworkInterfacePriority : null;
options.serviceDependencies = (config.app.microservice.server.dependencies) ? config.app.microservice.server.dependencies.split(',') : null;
options.zookeeper = config.app.zookeeper || { connectionString: 'localhost:2181', retry: { count: 5 } };
return options;
};