2011-03-26 14:52:56 +00:00
|
|
|
/**
|
2011-05-30 15:53:11 +01:00
|
|
|
* 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
|
|
|
|
* to MessageHandler and minfied requests are passed to minified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-03-26 14:52:56 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-12-04 16:50:02 +01:00
|
|
|
var ERR = require("async-stacktrace");
|
2011-08-03 19:31:25 +01:00
|
|
|
var log4js = require('log4js');
|
2011-08-18 21:29:34 +01:00
|
|
|
var os = require("os");
|
2011-05-28 18:09:17 +01:00
|
|
|
var socketio = require('socket.io');
|
2011-06-30 20:03:09 +01:00
|
|
|
var fs = require('fs');
|
2011-07-27 18:52:23 +01:00
|
|
|
var settings = require('./utils/Settings');
|
|
|
|
var db = require('./db/DB');
|
2011-05-19 17:36:26 +01:00
|
|
|
var async = require('async');
|
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
2011-07-27 18:52:23 +01:00
|
|
|
var minify = require('./utils/Minify');
|
2011-07-25 16:31:41 +01:00
|
|
|
var formidable = require('formidable');
|
2011-08-03 19:31:25 +01:00
|
|
|
var apiHandler;
|
2011-07-19 19:48:11 +01:00
|
|
|
var exportHandler;
|
2011-07-21 20:13:58 +01:00
|
|
|
var importHandler;
|
2011-07-08 18:33:01 +01:00
|
|
|
var exporthtml;
|
|
|
|
var readOnlyManager;
|
2011-08-04 16:07:58 +01:00
|
|
|
var padManager;
|
2011-08-15 22:20:37 +01:00
|
|
|
var securityManager;
|
2011-08-16 15:53:09 +01:00
|
|
|
var socketIORouter;
|
2011-05-19 17:36:26 +01:00
|
|
|
|
2011-06-30 20:03:09 +01:00
|
|
|
//try to get the git version
|
|
|
|
var version = "";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var ref = fs.readFileSync("../.git/HEAD", "utf-8");
|
|
|
|
var refPath = "../.git/" + ref.substring(5, ref.indexOf("\n"));
|
|
|
|
version = fs.readFileSync(refPath, "utf-8");
|
2011-08-19 22:01:33 +01:00
|
|
|
version = version.substring(0, 7);
|
|
|
|
console.log("Your Etherpad Lite git version is " + version);
|
2011-06-30 20:03:09 +01:00
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
2011-07-31 18:25:51 +01:00
|
|
|
console.warn("Can't get git version for server header\n" + e.message)
|
2011-06-30 20:03:09 +01:00
|
|
|
}
|
|
|
|
|
2011-08-21 19:52:24 +01:00
|
|
|
console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues")
|
|
|
|
|
2011-06-30 20:03:09 +01:00
|
|
|
var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)";
|
|
|
|
|
2011-07-21 20:13:58 +01:00
|
|
|
//cache 6 hours
|
2011-06-30 12:43:11 +01:00
|
|
|
exports.maxAge = 1000*60*60*6;
|
2011-05-19 17:36:26 +01:00
|
|
|
|
2011-08-17 17:45:47 +01:00
|
|
|
//set loglevel
|
|
|
|
log4js.setGlobalLogLevel(settings.loglevel);
|
|
|
|
|
2012-01-19 09:42:56 -06:00
|
|
|
function setupDb(callback){
|
|
|
|
db.init(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function padAccessCombinator(securityManager, req, res, callback, errorback){
|
|
|
|
function checkback(err, accessObj){
|
|
|
|
if(err) return errorback(err);
|
|
|
|
if("grant" == accessObj.accessStatus) return callback();
|
|
|
|
return res.send("403 - Can't touch this", 403);
|
|
|
|
}
|
|
|
|
//works great for one session
|
|
|
|
//but what if there are multiple?
|
|
|
|
//if(!("sessionIDs" in req.cookies))
|
|
|
|
return securityManager.checkAccess(
|
|
|
|
req.params.pad,
|
|
|
|
req.cookies.sessionid,
|
|
|
|
req.cookies.token,
|
|
|
|
req.cookies.password,
|
|
|
|
checkback
|
|
|
|
);
|
|
|
|
/*sessIds = JSON.parse(req.cookies.sessionIDs);
|
|
|
|
var tasks = [];
|
|
|
|
function createTask(sid){
|
|
|
|
return function(cb){
|
|
|
|
return securityManager.checkAccess(
|
|
|
|
req.params.pad,
|
|
|
|
sid,
|
|
|
|
req.cookies.token,
|
|
|
|
req.cookies.password,
|
|
|
|
cb//function(err, accessObj){return cb(err, accessObj);}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var i = 0; i < sessIds.length; i++)
|
|
|
|
tasks[i] = createTasks(sessIds[i]);
|
|
|
|
return async.parallel(
|
|
|
|
tasks,
|
|
|
|
function(err, obs){
|
|
|
|
if(err) return errorback(err);
|
|
|
|
for(var i = 0; i < obs.length; i++)
|
|
|
|
if("grant" == obs[i].accessStatus) return callback(null);
|
|
|
|
return res.send("none of those IDs worked", 403);
|
|
|
|
}
|
|
|
|
)*/
|
|
|
|
}
|
|
|
|
function getStatic(req, res){
|
2012-01-19 09:46:06 -06:00
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(
|
|
|
|
__dirname + "/.." +
|
|
|
|
req.url.replace(/\.\./g, '').split("?")[0]
|
|
|
|
);
|
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
|
|
|
}
|
|
|
|
function getMinified(req, res, next)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
|
|
|
if(id == "pad.js" || id == "timeslider.js")
|
|
|
|
{
|
|
|
|
minify.minifyJS(req,res,id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function checkPadName(padManager, req, res, callback){
|
|
|
|
//ensure the padname is valid and the url doesn't end with a /
|
|
|
|
if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url))
|
|
|
|
return res.send("Such a padname is forbidden", 404);
|
|
|
|
return callback();
|
2012-01-19 09:42:56 -06:00
|
|
|
}
|
2012-01-19 10:11:51 -06:00
|
|
|
function setupIo(socketio, log4js, settings, socketIORouter, app){
|
|
|
|
//init socket.io and redirect all requests to the MessageHandler
|
|
|
|
var io = socketio.listen(app);
|
|
|
|
|
|
|
|
//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
|
|
|
|
io.set('transports', ['xhr-polling']);
|
|
|
|
|
|
|
|
var socketIOLogger = log4js.getLogger("socket.io");
|
|
|
|
io.set('logger', {
|
|
|
|
debug: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.debug.apply(socketIOLogger, arguments);
|
|
|
|
},
|
|
|
|
info: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.info.apply(socketIOLogger, arguments);
|
|
|
|
},
|
|
|
|
warn: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.warn.apply(socketIOLogger, arguments);
|
|
|
|
},
|
|
|
|
error: function (str)
|
|
|
|
{
|
|
|
|
socketIOLogger.error.apply(socketIOLogger, arguments);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//minify socket.io javascript
|
|
|
|
if(settings.minify)
|
|
|
|
io.enable('browser client minification');
|
|
|
|
|
|
|
|
var padMessageHandler = require("./handler/PadMessageHandler");
|
|
|
|
var timesliderMessageHandler = require("./handler/TimesliderMessageHandler");
|
|
|
|
|
|
|
|
//Initalize the Socket.IO Router
|
|
|
|
socketIORouter.setSocketIO(io);
|
|
|
|
socketIORouter.addComponent("pad", padMessageHandler);
|
|
|
|
socketIORouter.addComponent("timeslider", timesliderMessageHandler);
|
|
|
|
return io;
|
|
|
|
}
|
2012-01-19 10:14:14 -06:00
|
|
|
function setupShutdown(db, app){
|
|
|
|
var onShutdown = false;
|
|
|
|
var gracefulShutdown = function(err)
|
|
|
|
{
|
|
|
|
if(err && err.stack)
|
|
|
|
{
|
|
|
|
console.error(err.stack);
|
|
|
|
}
|
|
|
|
else if(err)
|
|
|
|
{
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//ensure there is only one graceful shutdown running
|
|
|
|
if(onShutdown) return;
|
|
|
|
onShutdown = true;
|
|
|
|
|
|
|
|
console.log("graceful shutdown...");
|
|
|
|
|
|
|
|
//stop the http server
|
|
|
|
app.close();
|
|
|
|
|
|
|
|
//do the db shutdown
|
|
|
|
db.db.doShutdown(function()
|
|
|
|
{
|
|
|
|
console.log("db sucessfully closed.");
|
|
|
|
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(function(){
|
|
|
|
process.exit(1);
|
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//connect graceful shutdown with sigint and uncaughtexception
|
|
|
|
if(os.type().indexOf("Windows") == -1)
|
|
|
|
{
|
|
|
|
//sigint is so far not working on windows
|
|
|
|
//https://github.com/joyent/node/issues/1553
|
|
|
|
process.on('SIGINT', gracefulShutdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
return process.on('uncaughtException', gracefulShutdown);
|
|
|
|
}
|
2012-01-19 10:44:38 -06:00
|
|
|
function sendStatic(path, res, filename, callback){
|
|
|
|
if("function" != typeof callback) callback = function(){};
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/" + filename);
|
|
|
|
return res.sendfile(filePath, { maxAge: exports.maxAge }, callback);
|
|
|
|
}
|
2012-01-19 11:56:50 -06:00
|
|
|
function translateRoCombinator(managers, req, ERR){
|
2012-01-19 11:55:59 -06:00
|
|
|
return function translateRo(callback){
|
|
|
|
function padBack(err, padId){
|
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
//we need that to tell hasPadAccess about the pad
|
|
|
|
req.params.pad = padId;
|
2012-01-19 13:17:42 -06:00
|
|
|
return callback(err, padId);
|
2012-01-19 11:55:59 -06:00
|
|
|
}
|
2012-01-19 13:17:42 -06:00
|
|
|
return managers.ro.getPadID(req.params.id, padBack);
|
2012-01-19 11:55:59 -06:00
|
|
|
};
|
|
|
|
}
|
2012-01-19 12:52:52 -06:00
|
|
|
function roAsyncOutCombinator(ERR, callback){
|
|
|
|
return function(err, data){
|
|
|
|
if(ERR(err, callback)) return;
|
2012-01-19 13:17:42 -06:00
|
|
|
return callback(err, data);
|
2012-01-19 12:52:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function roAccessbackCombinator(exporthtml, padId, callback){
|
2012-01-19 13:17:42 -06:00
|
|
|
return function accessBack(){
|
|
|
|
return exporthtml.getPadHTMLDocument(padId, null, false, callback);
|
2012-01-19 12:52:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function roRenderCombinator(padAccessp, req, res, exporthtml, ERR){
|
|
|
|
return function(padId, callback){
|
|
|
|
if(null == padId)
|
|
|
|
return callback("notfound");
|
2012-01-19 13:17:42 -06:00
|
|
|
return padAccessp(
|
2012-01-19 12:52:52 -06:00
|
|
|
req, res,
|
|
|
|
roAccessbackCombinator(
|
|
|
|
exporthtml, padId,
|
|
|
|
roAsyncOutCombinator(
|
|
|
|
ERR, callback
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 13:17:42 -06:00
|
|
|
function roBindSendBackCombinator(res){
|
|
|
|
return function(html, callback){
|
|
|
|
res.send(html);
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function roErrBackCombinator(ERR, res){
|
|
|
|
return function(err){
|
|
|
|
if(!err) return
|
|
|
|
if("notfound" == err)
|
|
|
|
return res.send("404 - Not Found", 404);
|
|
|
|
return ERR(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function getRoCombinator(serverName, managers, padAccessp, ERR, exporthtml){
|
|
|
|
return function(req, res){
|
|
|
|
res.header("Server", serverName);
|
|
|
|
return async.waterfall(
|
|
|
|
[
|
|
|
|
translateRoCombinator(managers),
|
|
|
|
roRenderCombinator(padAccessp, req, res, exporthtml, ERR),
|
|
|
|
roBindSendBackCombinator(res)
|
|
|
|
],
|
|
|
|
roErrBackCombinator(ERR, res))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 13:36:50 -06:00
|
|
|
function sendStaticIfPad(goToPad, padManager, path, filename){
|
|
|
|
return function staticSender(req, res, next){
|
|
|
|
return goToPad(
|
|
|
|
req, res,
|
|
|
|
function goBack(){
|
|
|
|
return sendStatic(path, res, filename);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
2012-01-19 14:26:05 -06:00
|
|
|
function getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler, serverName){
|
|
|
|
return function getExportPad(req, res, next){
|
|
|
|
goToPad(req, res, function padback() {
|
2012-01-19 14:07:19 -06:00
|
|
|
var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"];
|
|
|
|
//send a 404 if we don't support this filetype
|
|
|
|
if(types.indexOf(req.params.type) == -1)
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if abiword is disabled, and this is a format we only support with abiword, output a message
|
|
|
|
if(settings.abiword == null &&
|
|
|
|
["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");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
hasPadAccess(req, res, function()
|
|
|
|
{
|
|
|
|
exportHandler.doExport(req, res, req.params.pad, req.params.type);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 14:26:05 -06:00
|
|
|
|
|
|
|
function postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler){
|
|
|
|
return function postImportPad(req, res, next){
|
|
|
|
goToPad(req, res, function padback() {
|
|
|
|
//if abiword is disabled, skip handling this request
|
|
|
|
if(settings.abiword == null)
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
hasPadAccess(req, res, function()
|
|
|
|
{
|
|
|
|
importHandler.doImport(req, res, req.params.pad);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 14:42:40 -06:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 14:26:05 -06:00
|
|
|
|
2012-01-19 16:29:04 -06:00
|
|
|
function configureCombinator(app, settings, basic_auth, log4js, httpLogger){
|
|
|
|
return function configBack()
|
|
|
|
{
|
|
|
|
// Activate http basic auth if it has been defined in settings.json
|
|
|
|
if(settings.httpAuth != null) 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.
|
|
|
|
// 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"))
|
|
|
|
app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'}));
|
|
|
|
app.use(express.cookieParser());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//checks for basic http auth
|
|
|
|
function basic_auth (req, res, next) {
|
|
|
|
if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) {
|
|
|
|
// fetch login and password
|
|
|
|
if (new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString() == settings.httpAuth) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.header('WWW-Authenticate', 'Basic realm="Protected Area"');
|
|
|
|
if (req.headers.authorization) {
|
|
|
|
setTimeout(function () {
|
|
|
|
res.send('Authentication required', 401);
|
|
|
|
}, 1000);
|
|
|
|
} else {
|
|
|
|
res.send('Authentication required', 401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:48:24 -06:00
|
|
|
function gotoPadCombinator(checkPadName, padManager){
|
|
|
|
return function gotoPad(req, res, render){
|
|
|
|
return checkPadName(
|
|
|
|
padManager, req, res,
|
|
|
|
function callback(){
|
|
|
|
padManager.sanitizePadId(req.params.pad, function(padId) {
|
|
|
|
//the pad id was sanitized, so we redirect to the sanitized version
|
|
|
|
if(padId != req.params.pad)
|
|
|
|
{
|
|
|
|
var real_path = req.path.replace(/^\/p\/[^\/]+/, '/p/' + padId);
|
|
|
|
res.header('Location', real_path);
|
|
|
|
res.send('You should be redirected to <a href="' + real_path + '">' + real_path + '</a>', 302);
|
|
|
|
}
|
|
|
|
//the pad id was fine, so just render it
|
|
|
|
else
|
|
|
|
{
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:55:39 -06:00
|
|
|
function init(additionalSetup){
|
2012-01-19 17:36:56 -06:00
|
|
|
if("function" != typeof additionalSetup)
|
|
|
|
additionalSetup = function(){};
|
2012-01-19 16:55:39 -06:00
|
|
|
async.waterfall([
|
2011-05-19 17:36:26 +01:00
|
|
|
//initalize the database
|
2012-01-19 09:42:56 -06:00
|
|
|
setupDb,
|
2011-05-19 17:36:26 +01:00
|
|
|
//initalize the http server
|
2011-05-14 18:57:07 +01:00
|
|
|
function (callback)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2011-05-19 17:36:26 +01:00
|
|
|
//create server
|
|
|
|
var app = express.createServer();
|
2012-01-19 17:28:45 -06:00
|
|
|
|
|
|
|
|
2011-07-08 18:33:01 +01:00
|
|
|
//load modules that needs a initalized db
|
2011-07-27 18:52:23 +01:00
|
|
|
readOnlyManager = require("./db/ReadOnlyManager");
|
|
|
|
exporthtml = require("./utils/ExportHtml");
|
|
|
|
exportHandler = require('./handler/ExportHandler');
|
|
|
|
importHandler = require('./handler/ImportHandler');
|
2011-08-03 19:31:25 +01:00
|
|
|
apiHandler = require('./handler/APIHandler');
|
2011-08-04 16:07:58 +01:00
|
|
|
padManager = require('./db/PadManager');
|
2011-08-15 22:20:37 +01:00
|
|
|
securityManager = require('./db/SecurityManager');
|
2011-08-16 15:53:09 +01:00
|
|
|
socketIORouter = require("./handler/SocketIORouter");
|
2012-01-19 17:28:45 -06:00
|
|
|
|
|
|
|
var managers = {
|
|
|
|
ro: readOnlyManager,
|
|
|
|
security: securityManager
|
|
|
|
};
|
|
|
|
var handlers = {
|
|
|
|
"export": exportHandler,
|
|
|
|
"import": importHandler,
|
|
|
|
api: apiHandler
|
|
|
|
};
|
|
|
|
|
2011-07-31 18:25:51 +01:00
|
|
|
//install logging
|
|
|
|
var httpLogger = log4js.getLogger("http");
|
2012-01-19 16:48:24 -06:00
|
|
|
var apiLogger = log4js.getLogger("API");
|
|
|
|
|
2012-01-19 17:36:56 -06:00
|
|
|
|
2011-08-21 19:43:42 +01:00
|
|
|
//checks for padAccess
|
|
|
|
function hasPadAccess(req, res, callback)
|
|
|
|
{
|
2012-01-19 09:42:56 -06:00
|
|
|
return padAccessCombinator(
|
|
|
|
securityManager, req, res,
|
|
|
|
callback,
|
|
|
|
function errorback(err, accessObj){
|
2012-01-19 11:55:59 -06:00
|
|
|
return ERR(err, callback);
|
2012-01-19 09:42:56 -06:00
|
|
|
}
|
|
|
|
);
|
2011-08-21 19:43:42 +01:00
|
|
|
}
|
2012-01-19 16:48:24 -06:00
|
|
|
//redirects browser to the pad's sanitized url if needed. otherwise, renders the html
|
|
|
|
var goToPad = gotoPadCombinator(checkPadName, padManager);
|
2012-01-19 16:55:39 -06:00
|
|
|
//This is for making an api call, collecting all post information and passing it to the apiHandler
|
|
|
|
var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler);
|
|
|
|
|
2012-01-19 16:48:24 -06:00
|
|
|
|
2011-12-01 16:44:51 +01:00
|
|
|
|
2012-01-19 16:48:24 -06:00
|
|
|
app.configure(configureCombinator(app, settings, basic_auth, log4js, httpLogger));
|
2012-01-19 16:29:04 -06:00
|
|
|
|
2012-01-19 16:48:24 -06:00
|
|
|
app.error(function(err, req, res, next){
|
|
|
|
res.send(500);
|
|
|
|
console.error(err.stack ? err.stack : err.toString());
|
|
|
|
gracefulShutdown();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-19 17:20:26 -06:00
|
|
|
var gets = {
|
|
|
|
'/static/*': getStatic,
|
|
|
|
'/minified/:id': getMinified,
|
|
|
|
'/ro/:id': getRoCombinator(
|
|
|
|
serverName,
|
|
|
|
{ro: readOnlyManager},
|
|
|
|
hasPadAccess,
|
|
|
|
ERR,
|
|
|
|
exporthtml
|
|
|
|
),
|
|
|
|
'/p/:pad': sendStaticIfPad(
|
|
|
|
goToPad,
|
|
|
|
padManager,
|
|
|
|
path,
|
|
|
|
"pad.html"
|
|
|
|
)
|
|
|
|
'/p/:pad/timeslider': sendStaticIfPad(
|
|
|
|
goToPad, padManager, path, "timeslider.html"
|
|
|
|
),
|
|
|
|
'/p/:pad/:rev?/export/:type': getExportPadCombinator(
|
|
|
|
goToPad, settings, hasPadAccess, exportHandler, serverName
|
|
|
|
'/api/1/:func': function(req, res)
|
|
|
|
{
|
|
|
|
apiCaller(req, res, req.query)
|
|
|
|
},
|
|
|
|
'/': function(req, res)
|
|
|
|
{
|
|
|
|
return sendStatic(path, res, "index.html");
|
|
|
|
},
|
|
|
|
'/robots.txt': function(req, res)
|
|
|
|
{
|
|
|
|
return sendStatic(path, res, "robots.txt");
|
|
|
|
},
|
|
|
|
'/favicon.ico': function(req, res)
|
|
|
|
{
|
|
|
|
return sendStatic(path, res, "custom/favicon.ico",
|
|
|
|
function(err){
|
|
|
|
//there is no custom favicon, send the default favicon
|
|
|
|
if(err)
|
|
|
|
{
|
|
|
|
filePath = path.normalize(__dirname + "/../static/favicon.ico");
|
|
|
|
res.sendfile(filePath, { maxAge: exports.maxAge });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-01-19 17:04:17 -06:00
|
|
|
};
|
2012-01-19 17:20:26 -06:00
|
|
|
|
2012-01-19 17:28:45 -06:00
|
|
|
var posts = {
|
2012-01-19 17:36:56 -06:00
|
|
|
'/p/:pad/import': postImportPadCombinator(
|
|
|
|
goToPad, settings, serverName, hasPadAccess, importHandler
|
|
|
|
),
|
|
|
|
'/api/1/:func': function(req, res){
|
|
|
|
new formidable.IncomingForm().parse(
|
|
|
|
req,
|
|
|
|
function(err, fields, files)
|
|
|
|
{
|
|
|
|
apiCaller(req, res, fields)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'/ep/pad/connection-diagnostic-info': logOkPostCombinator(
|
|
|
|
"DIAGNOSTIC-INFO", "log", "diagnosticInfo"
|
|
|
|
),
|
|
|
|
'/jserror': logOkPostCombinator(
|
|
|
|
"CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo"
|
|
|
|
)
|
2012-01-19 17:28:45 -06:00
|
|
|
};
|
2012-01-19 17:36:56 -06:00
|
|
|
|
|
|
|
|
2012-01-19 16:55:39 -06:00
|
|
|
|
2012-01-19 17:36:56 -06:00
|
|
|
additionalSetup(app, gets, posts, managers, handlers, db);
|
2012-01-19 17:20:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
for(var key in gets) app.get(key, gets[key]);
|
2012-01-19 17:28:45 -06:00
|
|
|
for(var key in posts) app.post(key, posts[key]);
|
2012-01-19 16:55:39 -06:00
|
|
|
|
2011-05-19 17:36:26 +01:00
|
|
|
//let the server listen
|
2011-07-30 16:39:53 +01:00
|
|
|
app.listen(settings.port, settings.ip);
|
|
|
|
console.log("Server is listening at " + settings.ip + ":" + settings.port);
|
2011-05-14 18:57:07 +01:00
|
|
|
|
2012-01-19 10:14:14 -06:00
|
|
|
setupShutdown(db, app);
|
2011-08-17 15:58:42 +01:00
|
|
|
|
2012-01-19 10:11:51 -06:00
|
|
|
var io = setupIo(socketio, log4js, settings, socketIORouter, app);
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-05-14 18:57:07 +01:00
|
|
|
callback(null);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2012-01-19 16:55:39 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.init = init;
|
2012-01-19 17:36:56 -06:00
|
|
|
init(function(app, gets, posts, managers, handlers, db){});
|