-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbatchApi.js
94 lines (80 loc) · 1.96 KB
/
batchApi.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
#!/usr/bin/env node
/**
* Handles the incoming requests, calls the parsing and processing functions, sends response
*/
var async = require('async'),
bp = require('./lib/batchProcessor'),
config = require('./config.json'),
express = require('express'),
dependencyGraph = require('./lib/dependencyGraph'),
request = require('request'),
jsonpath = require('JSONPath').eval,
logger = require('./lib/logger');
var app = express(),
log = logger.getLogger(config.logger);
/**
* Expose configure
*
* Standard configuration.
*
* @return app (configured express object)
*/
exports.configure = function() {
app.configure(function() {
app.use(express.compress());
app.use(express.bodyParser());
});
app.configure('development', function() {
app.use(express.logger({stream:log.winstonStream}));
app.use(express.errorHandler({
dumpExceptions: true,
showStack : true
}));
});
app.configure('production', function() {
app.use(express.errorHandler({
dumpExceptions: false,
showStack: false
}));
});
return app;
}
/**
* Expose processBatchRequest
*
* This function processes a batch request, using a given express instance
*
* @param {Object} batchApp
*
*/
exports.processBatchRequest = function(batchApp) {
batchApp.all('*',
dependencyGraph.getGraph,
bp.processBatch,
sendResponse
);
}
/**
* Expose sendResponse
*
* This function sends the response of all apiCall objects back to the client, wrapped into one JSON object.
*
* @param {Object} req
* @param {Object} res
*
*/
exports.sendResponse = sendResponse = function(req, res) {
var response = [];
req.body.map(function(apiRequest) {
if (apiRequest.setNull) {
response.push(null);
} else {
response.push({
body: apiRequest.bodyResponse,
headers: apiRequest.headers,
statusCode: apiRequest.statusCode
});
}
});
res.send(JSON.stringify(response));
}