-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
448 lines (401 loc) · 11.2 KB
/
api.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
/*
* API.js
*
* Defines REST API endpoints.
* Connects to Mongo as a data store.
* Writes logs to console & Loggly service.
*
*/
var flatiron = require('flatiron'),
app = flatiron.app,
union = require('union'),
Color = require('color'),
util = require('util'),
winston = require('winston'),
ecstatic = require('ecstatic');
// Add Loggly support
require('winston-loggly');
// Activate Flatiron plugins
app.use(flatiron.plugins.http);
// Set config module to read environment variables.
//
app.config.use('env');
var v = validateEnv();
// Constants
//
var JSONtype = { 'Content-Type': 'application/json' };
var PAGE_SIZE = 20;
// Configure Loggly options
//
var logglyOpt =
{
subdomain: v.LOGGLY_SUB_DOMAIN,
inputToken: v.LOGGLY_INPUT_TOKEN,
inputName: v.LOGGLY_INPUT_NAME,
auth: {
username: v.LOGGLY_USERNAME,
password: v.LOGGLY_PASSWORD,
}
};
winston.add(winston.transports.Loggly,logglyOpt);
winston.info('=================== STARTING APP =================');
//Mongo connection
//
var mUrl = 'mongodb://'+v.MONGO_HOST+':'+v.MONGO_PORT+'/hcp1';
winston.info('Mongo connection URL: ', mUrl);
var hcp1 = require('mongojs').connect(mUrl);
//Mongo authentication
//
winston.info('Attempting database authentication');
hcp1.authenticate(v.MONGO_USER,v.MONGO_PASS,function(err, data) {
if (!err) {
winston.info('Database authentication successful.');
} else {
winston.info('Database authentication error. Aborting now.');
return process.exit(1);
}
});
// Mongo collections
//
var audit = hcp1.collection('audit'),
force = hcp1.collection('force'),
pulse = hcp1.collection('pulse'),
shlocks = hcp1.collection('shlocks');
audit.name = 'audit';
force.name = 'force';
pulse.name = 'pulse';
shlocks.name = 'shlocks';
// Generic callback function to use with collection.save function.
//
function saveCallback(err, docs) {
if (!err) {
} else {
winston.error('mongo collection save failed',err);
}
}
// Reads all environment variables and returns then as an object.
// If any variable is missing from the environment the node process will exit.
function validateEnv() {
winston.info('Validating Environment');
var fail = false;
var v = {};
var envVariables =
['MONGO_USER', 'MONGO_PASS','MONGO_HOST', 'MONGO_PORT',
'LOGGLY_INPUT_TOKEN', 'LOGGLY_INPUT_NAME', 'LOGGLY_SUB_DOMAIN',
'LOGGLY_USERNAME', 'LOGGLY_PASSWORD', 'NODE_ENV','LOCATION','API_PORT'];
for (i in envVariables) {
var item = envVariables[i];
winston.info('Reading env variable ',item);
if (!fail && app.config.env().get(item) === undefined) {
fail = true;
winston.error(item,' undefined');
} else {
v[item] = app.config.get(item);
};
};
if (fail) {
winston.error('Environment improperly defined',process.env);
return process.exit(1);
} else {
winston.info('Environment validated successfully');
}
return v;
};
// Object used for holding analytics data.
// Name is irreverant.
//
function Shlock(kind, method, url ) {
this.kind = kind;
this.method = method;
this.url = url;
this.time = new Date().toJSON();
};
// Object used to hold common API related information.
//
function Meta(status, path, count) {
this.status = status;
this.path = path;
this.count = count;
}
// Write standard meta data and header.
//
function writeMeta(res,code,url,count) {
var meta = new Meta('success',url,count);
res.writeHead(code,JSONtype);
res.write(JSON.stringify(meta) + '\n');
}
// POST Handler
// Always returns status code 200 (success) to the client.
// Creates metrics data and saves it to Mongo.
// logs the event with metrics information.
//
function postHandler( url, body, res, coll) {
var self = this;
var method = 'POST';
// Always return success
writeMeta(res, 200,url,0);
res.end('\n');
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
shlock.system = v.MONGO_USER;
winston.info(util.inspect(shlock));
coll.save(shlock,saveCallback);
};
// Handle Errors for GET API calls.
//
function getHandlerError(res,url) {
writeMeta(res,501,url,0);
res.end('\n');
}
// Analyzes the query object, which holds parameters from the URL's
// query string. Looks for parameters based on the collection name.
//
function getFindCriteria(coll,query) {
var criteria = {};
switch (coll) {
case 'force':
if (query.hasOwnProperty('source')) {
criteria.source = query.source;
};
if (query.hasOwnProperty('ip')) {
criteria.IP = query.ip;
};
break;
}
winston.info('find criteria: ',util.inspect(criteria));
return criteria;
}
// Pull out a list of fields which should be returned from the query.
// This is more like a whitelist filter.
function getFindFields(coll,query) {
var filter = {};
switch (coll) {
case 'force':
if (query.hasOwnProperty('fields')) {
filter.fields = query.fields.split(',')
.reduce(function(acc, item) {
acc[item] = 1;
return acc;
},{})
}
break;
}
winston.info('mongo collection find fields: ',util.inspect(filter));
return filter;
}
// Checks querystring to determine whether or not it has
// the expected properties
//
function isQueryValid(query) {
var valid = true;
winston.info('query: ',util.inspect(query));
if (Object.keys(query).length == 0) {
// No query, RELAX!
} else {
// Check for page
if (query.hasOwnProperty('page')) {
if (isNaN(parseInt(query.page))) {
valid = false;
}
}
// Check for count
if (query.hasOwnProperty('count')) {
if (!(query.count === 'true' ||
query.count === 'false')) {
valid = false;
}
}
// Check for filter
if (query.hasOwnProperty('fields')) {
if (!isNaN(query.filter)) {
valid = false;
}
}
}
if (!valid) {
winston.warn('isQueryValid: ',valid);
} else {
winston.info('isQueryValid: ',valid);
}
return valid;
}
// Generic function to handle a find function callback
// for a Mongo collection.
function findHandler(err, docs, res, url) {
if (!err) {
// Return Success & JSON Content-type
var length = docs.length;
writeMeta(res,200,url,length);
// Process results from Mongo
docs.forEach(function(e,i,a) {
delete e._id;
res.write(JSON.stringify(e)+'\n');
});
res.end('\n');
} else {
getHandlerError(res,url);
}
}
// API ENDPOINT
app.router.get('/audit',function() {
// Setup
var self = this;
var method = 'GET';
var url = '/shlocks';
var body = self.req.body;
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
winston.info(util.inspect(shlock));
// Query Mongo
audit.find(function(err,docs) {findHandler(err,docs,self.res, url)});
});
function getHandler(request,response,url,coll) {
// Setup
var self = this;
var method = 'GET';
var body = request.body;
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
winston.info(util.inspect(shlock));
// Check for query string
var query = request.query;
if (!isQueryValid(query)) {
writeMeta(resp,400,url,0);
resp.end('\n');
} else {
// Handle count=true
if (query.hasOwnProperty('count') &&
query.count === 'true') {
var only = getFindCriteria(coll.name,query);
coll.count(only,function(err,docs) {
writeMeta(response,200,url,docs);
response.end('\n');
});
} else {
// Handle paging
if (query.hasOwnProperty('page')) {
var only = getFindCriteria(coll.name,query);
var fields = getFindFields(coll.name,query);
coll.find(only,fields,function(err,docs) {findHandler(err,docs,response, url)})
.skip(PAGE_SIZE*query.page)
.limit(PAGE_SIZE);
} else {
// Query Mongo
var only = getFindCriteria(coll.name,query);
var fields = getFindFields(coll.name,query);
coll.find(only,fields,function(err,docs) {findHandler(err,docs,response, url)});
}
}
}
}
// API ENDPOINT
app.router.get('/force',function() {
// Setup
var self = this;
var method = 'GET';
var url = '/force';
var body = self.req.body;
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
winston.info(util.inspect(shlock));
// Check for query string
var query = self.req.query;
if (!isQueryValid(query)) {
writeMeta(self.res,400,url,0);
self.res.end('\n');
} else {
// Handle count=true
if (query.hasOwnProperty('count') &&
query.count === 'true') {
var only = getFindCriteria('force',query);
force.count(only,function(err,docs) {
writeMeta(self.res,200,url,docs);
self.res.end('\n');
});
} else {
// Handle paging
if (query.hasOwnProperty('page')) {
var only = getFindCriteria('force',query);
var fields = getFindFields('force',query);
force.find(only,fields,function(err,docs) {findHandler(err,docs,self.res, url)})
.skip(PAGE_SIZE*query.page)
.limit(PAGE_SIZE);
} else {
// Query Mongo
var only = getFindCriteria('force',query);
var fields = getFindFields('force',query);
force.find(only,fields,function(err,docs) {findHandler(err,docs,self.res, url)});
}
}
}
});
// API ENDPOINT
app.router.get('/pulse',function() {
// Setup
var self = this;
var method = 'GET';
var url = '/pulse';
var body = self.req.body;
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
winston.info(util.inspect(shlock));
// Query Mongo
pulse.find(function(err,docs) {findHandler(err,docs,self.res, url)});
});
// API ENDPOINT
app.router.get('/shlocks',function() {
// Setup
var self = this;
var method = 'GET';
var url = '/shlocks';
var body = self.req.body;
// Create metrics data.
var shlock = new Shlock('api', method, url);
shlock.body = body;
winston.info(util.inspect(shlock));
getHandler(self.req,self.res,url,shlocks);
});
// API ENDPOINT
app.router.post('/shlocks',function() {
var self = this;
var body = self.req.body;
postHandler('/shlocks', body, self.res, shlocks);
});
// API ENDPOINT
app.router.post('/force',function() {
var self = this;
var body = self.req.body;
postHandler('/force', body, self.res, shlocks);
});
// API ENDPOINT
// Takes req.body and saves it into the audit collection.
//
app.router.post('/audit',function () {
var self = this;
var body = self.req.body;
postHandler('/audit', body, self.res, audit);
});
// API ENDPOINT
// Takes radius and color from req.body and emits event to update a d3 circle.
//
app.router.post('/pulse',function(){
var self = this;
var body = self.req.body;
postHandler('/pulse', body, self.res, pulse);
// Update circles on clients.
var circle = new Circle(body.r,body.fill);
io.sockets.emit('pulse', circle);
});
winston.info('Running on port: '+v.API_PORT+' in NODE_ENV: '+ v.NODE_ENV + ' on ' + v.LOCATION );
app.start(v.API_PORT);
function Circle(r, fill) {
this.r = r;
this.fill = fill;
};
// Display all configured API endpoints, aka routes.
winston.info('route',util.inspect(app.router.routes));