summaryrefslogtreecommitdiff
path: root/src/node/hooks/express/apicalls.js
blob: 7f2f8ecf709a239b118e03cc48618a39551913cc (plain)
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
var log4js = require('log4js');
var apiLogger = log4js.getLogger("API");
var clientLogger = log4js.getLogger("client");
var formidable = require('formidable');
var apiHandler = require('../../handler/APIHandler');

//This is for making an api call, collecting all post information and passing it to the apiHandler
var apiCaller = function(req, res, fields) {
  res.header("Content-Type", "application/json; charset=utf-8");

  apiLogger.info("REQUEST, v"+ req.params.version + ":" + req.params.func + ", " + JSON.stringify(fields));

  //wrap the send function so we can log the response
  //note: res._send seems to be already in use, so better use a "unique" name
  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 && isVarName(response))
      response = req.query.jsonp + "(" + response + ")";

    res._____send(response);
  }

  //call the api handler
  apiHandler.handle(req.params.version, req.params.func, fields, req, res);
}

exports.apiCaller = apiCaller;

exports.expressCreateServer = function (hook_name, args, cb) {
  //This is a api GET call, collect all post informations and pass it to the apiHandler
  args.app.get('/api/:version/:func', function (req, res) {
    apiCaller(req, res, req.query)
  });

  //This is a api POST call, collect all post informations and pass it to the apiHandler
  args.app.post('/api/:version/:func', function(req, res) {
    new formidable.IncomingForm().parse(req, function (err, fields, files) {
      apiCaller(req, res, fields)
    });
  });

  //The Etherpad client side sends information about how a disconnect happened
  args.app.post('/ep/pad/connection-diagnostic-info', function(req, res) {
    new formidable.IncomingForm().parse(req, function(err, fields, files) { 
      clientLogger.info("DIAGNOSTIC-INFO: " + fields.diagnosticInfo);
      res.end("OK");
    });
  });

  //The Etherpad client side sends information about client side javscript errors
  args.app.post('/jserror', function(req, res) {
    new formidable.IncomingForm().parse(req, function(err, fields, files) { 
      try {
        var data = JSON.parse(fields.errorInfo)
      }catch(e){
        return res.end()
      }
      clientLogger.warn(data.msg+' --', data);
      res.end("OK");
    });
  });
  
  //Provide a possibility to query the latest available API version
  args.app.get('/api', function (req, res) {
     res.json({"currentVersion" : apiHandler.latestApiVersion});
  });
}