-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·467 lines (405 loc) · 13.1 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env node
var app = require('express')(),
http = require('http').Server(app),
cors = require('cors'),
bodyParser = require('body-parser');
var request = require('request'),
nodemailer = require("nodemailer"),
extend = require('util')._extend,
cliOptions = require('commander'),
fs = require('fs'),
modelParser = require('./parsers/model.js'),
fbaParser = require('./parsers/fba.js');
cliOptions.version('0.0.1')
.option('-d, --dev', 'Developer mode; this option attempts to use a token in the file: dev-user-token')
.parse(process.argv);
var WS_URL = 'http://p3.theseed.org/services/Workspace',
MS_URL = 'https://p3.theseed.org/services/ProbModelSEED',
APP_URL = 'http://p3.theseed.org/services/app_service';
// default RPC request data structure
var postData = {
version: "1.1",
method: null,
params: null
};
// user's sockets
var userClients = {},
sockets = {},
anonymousCount = 0;
// if --dev option is used, set token to token in file "dev-user-token"
// otherwise, pass on token, if there is one
if (cliOptions.dev) {
var token = fs.readFileSync('dev-user-token', 'utf8').trim();
console.log('\n\x1b[36m'+'using development token:'+'\x1b[0m', token, '\n')
app.all('/', function(req, res, next) {
req.header = {"Authorization": token};
next();
}).use(function(req, res, next) {
req.header = {"Authorization": token};
next();
})
}
// Configure CORs and body parser.
app.use( cors() )
.use( bodyParser.urlencoded({extended: false, limit: '50mb'}) )
// Configure Logging
app.use(function(req, res, next) {
console.log('%s %s', req.method, req.url);
next();
});
/**
* @api {get} /list/:path list objects in a folder
* @apiName list
*
* @apiParam {string} path path of workspace folder
* @apiParam {string} ?filter=(folders|objects) only fetch folders or objects
*
* @apiSampleRequest /list/nconrad/home/models/
*
* @apiSuccess {json} meta metadata for listed objects
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "name": "my things",
* "type": "folder",
* "path": "/nconrad/home/things",
* "modDate": "2015-10-19T03:40:29",
* "id": "245FFF5C-7613-11E5-8FA2-41B4682E0674",
* "owner": "nconrad",
* "size": 648464,
* "userMeta": { },
* "autoMeta": { },
* "userPermission": "r",
* "globalPermission": "n",
* "shockUrl": ""
* },
* {
* "name": "1520703.3_model",
* "type": "model",
* "path": "/nconrad/home/1520703.3_model",
* "modDate": "2015-10-19T03:40:29",
* "id": "245FFF5C-7613-11E5-8FA2-41B4682E0674",
* "owner": "nconrad",
* "size": 648464,
* "userMeta": { },
* "autoMeta": { },
* "userPermission": "r",
* "globalPermission": "n",
* "shockUrl": ""
* }
* ]
*/
app.get('/v0/list/*', AuthRequired, function(req, res) {
var path = '/'+req.params[0];
var post = extend(postData, {
method: 'Workspace.ls',
params: {paths: [ path ] }
})
if ('filter' in req.query && req.query.filter === 'folders')
post.params.excludeObjects = true;
else if ('filter' in req.query && req.query.filter === 'objects')
post.params.excludeDirectories = true;
if ('recursive' in req.query)
post.params.recursive = true;
post.params = [post.params]
request.post(WS_URL, {form: JSON.stringify(post), headers: req.header },
function (error, response, body) {
//console.log('error', response)
var data = JSON.parse(body)
if (!('result' in data)) {
var e = sanitizeError(data);
res.status(520).send( e );
return;
}
var items = data.result[0][path];
var contents = [];
for (var i=0; i<items.length; i++ ) {
contents.push( sanitizeMeta(items[i]) );
}
res.send( contents );
});
})
/**
* @api {get} /meta/:path Get meta data on worksapce obj
* @apiName getMeta
*
* @apiParam {string} path path to workspace object
*
* @apiSampleRequest /list/nconrad/home/models/1520703.3_model
*
* @apiSuccess {json} meta metadata for given object
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "name": "1520703.3_model",
* "type": "model",
* "path": "/nconrad/home/models/1520703.3_model",
* "modDate": "2015-10-19T03:40:29",
* "id": "245FFF5C-7613-11E5-8FA2-41B4682E0674",
* "owner": "nconrad",
* "size": 648464,
* "userMeta": { },
* "autoMeta": { },
* "userPermission": "r",
* "globalPermission": "n",
* "shockUrl": ""
* }
*/
.get('/v0/meta/*', AuthRequired, function(req, res) {
var path = '/'+req.params[0];
var post = extend(postData, {
method: 'Workspace.get',
params: [ {objects: [ path ], metadata_only: true} ]
})
request.post(WS_URL, {form: JSON.stringify(post), headers: req.header},
function (error, response, body) {
var data = JSON.parse(body)
if (!('result' in data)) {
var e = sanitizeError(data);
res.status(520).send( e );
return;
}
var d = data.result[0][0];
res.send( sanitizeMeta(d[0]) );
});
})
/**
* @api {get} /objects/:path get object data from workspace
* @apiName getModel
*
* @apiParam {string} path Path to workspace object
*
* @apiSuccess {json} data the metadata [meta] and object data [data] for the given object
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "meta": {},
* "data": {}
* }
*/
.get('/v0/objects/*', AuthRequired, function (req, res) {
var path = '/'+req.params[0];
var post = extend(postData, {
method: 'Workspace.get',
params: [ {objects: [ path ]} ]
})
request.post(WS_URL, {form: JSON.stringify(post), headers: req.header},
function (error, response, body) {
var data = JSON.parse(body);
if (!('result' in data)) {
var e = sanitizeError(data);
res.status(520).send( e );
return;
}
var d = data.result[0][0];
// content may or may not be json :(
try {
var obj = JSON.parse(d[1]);
} catch (e) {
obj = d[1]
}
res.send( {meta: d[0], data: obj } );
});
})
/**
* @api {get} /model/:path Request model detail information
* @apiName getModel
*
* @apiParam {string} path Path to workspace object
*
* @apiSuccess {Model Object} A parsed and sanitized model object. Great for model tables.
*/
.get('/v0/model/*', AuthRequired, (req, res) => {
var path = '/'+req.params[0];
var post = extend(postData, {
method: 'Workspace.get',
params: [ {objects: [ path ]} ]
})
request.post(WS_URL, {json: post, headers: req.header},
(error, response, body) => {
if (!('result' in body)) {
var e = sanitizeError(data);
res.status(520).send( e );
return;
}
var meta = body.result[0][0][0],
data = body.result[0][0][1];
var meta = sanitizeMeta(meta);
// if data is stored in shock, fetch;
// otherwise, error
if (meta.shockUrl.length > 0) {
getShockData(meta.shockUrl, req.header.Authorization,
(d) => {
res.send({
meta: meta,
data: modelParser.parse(JSON.parse(d))
});
})
} else {
res.status(520).send( {msg: 'No node was found for shock data.'} );
return;
}
});
})
/**
* @api {get} /fba/:path Request model detail information
* @apiName getModel
*
* @apiParam {string} path Path to workspace object
*
* @apiSuccess {fba object} A parsed and sanitized fba object
*
*/
.get('/v0/fba/*', function (req, res) {
var path = '/'+req.params[0];
var post = extend(postData, {
method: 'Workspace.get',
params: [ {objects: [ path ]} ]
})
request.post(WS_URL, {form: JSON.stringify(post), headers: req.header},
function (error, response, body) {
var d = JSON.parse(body).result[0][0];
if (!error && response.statusCode == 200) {
res.send( {meta: d[0], data: modelParser.parse(JSON.parse(d[1])) } );
}
});
})
/**
* @api {get} /my-models/:path List user's models. If no path is given,
* the directory /<username>/home/models/ is used (the default).
*
* @apiName getModel
*
* @apiParam {string} path Path to workspace object
*
* @apiSuccess { object} Metadata on user's models
*
*/
.get('/v0/my-models/*', AuthRequired, function (req, res) {
var path = '/'+req.params[0];
// do something
})
/**
* @api {get} /publications/ List select publications related to ModelSEED. List has no particular order.
* @apiName publications
*
*
* @apiSampleRequest /publications/
*
* @apiSuccess {json} meta List of publication objects. All values are strings, except 'authors',
* which is a an array of strings and year, which is an int.
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* [{ "authors":
* [ "Henry, Christopher S",
* "DeJongh, Matthew",
* "Best, Aaron A",
* "Frybarger, Paul M",
* "Linsay, Ben",
* "Stevens, Rick L"],
* "title": "High-throughput generation, optimization and analysis of genome-scale metabolic models",
* "publication": "Nature biotechnology",
* "volumn": "28",
* "number": "9",
* "pages": "977-982",
* "year": "2010",
* "publisher": "Nature Publishing Group"
* }]
*/
.get('/v0/publications', function (req, res) {
fs.readFile('./data/publications.json', 'utf8', function (err, data) {
res.send( data );
});
})
/**
* @api {post} /feedback/ post user feedback
* @apiName feedback
*/
.post('/v0/feedback', function (req, res) {
var fb = JSON.parse(req.body.feedback);
var transporter = nodemailer.createTransport({
port: 25,
direct: false,
secure: false,
ignoreTLS: true
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]', // list of receivers
subject: 'ModelSEED Feedback from Website',
text: '',
html: 'Message: '+fb.note+'<br><br>'+
'URL: '+fb.url+'<br><br>'+
'Browser: '+'<br>'+
'<pre>'+JSON.stringify(fb.browser, null, 4)+'</pre><br><br>'+
'<img src="'+fb.img+'"><br>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
res.status(500).send({'msg': 'Was not able to deliver message.'});
return console.log(error);
}
console.log('Message sent: ' + info.response);
res.status(200).send({'msg': 'Your feedback was accepted, thanks!'});
});
})
/**
* @api {get} /test-service/ Way to test simple, unauthenticated GET request.
* @apiName test-service
*
* @apiSampleRequest /test-server/
*
* @apiSuccess {json} string Should return code 200 with string "This is just a test. This is only a test."
*
*/
.get('/test-service', function (req, res) {
res.status(200).send( 'This is just a test. This is only a test.' );
})
// sanitize error messages from services
function sanitizeError(data) {
if ('error' in data.error) {
var error = data.error.error.split('_ERROR_');
var msg = error[1],
debug = error[2].trim();
} else {
var msg = data.error.message,
debug = '';
}
return {msg: msg, debug: debug};
}
function AuthRequired(req, res, next) {
if ('authorization' in req.headers) {
req.header = {"Authorization": req.headers.authorization};
next();
} else {
res.status(401).send( {error: 'Auth is required!'} );
}
}
// sanitize workspace meta data list into dictionary
function sanitizeMeta(l) {
return {
name: l[0],
type: l[1],
path: l[2]+l[0],
modDate: l[3],
id: l[4],
owner: l[5],
size: l[6],
userMeta: l[7],
autoMeta: l[8],
userPermission: l[9],
globalPermission: l[10],
shockUrl: l[11]
};
}
function getShockData(node, token, cb) {
var url = node+'?download',
header = {headers: {Authorization: 'OAuth '+token}};
request(url, header, (error, response, body) => { cb(body); })
}
var server = http.listen(3000, () => {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});