forked from apigee/microgateway-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.63 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
'use strict';
var debug = require('debug')('gateway:index');
var util = require('util');
var gateway = require('./lib/gateway');
var assert = require('assert');
var logging = require('./lib/logging');
var stats = require('./lib/stats');
var pluginsLib = require('./lib/plugins');
var uuid = require('uuid')
var configService = require('./lib/config');
var _ = require('lodash');
/**
*
* @param config must include {key:string,secret:string}
* @param cb callback when started
*/
var Gateway = function (config) {
assert(config, 'options must contain config')
config.uid = uuid.v1()
configService.init(config);
logging.init()
this.plugins = [];
this.pluginLoader = pluginsLib();
};
module.exports = function (config) {
return new Gateway(config);
}
module.exports.Logging = logging;
Gateway.prototype.start = function (cb) {
const logger =logging.getLogger();
const config = configService.get();
let plugins = this.plugins;
debug('starting edgemicro');
//debug('loaded config ' + util.inspect(config, {colors: true}));
gateway.start( plugins, function (err, server) {
if (err) {
console.error('error starting edge micro', err);
}
return cb(err, server)
});
};
Gateway.prototype.stop = function(cb){
gateway.stop(cb);
}
Gateway.prototype.addPlugin = function (name,plugin) {
assert(name,"plugin must have a name")
assert(_.isString(name),"name must be a string");
assert(_.isFunction(plugin),"plugin must be a function(config,logger,stats){return {onresponse:function(req,res,data,next){}}}");
const handler = this.pluginLoader.loadPlugin({plugin:plugin,pluginName:name});
this.plugins.push(handler);
};