-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-batch.js
More file actions
245 lines (226 loc) · 8.23 KB
/
Copy pathhttps-batch.js
File metadata and controls
245 lines (226 loc) · 8.23 KB
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
var restify = require('restify');
var rabbitmq = require('./rabbitmq.js');
var mongodb = require('./mongodb.js');
var cas = require('./cas_mongo.js');
var ubertool = require('./ubertool.js');
var utils = require('./utils.js');
var flow = require('nimble');
var user = require('./user.js');
var fs = require('fs');
var Cookies = require('cookies');
function submitBatch(req, res, next)
{
console.log("Batch Submitted to Node.js server.");
var body = '';
req.on('data', function (data)
{
body += data;
});
//console.log(body);
req.on('end', function ()
{
var json = JSON.parse(body);
//console.log(JSON.stringify(json));
var results = rabbitmq.submitUbertoolBatchRequest(json);
});
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send("Submitting Batch.\n");
}
function getBatchResults(req, res, next)
{
//console.log(req);
res.send("Getting Batch Results.\n");
return next();
}
var credentials = {certificate: fs.readFileSync('certs/server-cert.pem'),key: fs.readFileSync('certs/server-key.pem')};
var server = restify.createServer(credentials);
server.listen(9443, function() {
console.log('%s listening at %s', server.name, server.url);
});
server.use(restify.CORS());
server.use(restify.fullResponse());
server.post('/user/login/:userid', function(req, res, next){
var user_id = req.params.userid;
console.log('user id: ' + user_id);
var body = '';
req.on('data', function (data)
{
body += data;
});
req.on('end', function ()
{
json = JSON.parse(body);
console.log(json);
console.log('json: ' + json);
console.log('password' + json.pswrd);
user.getLoginDecision(user_id,json.pswrd,function(err, decision_data){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Methods', "POST");
if(decision_data.decision)
{
var acsid_string = "test="+decision_data.sid;
console.log("acsid_string: " + acsid_string);
res.header('Set-Cookie',acsid_string);
}
res.send(decision_data);
});
});
});
server.post('/user/registration/:user_id', function(req, res, next){
var user_id = req.params.user_id;
console.log('user id: ' + user_id);
var body = '';
req.on('data', function (data)
{
body += data;
});
req.on('end', function ()
{
json = JSON.parse(body);
console.log(json);
console.log('password: ' + json.pswrd);
console.log('email address: ' + json.email_address);
user.registerUser(user_id,json.pswrd,json.email_address,function(err, sid_data){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Methods', "POST");
res.send(sid_data);
});
});
});
//Batch REST Services
server.get('/batch_configs', function(req, res, next){
mongodb.getBatchNames(function(error, batch_ids){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(batch_ids);
});
});
server.post('/batch',submitBatch);
server.get('/batch_results/:batchId', function(req, res, next){
var batchId = req.params.batchId;
console.log("BatchId: " + batchId);
mongodb.getBatchResults(batchId, function(error, batch_data){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
if(batch_data != null)
{
res.send(batch_data);
} else {
res.send("Problem returning results");
}
});
});
server.post('/batch_results/:batchId', function(req, res, next){
var batchId = req.params.batchId;
console.log("BatchId: " + batchId);
var body = '';
req.on('data', function (data)
{
body += data;
});
req.on('end', function ()
{
console.log("body: " + body);
var json = JSON.parse(body);
var user_id = json.user_id;
var user_api_key = json.api_key;
console.log("json user_id: " + user_id + " user_api_key: " + user_api_key);
user.authenticateRestAccess(user_id,user_api_key,function(err,authenticated){
console.log("authenticated: " + authenticated);
if(authenticated){
mongodb.getBatchResults(batchId, function(error, batch_data){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
if(batch_data != null)
{
res.send(batch_data);
} else {
res.send("Problem returning results");
}
});
} else {
console.log('User API Authentication failed');
res.send("User: " + user_id + " passed an incorrect api key and cannot call this method.");
}
});
});
});
//CAS Services
server.get('/cas/:cas_num', function(req, res, next){
var cas_number = req.params.cas_num;
console.log("Cas Number: " + cas_number);
cas.getChemicalName(cas_number, function(error,chemical_name){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(chemical_name);
});
});
server.get('/all-cas', function(req, res, next){
cas.getAll(function(error,all_cas){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(all_cas);
});
});
//Ubertool Services
server.get('/ubertool/:config_type/config_names', function(req, res, next){
var config_type = req.params.config_type;
ubertool.getAllConfigNames(config_type,function(error,config_names){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(config_names);
});
});
server.get('/ubertool/:config_type/:config', function(req, res, next){
var config_type = req.params.config_type;
var config = req.params.config;
console.log("GET for Configuration Name: " + config);
ubertool.getConfigData(config_type,config, function(error,config_data){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(config_data);
});
});
server.post('/ubertool/:config_type/:config', function(req, res, next){
var config_type = req.params.config_type;
var config = req.params.config;
var body = '';
var json = '';
req.on('data', function (data)
{
body += data;
});
req.on('end', function ()
{
json = JSON.parse(body);
console.log("POST for Configuration Name: " + config + " config type: " + config_type + " json data: " + json);
ubertool.addUpdateConfig(config_type,config,json, function(error, results)
{
res.send(results);
});
});
});
server.get('/api-key', function(req, res, next){
console.log("GET for API Key");
apiKey = utils.generateNewAPIKey();
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(apiKey);
});
server.on('MethodNotAllowed', unknownMethodHandler);
function unknownMethodHandler(req, res) {
if (req.method.toLowerCase() === 'options') {
var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version'];
if (res.methods.indexOf('OPTIONS') === -1) res.methods.push('OPTIONS');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
res.header('Access-Control-Allow-Methods', res.methods.join(', '));
res.header('Access-Control-Allow-Origin', req.headers.origin);
return res.send(204);
}
else
return res.send(new restify.MethodNotAllowedError());
}