-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
154 lines (145 loc) · 4.57 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var config = require('config');
var httpProxy = require('http-proxy');
var request = require('request');
var q = require('q');
var express = require('express');
var app = express();
var https = require('https');
var http = require('http');
var url = require('url');
// cross origin
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
// list all swagger document urls
var listUrl = config.get("list_url");
// general infor of your application
var info = config.get("info");
app.get('/docs', function(req, res) {
var schemes = [ req.protocol ];
if (config.has('schemes')) {
schemes = config.get('schemes', false);
}
getApis(listUrl).then(function(data){
var ret = data.reduce(function(a, i){
var filters = i.filters;
i = i.body;
if (!a) {
a = Object.assign({}, i);
a.paths = {};
a.definitions = {};
}
// combines paths
for (var key in i.paths){
if (filters) {
filters.forEach( function(filter){
if ( !(key.startsWith(filter)) ){
a.paths[i.basePath + key] = i.paths[key];
}
});
}
else {
a.paths[i.basePath + key] = i.paths[key];
}
}
// combines definitions
for (var k in i.definitions){
a.definitions[i.basePath + k] = i.definitions[k];
}
return a;
}, false);
ret.info = info;
ret.host = null;
ret.basePath = null;
ret.schemes = schemes;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(ret));
});
});
var proxy = httpProxy.createProxyServer();
listUrl.forEach(function(url){
url.route_match.forEach(function(r){
// GET proxy
app.get(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
// POST proxy
app.post(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
// PUT proxy
app.put(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
// PATCH proxy
app.patch(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
// DELETE proxy
app.delete(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
// OPTIONS proxy
app.options(r, function(req, res){
doForward(req, res, url.base_path, proxy);
});
});
});
var doForward = function(req, res, baseUrl, p) {
try {
console.log('doForward %s', baseUrl);
console.log('With path', req.path);
if (url.parse(baseUrl).protocol === 'https:') {
p.web(req, res, {
target: baseUrl,
agent : https.globalAgent ,
headers: {
host: url.parse(baseUrl).hostname
}
}, function(e) {
console.log(e);
res.status(500).json({});
});
} else {
p.web(req, res, {
target: baseUrl,
agent : http.globalAgent ,
headers: {
host: url.parse(baseUrl).hostname
}
}, function(e) {
console.log(e);
res.status(500).json({});
});
}
} catch (e) {
console.log(e);
}
}
// addon swagger page
app.use('/', express.static(__dirname + '/swagger-ui/'));
// Start web server at port 3000
var port = config.get("port");
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Combines swaggers http://%s:%s', host, port);
});
// get swagger json data from urls
var getApis = function(urls){
var the_promises = [];
urls.forEach(function(url){
var def = q.defer();
request(url.docs, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
var result = { body: body, filters: url.filters };
def.resolve(result);
}
});
the_promises.push(def.promise);
});
return q.all(the_promises);
}