-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
386 lines (360 loc) · 13 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var mongojs = require("mongojs");
var Q = require('q');
var _u = require('underscore');
var apiService = require('./apiService');
var config = require('./config');
var silkExport = require('./silkExport');
var app = express();
var db = mongojs("apis", ["apis"]);
app.use( bodyParser.json() );
app.set('json spaces', 4); //prettify json
app.use('/', express.static(__dirname + '/public') );
// only used to access cached collection from external tools, like custom movie visualizer
var cors = require('cors')
app.use(cors())
var fillApiDesc = function(apiDesc) {
apiDesc._links = {
"self": {
"href": config.host +"/"+ apiDesc._id +"/",
"ns:template": config.host +"/{api}/",
"title": apiDesc.label
}
};
if (apiDesc._embedded && apiDesc._embedded["ns:endpoints"]) {
apiDesc._embedded["ns:endpoints"].forEach( function(ep) {
ep._links = {
"self": {
"href": config.host +"/"+ apiDesc._id +"/"+ ep._id +"/",
"ns:template": config.host +"/{api}/{endpoint}/",
"title": ep.label
}
};
});
}
return apiDesc;
};
var getColNamesPromise = function(){
var deferred = Q.defer();
db.getCollectionNames( function(err, colNames) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(
_u.filter(colNames, function(name){
return name !== 'apis' && name.split('.')[0] !== 'system';
})
);
}
});
return deferred.promise;
};
app.post('/silkExport/:apiId/:ecId/', function(req, res){
db.apis.findOne( { _id: req.params.apiId }, function(err, apiDesc) {
if (err) {
res.send(500, err.message);
} else if (apiDesc == null) {
res.send(404);
} else if ( Object.keys(req.body).length === 0 || !(req.body instanceof Array) ) {
res.send(400, "Request body must be a JSON array.");
} else {
var ec = apiService.getEntityClass(apiDesc, req.params.ecId);
silkExport.go(req.body, ec, function() {
var json = {"status": "success"};
res.send(json);
}, function(err) {
res.send(500, err.message);
});
}
});
});
app.get('/swagger/:apiId', function(req, res){
db.apis.findOne( { _id: req.params.apiId }, function(err, apiDesc) {
if (err) {
res.send(500, err.message);
} else if (apiDesc == null) {
res.send(404);
} else {
var json = apiService.swagger(apiDesc);
res.send(json);
}
});
});
app.get('/api/', function(req, res){
db.apis.find( function(err, docs) {
if (err) {
res.send(500, err.message);
} else {
getColNamesPromise().then( function(colNames) {
var fillCachedCols = function(apiDesc) {
apiDesc._links["ns:cachedCollections"] = _u.filter(colNames, function(name){
return name.split('!')[0] == apiDesc._id
})
.map( function(name) {
var nameParts = name.split('!');
return {
"href": config.host +"/"+ nameParts[0] +"/!"+ nameParts[1] +"/",
"title": nameParts[1] + "Cache",
"name": "!"+ nameParts[1]
};
});
return apiDesc;
};
var json = {
"_links": {
"self": { "href": config.host +"/" },
"curies": config.curies
},
"_embedded": {
"ns:apis" : docs.map( _u.compose(fillCachedCols, fillApiDesc) )
}
};
json._links.curies = config.curies;
res.send(json);
}, function(err) {
res.send(500, err.message);
});
}
});
});
app.get('/api/:apiId/', function(req, res){
db.apis.findOne( { _id: req.params.apiId }, function(err, apiDesc) {
if (err) {
res.send(500, err.message);
} else if (apiDesc == null) {
res.send(404);
} else {
var json = fillApiDesc(apiDesc);
json._links.curies = config.curies.concat( apiService.curies(apiDesc) );
res.send(json);
}
});
});
app.put('/api/:apiId', function(req, res) {
//to move resource, PUT to old id with new id in json
if ( Object.keys(req.body).length === 0 ) {
res.send(400, "Request body mustn't be empty.");
return;
}
var apiDesc = req.body;
if (apiDesc._id === undefined) {
apiDesc._id = apiService.cleanString( req.params.apiId );
} else {
apiDesc._id = apiService.cleanString( apiDesc._id );
}
//insert or update
var resJson;
db.apis.save(apiDesc, function(err, saved) {
if(err || !saved) {
res.send(500, err.message);
} else {
resJson = fillApiDesc(saved);
//check if it's a move
if (apiDesc._id != req.params.apiId) {
db.apis.remove({_id: req.params.apiId }, true, function(err) {
if (err) {
res.send(500, err.message);
} else {
res.send(204);
}
});
} else {
resJson._links.curies = config.curies.concat( apiService.curies(apiDesc) );
res.send(resJson);
}
}
});
});
app.delete('/api/:apiId', function(req, res) {
db.apis.remove({_id: req.params.apiId}, true, function(err) {
if(err) {
res.send(500, err.message);
} else {
res.send(204);
}
});
});
// if endpointId prefixed with a !, it's a cached collection
// curl 'http://localhost:3000/api/rotten/!movie?sort=release_dates.theater&direction=-1'
// curl 'http://localhost:3000/api/rotten/!movie?silkExport=1'
app.get('/api/:apiId/!:ecId/', function(req, res){
var ecId = req.params.ecId;
var colName = req.params.apiId +"!"+ ecId;
var sortOpts = {};
var sortName = req.query.sort;
var sortDirection = req.query.direction ? parseInt(req.query.direction) : 1;
if (sortName) {
sortOpts[sortName] = sortDirection;
}
db.collection(colName).find().sort(sortOpts, function(err, docs) {
if (err) {
res.send(500, err.message);
} else {
if ( req.query.silkExport === "1" ) {
//export to silk
db.apis.findOne( { _id: req.params.apiId }, function(err, apiDesc) {
if (err) {
res.send(500, err.message);
} else if (apiDesc == null) {
res.send(404);
} else {
var ec = apiService.getEntityClass(apiDesc, ecId);
silkExport.go(docs, ec, function() {
var json = {"status": "success"};
res.send(json);
}, function(err) {
res.send(500, err.message);
});
}
});
} else {
//return cached collection
var json = {
"_links": {
"self": {
"href": config.host +"/"+ req.params.apiId +"/!"+ ecId +"/",
"title": ecId + "Cache"
},
"curies": config.curies
},
"_embedded": {
"ns:results" : docs
}
};
res.send(json);
}
}
});
});
app.delete('/api/:apiId/!:ecId/', function(req, res){
var colName = req.params.apiId +"!"+ req.params.ecId;
db.collection(colName).drop( function(err) {
if (err) {
res.send(500, err.message);
} else {
res.send(204);
}
});
});
// get one record of a cached collection
app.get('/api/:apiId/!:epId/:recId', function(req, res){
var colName = req.params.apiId +"!"+ req.params.epId;
var recId = req.params.recId;
db.collection(colName).findOne( { _id: recId }, function(err, rec) {
if (err) {
res.send(500, err.message);
} else if (rec == null) {
res.send(404);
} else {
res.send(rec);
}
});
});
app.put('/api/:apiId/!:epId/:recId', function(req, res){
var colName = req.params.apiId +"!"+ req.params.epId;
if ( Object.keys(req.body).length === 0 ) {
res.send(400, "Request body mustn't be empty.");
return;
}
var rec = req.body;
//set id
if (rec._id === undefined) {
rec._id = apiService.cleanString( req.params.recId );
} else {
rec._id = apiService.cleanString( rec._id.toString() );
}
//set links
if (rec._links === undefined) {
rec._links = {};
}
if (rec._links.self === undefined) {
rec._links.self = {};
}
rec._links.self.href = config.host +"/"+ req.params.apiId +"/!"+ req.params.epId +"/"+ rec._id;
rec._links.curies = config.curies;
//insert or update
db.collection(colName).save(rec, function(err, saved) {
if(err || !saved) {
res.send(500, err.message);
} else {
res.send(saved);
}
});
});
app.delete('/api/:apiId/!:epId/:recId', function(req, res){
var colName = req.params.apiId +"!"+ req.params.epId;
db.collection(colName).remove({_id: req.params.recId}, true, function(err) {
if(err) {
res.send(500, err.message);
} else {
res.send(204);
}
});
});
// with this route you can actually make queries to the original servers using the saved API descriptions
app.get(/^\/api\/([a-zA-Z0-9-_]+)\/([a-zA-Z0-9-_]+)\/(.*)$/, function(req, res){
var apiId = req.params[0];
var endpointId = req.params[1];
var url = req.params[2];
db.apis.findOne({_id: apiId }, function(err, apiDesc) {
if (err) {
res.send(500, err.message);
} else if (apiDesc === null) {
res.send(404, "API '"+ apiId +"' not found.");
} else {
var epId = endpointId;
var p;
if (epId == "_") {
if (url === undefined) {
var errMsg = "endpoint and url both undefined!";
console.error(errMsg);
res.send(400, errMsg);
return;
}
p = apiService.httpGet(url, apiDesc);
} else {
var ep = apiService.getEp(apiDesc, epId);
if (ep === undefined) {
res.send(404, "Endpoint '"+ epId +"' not found.");
return;
}
p = apiService.query(apiDesc, ep, req.query, url)
.then( function(json) {
return apiService.wrapInTryCatch( function(){
var hostApiEp = config.host +"/"+ apiDesc._id +"/"+ (ep ? ep._id : "_") +"/";
if (json._links === undefined) {
json._links = {};
}
var slf = json._links.self;
console.log(json);
slf.href = apiService.buildUrl(hostApiEp + (url ? url : ""), req.query);
slf["ns:template"] = hostApiEp +
(ep && ep.variables.length > 0
? "{?"+ ep.variables.map( function(v){ return v.key; }).join(",") +"}" : "");
slf["ns:template-general"] = config.host +"/{api}/{endpoint}/{url || ?templateParams}";
if (!slf.title) {
slf.title = apiDesc.label;
}
json._links.curies = config.curies.concat( apiService.curies(apiDesc) );
return json;
}, function(err){
res.send(500, err + "\n\n" + err.stack);
});
});
}
p.then( function(json) {
res.send(json);
}, function(err) {
res.send(500, err + "\n\n" + err.stack);
});
}
});
});
app.get("*", function(req, res) {
res.send(404, "four oh four...");
});
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});