server.js: use jshint

This commit is contained in:
booo 2011-12-22 11:53:19 +01:00
parent 11746d974b
commit 224e82d8b0

View file

@ -1,6 +1,6 @@
/** /**
* This module is started with bin/run.sh. It sets up a Express HTTP and a Socket.IO Server. * This module is started with bin/run.sh. It sets up a Express HTTP and a Socket.IO Server.
* Static file Requests are answered directly from this module, Socket.IO messages are passed * Static file Requests are answered directly from this module, Socket.IO messages are passed
* to MessageHandler and minfied requests are passed to minified. * to MessageHandler and minfied requests are passed to minified.
*/ */
@ -51,12 +51,12 @@ try
version = version.substring(0, 7); version = version.substring(0, 7);
console.log("Your Etherpad Lite git version is " + version); console.log("Your Etherpad Lite git version is " + version);
} }
catch(e) catch(e)
{ {
console.warn("Can't get git version for server header\n" + e.message) console.warn("Can't get git version for server header\n" + e.message);
} }
console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues") console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues");
var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)"; var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)";
@ -77,7 +77,7 @@ async.waterfall([
{ {
//create server //create server
var app = express.createServer(); var app = express.createServer();
//load modules that needs a initalized db //load modules that needs a initalized db
readOnlyManager = require("./db/ReadOnlyManager"); readOnlyManager = require("./db/ReadOnlyManager");
exporthtml = require("./utils/ExportHtml"); exporthtml = require("./utils/ExportHtml");
@ -87,43 +87,46 @@ async.waterfall([
padManager = require('./db/PadManager'); padManager = require('./db/PadManager');
securityManager = require('./db/SecurityManager'); securityManager = require('./db/SecurityManager');
socketIORouter = require("./handler/SocketIORouter"); socketIORouter = require("./handler/SocketIORouter");
//install logging //install logging
var httpLogger = log4js.getLogger("http"); var httpLogger = log4js.getLogger("http");
app.configure(function() app.configure(function()
{ {
// Activate http basic auth if it has been defined in settings.json // Activate http basic auth if it has been defined in settings.json
if(settings.httpAuth != null) app.use(basic_auth); if(settings.httpAuth) {
app.use(basic_auth);
}
// If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158. // If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158.
// Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway. // Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway.
if (!(settings.loglevel === "WARN" || settings.loglevel == "ERROR")) if (!(settings.loglevel === "WARN" || settings.loglevel == "ERROR")) {
app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'})); app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'}));
}
app.use(express.cookieParser()); app.use(express.cookieParser());
}); });
app.error(function(err, req, res, next){ app.error(function(err, req, res, next){
res.send(500); res.send(500);
console.error(err.stack ? err.stack : err.toString()); console.error(err.stack ? err.stack : err.toString());
gracefulShutdown(); gracefulShutdown();
}); });
//serve static files //serve static files
app.get('/static/*', function(req, res) app.get('/static/*', function(req, res)
{ {
res.header("Server", serverName); res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/.." + var filePath = path.normalize(__dirname + "/.." +
req.url.replace(/\.\./g, '').split("?")[0]); req.url.replace(/\.\./g, '').split("?")[0]);
res.sendfile(filePath, { maxAge: exports.maxAge }); res.sendfile(filePath, { maxAge: exports.maxAge });
}); });
//serve minified files //serve minified files
app.get('/minified/:id', function(req, res, next) app.get('/minified/:id', function(req, res, next)
{ {
res.header("Server", serverName); res.header("Server", serverName);
var id = req.params.id; var id = req.params.id;
if(id == "pad.js" || id == "timeslider.js") if(id == "pad.js" || id == "timeslider.js")
{ {
minify.minifyJS(req,res,id); minify.minifyJS(req,res,id);
@ -133,14 +136,15 @@ async.waterfall([
next(); next();
} }
}); });
//checks for padAccess //checks for padAccess
function hasPadAccess(req, res, callback) function hasPadAccess(req, res, callback)
{ {
console.log(req.params);
securityManager.checkAccess(req.params.pad, req.cookies.sessionid, req.cookies.token, req.cookies.password, function(err, accessObj) securityManager.checkAccess(req.params.pad, req.cookies.sessionid, req.cookies.token, req.cookies.password, function(err, accessObj)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//there is access, continue //there is access, continue
if(accessObj.accessStatus == "grant") if(accessObj.accessStatus == "grant")
{ {
@ -163,7 +167,7 @@ async.waterfall([
return; return;
} }
} }
res.header('WWW-Authenticate', 'Basic realm="Protected Area"'); res.header('WWW-Authenticate', 'Basic realm="Protected Area"');
if (req.headers.authorization) { if (req.headers.authorization) {
setTimeout(function () { setTimeout(function () {
@ -173,16 +177,16 @@ async.waterfall([
res.send('Authentication required', 401); res.send('Authentication required', 401);
} }
} }
//serve read only pad //serve read only pad
app.get('/ro/:id', function(req, res) app.get('/ro/:id', function(req, res)
{ {
res.header("Server", serverName); res.header("Server", serverName);
var html; var html;
var padId; var padId;
var pad; var pad;
async.series([ async.series([
//translate the read only pad to a padId //translate the read only pad to a padId
function(callback) function(callback)
@ -190,12 +194,12 @@ async.waterfall([
readOnlyManager.getPadId(req.params.id, function(err, _padId) readOnlyManager.getPadId(req.params.id, function(err, _padId)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
padId = _padId; padId = _padId;
//we need that to tell hasPadAcess about the pad //we need that to tell hasPadAcess about the pad
req.params.pad = padId; req.params.pad = padId;
callback(); callback();
}); });
}, },
@ -203,12 +207,12 @@ async.waterfall([
function(callback) function(callback)
{ {
//return if the there is no padId //return if the there is no padId
if(padId == null) if(!padId)
{ {
callback("notfound"); callback("notfound");
return; return;
} }
hasPadAccess(req, res, function() hasPadAccess(req, res, function()
{ {
//render the html document //render the html document
@ -225,29 +229,29 @@ async.waterfall([
//throw any unexpected error //throw any unexpected error
if(err && err != "notfound") if(err && err != "notfound")
ERR(err); ERR(err);
if(err == "notfound") if(err == "notfound")
res.send('404 - Not Found', 404); res.send('404 - Not Found', 404);
else else
res.send(html); res.send(html);
}); });
}); });
//serve pad.html under /p //serve pad.html under /p
app.get('/p/:pad', function(req, res, next) app.get('/p/:pad', function(req, res, next)
{ {
//ensure the padname is valid and the url doesn't end with a / //ensure the padname is valid and the url doesn't end with a /
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url)) if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
{ {
res.send('Such a padname is forbidden', 404); res.send('Such a padname is forbidden', 404);
return; return;
} }
res.header("Server", serverName); res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/pad.html"); var filePath = path.normalize(__dirname + "/../static/pad.html");
res.sendfile(filePath, { maxAge: exports.maxAge }); res.sendfile(filePath, { maxAge: exports.maxAge });
}); });
//serve timeslider.html under /p/$padname/timeslider //serve timeslider.html under /p/$padname/timeslider
app.get('/p/:pad/timeslider', function(req, res, next) app.get('/p/:pad/timeslider', function(req, res, next)
{ {
@ -257,12 +261,12 @@ async.waterfall([
res.send('Such a padname is forbidden', 404); res.send('Such a padname is forbidden', 404);
return; return;
} }
res.header("Server", serverName); res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/timeslider.html"); var filePath = path.normalize(__dirname + "/../static/timeslider.html");
res.sendfile(filePath, { maxAge: exports.maxAge }); res.sendfile(filePath, { maxAge: exports.maxAge });
}); });
//serve timeslider.html under /p/$padname/timeslider //serve timeslider.html under /p/$padname/timeslider
app.get('/p/:pad/export/:type', function(req, res, next) app.get('/p/:pad/export/:type', function(req, res, next)
{ {
@ -272,7 +276,7 @@ async.waterfall([
res.send('Such a padname is forbidden', 404); res.send('Such a padname is forbidden', 404);
return; return;
} }
var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"]; var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"];
//send a 404 if we don't support this filetype //send a 404 if we don't support this filetype
if(types.indexOf(req.params.type) == -1) if(types.indexOf(req.params.type) == -1)
@ -280,24 +284,23 @@ async.waterfall([
next(); next();
return; return;
} }
//if abiword is disabled, and this is a format we only support with abiword, output a message //if abiword is disabled, and this is a format we only support with abiword, output a message
if(settings.abiword == null && if(!settings.abiword && ["odt", "pdf", "doc"].indexOf(req.params.type) !== -1)
["odt", "pdf", "doc"].indexOf(req.params.type) !== -1)
{ {
res.send("Abiword is not enabled at this Etherpad Lite instance. Set the path to Abiword in settings.json to enable this feature"); res.send("Abiword is not enabled at this Etherpad Lite instance. Set the path to Abiword in settings.json to enable this feature");
return; return;
} }
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Server", serverName); res.header("Server", serverName);
hasPadAccess(req, res, function() hasPadAccess(req, res, function()
{ {
exportHandler.doExport(req, res, req.params.pad, req.params.type); exportHandler.doExport(req, res, req.params.pad, req.params.type);
}); });
}); });
//handle import requests //handle import requests
app.post('/p/:pad/import', function(req, res, next) app.post('/p/:pad/import', function(req, res, next)
{ {
@ -307,84 +310,84 @@ async.waterfall([
res.send('Such a padname is forbidden', 404); res.send('Such a padname is forbidden', 404);
return; return;
} }
//if abiword is disabled, skip handling this request //if abiword is disabled, skip handling this request
if(settings.abiword == null) if(!settings.abiword)
{ {
next(); next();
return; return;
} }
res.header("Server", serverName); res.header("Server", serverName);
hasPadAccess(req, res, function() hasPadAccess(req, res, function()
{ {
importHandler.doImport(req, res, req.params.pad); importHandler.doImport(req, res, req.params.pad);
}); });
}); });
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 = function(req, res, fields) {
res.header("Server", serverName); res.header("Server", serverName);
res.header("Content-Type", "application/json; charset=utf-8"); res.header("Content-Type", "application/json; charset=utf-8");
apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields)); apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields));
//wrap the send function so we can log the response //wrap the send function so we can log the response
res._send = res.send; res._send = res.send;
res.send = function(response) res.send = function(response)
{ {
response = JSON.stringify(response); response = JSON.stringify(response);
apiLogger.info("RESPONSE, " + req.params.func + ", " + response); apiLogger.info("RESPONSE, " + req.params.func + ", " + response);
//is this a jsonp call, if yes, add the function call //is this a jsonp call, if yes, add the function call
if(req.query.jsonp) if(req.query.jsonp)
response = req.query.jsonp + "(" + response + ")"; response = req.query.jsonp + "(" + response + ")";
res._send(response); res._send(response);
} };
//call the api handler //call the api handler
apiHandler.handle(req.params.func, fields, req, res); 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)
{ {
apiCaller(req, res, req.query) apiCaller(req, res, req.query);
}); });
//This is a api POST call, collect all post informations and pass it to the apiHandler //This is a api POST call, collect all post informations and pass it to the apiHandler
app.post('/api/1/:func', function(req, res) app.post('/api/1/:func', function(req, res)
{ {
new formidable.IncomingForm().parse(req, function(err, fields, files) new formidable.IncomingForm().parse(req, function(err, fields, files)
{ {
apiCaller(req, res, fields) apiCaller(req, res, fields);
}); });
}); });
//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', function(req, res)
{ {
new formidable.IncomingForm().parse(req, function(err, fields, files) new formidable.IncomingForm().parse(req, function(err, fields, files)
{ {
console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo); console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo);
res.end("OK"); 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', function(req, res)
{ {
new formidable.IncomingForm().parse(req, function(err, fields, files) new formidable.IncomingForm().parse(req, function(err, fields, files)
{ {
console.error("CLIENT SIDE JAVASCRIPT ERROR: " + fields.errorInfo); console.error("CLIENT SIDE JAVASCRIPT ERROR: " + fields.errorInfo);
res.end("OK"); res.end("OK");
}); });
}); });
//serve index.html under / //serve index.html under /
app.get('/', function(req, res) app.get('/', function(req, res)
{ {
@ -392,7 +395,7 @@ async.waterfall([
var filePath = path.normalize(__dirname + "/../static/index.html"); var filePath = path.normalize(__dirname + "/../static/index.html");
res.sendfile(filePath, { maxAge: exports.maxAge }); 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)
{ {
@ -400,7 +403,7 @@ async.waterfall([
var filePath = path.normalize(__dirname + "/../static/robots.txt"); var filePath = path.normalize(__dirname + "/../static/robots.txt");
res.sendfile(filePath, { maxAge: exports.maxAge }); res.sendfile(filePath, { maxAge: exports.maxAge });
}); });
//serve favicon.ico //serve favicon.ico
app.get('/favicon.ico', function(req, res) app.get('/favicon.ico', function(req, res)
{ {
@ -416,7 +419,7 @@ async.waterfall([
} }
}); });
}); });
//let the server listen //let the server listen
app.listen(settings.port, settings.ip); app.listen(settings.port, settings.ip);
console.log("Server is listening at " + settings.ip + ":" + settings.port); console.log("Server is listening at " + settings.ip + ":" + settings.port);
@ -432,13 +435,13 @@ async.waterfall([
{ {
console.error(err); console.error(err);
} }
//ensure there is only one graceful shutdown running //ensure there is only one graceful shutdown running
if(onShutdown) return; if(onShutdown) return;
onShutdown = true; onShutdown = true;
console.log("graceful shutdown..."); console.log("graceful shutdown...");
//stop the http server //stop the http server
app.close(); app.close();
@ -446,14 +449,14 @@ async.waterfall([
db.db.doShutdown(function() db.db.doShutdown(function()
{ {
console.log("db sucessfully closed."); console.log("db sucessfully closed.");
process.exit(0); process.exit(0);
}); });
setTimeout(function(){ setTimeout(function(){
process.exit(1); process.exit(1);
}, 3000); }, 3000);
} };
//connect graceful shutdown with sigint and uncaughtexception //connect graceful shutdown with sigint and uncaughtexception
if(os.type().indexOf("Windows") == -1) if(os.type().indexOf("Windows") == -1)
@ -462,22 +465,22 @@ async.waterfall([
//https://github.com/joyent/node/issues/1553 //https://github.com/joyent/node/issues/1553
process.on('SIGINT', gracefulShutdown); process.on('SIGINT', gracefulShutdown);
} }
process.on('uncaughtException', gracefulShutdown); process.on('uncaughtException', gracefulShutdown);
//init socket.io and redirect all requests to the MessageHandler //init socket.io and redirect all requests to the MessageHandler
var io = socketio.listen(app); var io = socketio.listen(app);
//this is only a workaround to ensure it works with all browers behind a proxy //this is only a workaround to ensure it works with all browers behind a proxy
//we should remove this when the new socket.io version is more stable //we should remove this when the new socket.io version is more stable
io.set('transports', ['xhr-polling']); io.set('transports', ['xhr-polling']);
var socketIOLogger = log4js.getLogger("socket.io"); var socketIOLogger = log4js.getLogger("socket.io");
io.set('logger', { io.set('logger', {
debug: function (str) debug: function (str)
{ {
socketIOLogger.debug.apply(socketIOLogger, arguments); socketIOLogger.debug.apply(socketIOLogger, arguments);
}, },
info: function (str) info: function (str)
{ {
socketIOLogger.info.apply(socketIOLogger, arguments); socketIOLogger.info.apply(socketIOLogger, arguments);
@ -489,21 +492,21 @@ async.waterfall([
error: function (str) error: function (str)
{ {
socketIOLogger.error.apply(socketIOLogger, arguments); socketIOLogger.error.apply(socketIOLogger, arguments);
}, }
}); });
//minify socket.io javascript //minify socket.io javascript
if(settings.minify) if(settings.minify)
io.enable('browser client minification'); io.enable('browser client minification');
var padMessageHandler = require("./handler/PadMessageHandler"); var padMessageHandler = require("./handler/PadMessageHandler");
var timesliderMessageHandler = require("./handler/TimesliderMessageHandler"); var timesliderMessageHandler = require("./handler/TimesliderMessageHandler");
//Initalize the Socket.IO Router //Initalize the Socket.IO Router
socketIORouter.setSocketIO(io); socketIORouter.setSocketIO(io);
socketIORouter.addComponent("pad", padMessageHandler); socketIORouter.addComponent("pad", padMessageHandler);
socketIORouter.addComponent("timeslider", timesliderMessageHandler); socketIORouter.addComponent("timeslider", timesliderMessageHandler);
callback(null); callback(null);
} }
]); ]);