mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-05 06:37:10 -04:00
move 'api' routes into own module
This commit is contained in:
parent
b8c0bb2ea2
commit
7d12ca2076
2 changed files with 50 additions and 43 deletions
48
node/routes/api.js
Normal file
48
node/routes/api.js
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
var formidable = require('formidable');
|
||||||
|
var log4js = require('log4js');
|
||||||
|
|
||||||
|
module.exports = function(app)
|
||||||
|
{
|
||||||
|
var apiLogger = log4js.getLogger("API");
|
||||||
|
|
||||||
|
//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, " + 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
|
||||||
|
app.apiHandler.handle(req.params.func, fields, req, res);
|
||||||
|
};
|
||||||
|
|
||||||
|
//This is a api GET call, collect all post informations and pass it to the apiHandler
|
||||||
|
app.get('/api/1/: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
|
||||||
|
app.post('/api/1/:func', function(req, res)
|
||||||
|
{
|
||||||
|
new formidable.IncomingForm().parse(req, function(err, fields, files)
|
||||||
|
{
|
||||||
|
apiCaller(req, res, fields);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
|
@ -32,7 +32,6 @@ var express = require('express');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var minify = require('./utils/Minify');
|
var minify = require('./utils/Minify');
|
||||||
var formidable = require('formidable');
|
var formidable = require('formidable');
|
||||||
var apiHandler;
|
|
||||||
var exportHandler;
|
var exportHandler;
|
||||||
var importHandler;
|
var importHandler;
|
||||||
var exporthtml;
|
var exporthtml;
|
||||||
|
@ -118,7 +117,7 @@ async.waterfall([
|
||||||
exporthtml = require("./utils/ExportHtml");
|
exporthtml = require("./utils/ExportHtml");
|
||||||
exportHandler = require('./handler/ExportHandler');
|
exportHandler = require('./handler/ExportHandler');
|
||||||
importHandler = require('./handler/ImportHandler');
|
importHandler = require('./handler/ImportHandler');
|
||||||
apiHandler = require('./handler/APIHandler');
|
app.apiHandler = require('./handler/APIHandler');
|
||||||
padManager = require('./db/PadManager');
|
padManager = require('./db/PadManager');
|
||||||
securityManager = require('./db/SecurityManager');
|
securityManager = require('./db/SecurityManager');
|
||||||
socketIORouter = require("./handler/SocketIORouter");
|
socketIORouter = require("./handler/SocketIORouter");
|
||||||
|
@ -293,48 +292,8 @@ async.waterfall([
|
||||||
importHandler.doImport(req, res, req.params.pad);
|
importHandler.doImport(req, res, req.params.pad);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var apiLogger = log4js.getLogger("API");
|
|
||||||
|
|
||||||
//This is for making an api call, collecting all post information and passing it to the apiHandler
|
require('./routes/api')(app);
|
||||||
var apiCaller = function(req, res, fields)
|
|
||||||
{
|
|
||||||
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
|
|
||||||
app.get('/api/1/: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
|
|
||||||
app.post('/api/1/:func', function(req, res)
|
|
||||||
{
|
|
||||||
new formidable.IncomingForm().parse(req, function(err, fields, files)
|
|
||||||
{
|
|
||||||
apiCaller(req, res, fields)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
require('./routes/debug')(app);
|
require('./routes/debug')(app);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue