From 6188c88e4a2891be732a486d024d4d7dae669394 Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer Date: Sat, 21 Jan 2012 09:41:56 +0100 Subject: [PATCH 1/4] make the app mountable --- node/server.js | 36 +++++++++++++++++++++++++++++------- node/utils/Minify.js | 22 ++++++++++++++-------- node/utils/Settings.js | 2 +- static/index.html | 4 ++-- static/js/pad2.js | 3 ++- static/pad.html | 2 +- 6 files changed, 49 insertions(+), 20 deletions(-) mode change 100644 => 100755 node/server.js mode change 100644 => 100755 node/utils/Minify.js mode change 100644 => 100755 node/utils/Settings.js mode change 100644 => 100755 static/index.html mode change 100644 => 100755 static/js/pad2.js mode change 100644 => 100755 static/pad.html diff --git a/node/server.js b/node/server.js old mode 100644 new mode 100755 index 6a4f0cfe2..5d8b29389 --- a/node/server.js +++ b/node/server.js @@ -60,6 +60,25 @@ console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues") var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)"; +//export app and handle mounting +var app = express.createServer(); +var ioApp = app; + +exports.helpExpress = function(opts) { + console.log('helping express', opts); + if (opts.basepath) app.set('basepath',opts.basepath); + if (opts.settings) { + settings = opts.settings; + minify = require('./utils/Minify')(opts.settings); + } + return app; +} + +app.mounted(function(other){ + ioApp = other; + console.log('mounted pad'); +}); + //cache 6 hours exports.maxAge = 1000*60*60*6; @@ -76,7 +95,7 @@ async.waterfall([ function (callback) { //create server - var app = express.createServer(); + //var app = express.createServer(); //load modules that needs a initalized db readOnlyManager = require("./db/ReadOnlyManager"); @@ -425,9 +444,10 @@ async.waterfall([ }); //let the server listen - app.listen(settings.port, settings.ip); - console.log("Server is listening at " + settings.ip + ":" + settings.port); - + if (!module.parent) { + app.listen(settings.port, settings.ip); + console.log("Server is listening at " + settings.ip + ":" + settings.port); + } var onShutdown = false; var gracefulShutdown = function(err) { @@ -447,7 +467,9 @@ async.waterfall([ console.log("graceful shutdown..."); //stop the http server - app.close(); + if(!module.parent) { + app.close(); + } //do the db shutdown db.db.doShutdown(function() @@ -473,7 +495,7 @@ async.waterfall([ process.on('uncaughtException', gracefulShutdown); //init socket.io and redirect all requests to the MessageHandler - var io = socketio.listen(app); + var io = socketio.listen(ioApp); //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 @@ -510,7 +532,7 @@ async.waterfall([ socketIORouter.setSocketIO(io); socketIORouter.addComponent("pad", padMessageHandler); socketIORouter.addComponent("timeslider", timesliderMessageHandler); - + callback(null); } ]); diff --git a/node/utils/Minify.js b/node/utils/Minify.js old mode 100644 new mode 100755 index 719cdaeeb..4cf3a5c39 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -35,6 +35,11 @@ var os = require('os'); var TAR_PATH = path.join(__dirname, 'tar.json'); var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); +module.exports = function(opts) { + settings = opts; + return exports; +} + /** * creates the minifed javascript for the given minified name * @param req the Express request @@ -63,7 +68,7 @@ exports.minifyJS = function(req, res, jsFilename) //find out the highest modification date function(callback) { - var folders2check = ["../static/css","../static/js"]; + var folders2check = [__dirname+"/../../static/css",__dirname+"/../../static/js"]; //go trough this two folders async.forEach(folders2check, function(path, callback) @@ -102,7 +107,7 @@ exports.minifyJS = function(req, res, jsFilename) function(callback) { //check the modification time of the minified js - fs.stat("../var/minified_" + jsFilename, function(err, stats) + fs.stat(__dirname+"/../../var/minified_" + jsFilename, function(err, stats) { if(err && err.code != "ENOENT") { @@ -127,7 +132,7 @@ exports.minifyJS = function(req, res, jsFilename) { async.forEach(jsFiles, function (item, callback) { - fs.readFile("../static/js/" + item, "utf-8", function(err, data) + fs.readFile(__dirname+"/../../static/js/" + item, "utf-8", function(err, data) { if(ERR(err, callback)) return; fileValues[item] = data; @@ -152,9 +157,10 @@ exports.minifyJS = function(req, res, jsFilename) { var filename = item.match(/"[^"]*"/g)[0].substr(1); filename = filename.substr(0,filename.length-1); - + filename = __dirname+"/../"+filename + var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); - + //read the included file fs.readFile(filename, "utf-8", function(err, data) { @@ -205,7 +211,7 @@ exports.minifyJS = function(req, res, jsFilename) //write the results plain in a file function(callback) { - fs.writeFile("../var/minified_" + jsFilename, result, "utf8", callback); + fs.writeFile(__dirname+"/../../var/minified_" + jsFilename, result, "utf8", callback); }, //write the results compressed in a file function(callback) @@ -219,7 +225,7 @@ exports.minifyJS = function(req, res, jsFilename) if(ERR(err, callback)) return; - fs.writeFile("../var/minified_" + jsFilename + ".gz", compressedResult, callback); + fs.writeFile(__dirname+"/../../var/minified_" + jsFilename + ".gz", compressedResult, callback); }); } //skip this step on windows @@ -262,7 +268,7 @@ exports.minifyJS = function(req, res, jsFilename) //read all js files async.forEach(jsFiles, function (item, callback) { - fs.readFile("../static/js/" + item, "utf-8", function(err, data) + fs.readFile(__dirname+"/../../static/js/" + item, "utf-8", function(err, data) { if(ERR(err, callback)) return; fileValues[item] = data; diff --git a/node/utils/Settings.js b/node/utils/Settings.js old mode 100644 new mode 100755 index 43c22975e..cb4968fcc --- a/node/utils/Settings.js +++ b/node/utils/Settings.js @@ -88,7 +88,7 @@ exports.abiwordAvailable = function() } //read the settings sync -var settingsStr = fs.readFileSync("../settings.json").toString(); +var settingsStr = fs.readFileSync(__dirname+"/../../settings.json").toString(); //remove all comments settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); diff --git a/static/index.html b/static/index.html old mode 100644 new mode 100755 index 1e1861639..b637f0d20 --- a/static/index.html +++ b/static/index.html @@ -121,12 +121,12 @@ function go2Name() { var padname = document.getElementById("padname").value; - padname.length > 0 ? window.location = "p/" + padname : alert("Please enter a name") + padname.length > 0 ? window.location = window.location + "/p/" + padname : alert("Please enter a name") } function go2Random() { - window.location = "p/" + randomPadName(); + window.location = window.location + "/p/" + randomPadName(); } function randomPadName() diff --git a/static/js/pad2.js b/static/js/pad2.js old mode 100644 new mode 100755 index bbb385b3b..4aeef82c3 --- a/static/js/pad2.js +++ b/static/js/pad2.js @@ -183,8 +183,9 @@ function handshake() //find out in which subfolder we are var resource = loc.pathname.substr(1, loc.pathname.indexOf("/p/")) + "socket.io"; //connect + console.log('create socket',url,resource) socket = io.connect(url, { - resource: resource, + resource: 'socket.io', 'max reconnection attempts': 3 }); diff --git a/static/pad.html b/static/pad.html old mode 100644 new mode 100755 index f57438d56..929be3e4b --- a/static/pad.html +++ b/static/pad.html @@ -13,7 +13,7 @@ var clientVars = {}; // ]]> - + From 2874e1b9045b7ef84cbb9acca37aab1db4a9784e Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer Date: Sat, 21 Jan 2012 10:30:23 +0100 Subject: [PATCH 2/4] optional settings-file --- node/utils/Settings.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/node/utils/Settings.js b/node/utils/Settings.js index cb4968fcc..99059daea 100755 --- a/node/utils/Settings.js +++ b/node/utils/Settings.js @@ -87,11 +87,20 @@ exports.abiwordAvailable = function() } } -//read the settings sync -var settingsStr = fs.readFileSync(__dirname+"/../../settings.json").toString(); +var settingsStr = '{}'; -//remove all comments -settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); +try +{ + //read the settings sync + settingsStr = fs.readFileSync(__dirname+"/../../settings.json").toString(); + + //remove all comments + settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); +} +catch(e) +{ + console.warn('could not read '+__dirname+"/../../settings.json") +} //try to parse the settings var settings; From dce5227e42eba65f6705a0eca35897c3fbe3a611 Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer Date: Sat, 21 Jan 2012 11:37:51 +0100 Subject: [PATCH 3/4] write settings upon mount --- node/server.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/node/server.js b/node/server.js index 5d8b29389..f37b78490 100755 --- a/node/server.js +++ b/node/server.js @@ -20,17 +20,35 @@ * limitations under the License. */ +//export app and handle mounting +var express = require('express'); +var app = express.createServer(); +var fs = require('fs'); +var ioApp = app; + + +var settings = require('./utils/Settings'); +var minify = require('./utils/Minify'); +var db = require('./db/DB'); + +exports.helpExpress = function(opts) { + console.log('helping express', opts); + if (opts.basepath) app.set('basepath',opts.basepath); + if (opts.settings) { + fs.writeFile(__dirname+'/../settings.json',JSON.stringify(opts.settings)) + } + settings = require('./utils/Settings'); + minify = require('./utils/Minify'); + db = require('./db/DB'); + return app; +} + var ERR = require("async-stacktrace"); var log4js = require('log4js'); var os = require("os"); var socketio = require('socket.io'); -var fs = require('fs'); -var settings = require('./utils/Settings'); -var db = require('./db/DB'); var async = require('async'); -var express = require('express'); var path = require('path'); -var minify = require('./utils/Minify'); var formidable = require('formidable'); var apiHandler; var exportHandler; @@ -60,20 +78,6 @@ console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues") var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)"; -//export app and handle mounting -var app = express.createServer(); -var ioApp = app; - -exports.helpExpress = function(opts) { - console.log('helping express', opts); - if (opts.basepath) app.set('basepath',opts.basepath); - if (opts.settings) { - settings = opts.settings; - minify = require('./utils/Minify')(opts.settings); - } - return app; -} - app.mounted(function(other){ ioApp = other; console.log('mounted pad'); From 1e71d41b5f38b434b2a58806ab69b25e4e631d1b Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer Date: Sat, 21 Jan 2012 11:46:51 +0100 Subject: [PATCH 4/4] minify helper isnt needed anymore --- node/utils/Minify.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index 4cf3a5c39..2055761f7 100755 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -35,11 +35,6 @@ var os = require('os'); var TAR_PATH = path.join(__dirname, 'tar.json'); var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); -module.exports = function(opts) { - settings = opts; - return exports; -} - /** * creates the minifed javascript for the given minified name * @param req the Express request