factored out API caller and client feedback combinators

This commit is contained in:
Montana Scott Rowe 2012-01-19 14:42:40 -06:00
parent e703fd1ba0
commit b0d23cb952

View file

@ -355,6 +355,42 @@ function postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, im
}); });
} }
} }
function apiCallerCombinator(serverName, apiLogger, apiHandler){
return function apiCaller(req, res, fields){
res.header("Server", serverName);
res.header("Content-Type", "application/json; charset=utf-8");
apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields));
//wrap the send function so we can log the response
res._send = res.send;
res.send = function(response)
{
response = JSON.stringify(response);
apiLogger.info("RESPONSE, " + req.params.func + ", " + response);
//is this a jsonp call, if yes, add the function call
if(req.query.jsonp)
response = req.query.jsonp + "(" + response + ")";
res._send(response);
}
//call the api handler
apiHandler.handle(req.params.func, fields, req, res);
}
}
function logOkPostCombinator(prefix, consoleFn, field){
return function(req, res){
new formidable.IncomingForm().parse(
req,
function(err, fields, files){
console[consoleFn](prefix + ": " + fields[field]);
res.end("OK");
}
);
}
}
async.waterfall([ async.waterfall([
//initalize the database //initalize the database
@ -476,30 +512,7 @@ async.waterfall([
var apiLogger = log4js.getLogger("API"); var apiLogger = log4js.getLogger("API");
//This is for making an api call, collecting all post information and passing it to the apiHandler //This is for making an api call, collecting all post information and passing it to the apiHandler
var apiCaller = function(req, res, fields) var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler);
{
res.header("Server", serverName);
res.header("Content-Type", "application/json; charset=utf-8");
apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields));
//wrap the send function so we can log the response
res._send = res.send;
res.send = function(response)
{
response = JSON.stringify(response);
apiLogger.info("RESPONSE, " + req.params.func + ", " + response);
//is this a jsonp call, if yes, add the function call
if(req.query.jsonp)
response = req.query.jsonp + "(" + response + ")";
res._send(response);
}
//call the api handler
apiHandler.handle(req.params.func, fields, req, res);
}
//This is a api GET call, collect all post informations and pass it to the apiHandler //This is a api GET call, collect all post informations and pass it to the apiHandler
app.get('/api/1/:func', function(req, res) app.get('/api/1/:func', function(req, res)
@ -517,45 +530,21 @@ async.waterfall([
}); });
//The Etherpad client side sends information about how a disconnect happen //The Etherpad client side sends information about how a disconnect happen
app.post('/ep/pad/connection-diagnostic-info', function(req, res) app.post('/ep/pad/connection-diagnostic-info', logOkPostCombinator("DIAGNOSTIC-INFO", "log", "diagnosticInfo"));
{
new formidable.IncomingForm().parse(req, function(err, fields, files)
{
console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo);
res.end("OK");
});
});
//The Etherpad client side sends information about client side javscript errors //The Etherpad client side sends information about client side javscript errors
app.post('/jserror', function(req, res) app.post('/jserror', logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo"));
{
new formidable.IncomingForm().parse(req, function(err, fields, files)
{
console.error("CLIENT SIDE JAVASCRIPT ERROR: " + fields.errorInfo);
res.end("OK");
});
});
//serve index.html under / //serve index.html under /
app.get('/', function(req, res) app.get('/', function(req, res)
{ {
return sendStatic(path, res, "index.html"); return sendStatic(path, res, "index.html");
/*
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/index.html");
res.sendfile(filePath, { maxAge: exports.maxAge });
*/
}); });
//serve robots.txt //serve robots.txt
app.get('/robots.txt', function(req, res) app.get('/robots.txt', function(req, res)
{ {
return sendStatic(path, res, "robots.txt"); return sendStatic(path, res, "robots.txt");
/*
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/robots.txt");
res.sendfile(filePath, { maxAge: exports.maxAge });
*/
}); });
//serve favicon.ico //serve favicon.ico
@ -563,12 +552,6 @@ async.waterfall([
{ {
return sendStatic(path, res, "custom/favicon.ico", return sendStatic(path, res, "custom/favicon.ico",
function(err){ function(err){
/*
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/custom/favicon.ico");
res.sendfile(filePath, { maxAge: exports.maxAge }, function(err)
{
*/
//there is no custom favicon, send the default favicon //there is no custom favicon, send the default favicon
if(err) if(err)
{ {