forked from apigee/microgateway-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (78 loc) · 2.19 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict'
const assert = require('assert')
var path = require('path')
var gateway = require('./index');
var uuid = require('uuid');
//todo replace this with better logic.
var key, secret, source;
process.argv.forEach(function (val) {
let params = val.split('=');
if (params.length != 2) {
return;
}
let k = params[0];
let v = params[1];
console.log('setting key %s to %s', k, v)
switch (k) {
case 'key' :
key = v;
break;
case 'secret' :
secret = v;
break;
case 'source' :
source = v;
break;
}
});
assert(key, 'must have a key');
assert(secret, 'must have a secret');
assert(source, 'must have a source config directory');
var options = {keys: {}, source: source};
options.keys.key = key;
options.keys.secret = secret;
//get the config
edgeConfig.get(options, function (err, config) {
assert(!err, err)
defaultConfig(config, key, secret);
var server = gateway(config);
//load plugins
var pluginsDir = path.normalize(config.edgemicro.plugins.dir);
var plugins = server.getPluginsLoader().loadPlugins(pluginsDir);
plugins && console.log('plugins loaded ' + plugins.length)
//start the server
server.start(plugins, (err, server) => {
assert(!err, err)
console.log('server is started');
});
})
var defaultConfig = function (config) {
//required by plugins
config.key = key;
config.secret = secret;
//turn on by default
config.analytics = config.analytics || {}
config.analytics.key = config.key;
config.analytics.secret = config.secret;
config.analytics['request'] = requestOptions;
// copy keys to quota section
if (config.quota) {
Object.keys(config.quota).forEach(function (name) {
var quota = config.quota[name];
quota.key = config.key;
quota.secret = config.secret;
});
config.quota['request'] = requestOptions;
}
// set proxying options for the request module, if so configured
const requestOptions = config.edgemicro.proxy ? {
proxy: config.edgemicro.proxy,
tunnel: config.edgemicro.proxy_tunnel
} : {};
if (config.oauth) {
config.oauth['request'] = requestOptions;
}
if (config.spikearrest) {
config.spikearrest['request'] = requestOptions;
}
}