make the app mountable

This commit is contained in:
Patrick Pfeiffer 2012-01-21 09:41:56 +01:00
parent 9f0ca7cc44
commit 6188c88e4a
6 changed files with 49 additions and 20 deletions

28
node/server.js Normal file → Executable file
View file

@ -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)"; 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 //cache 6 hours
exports.maxAge = 1000*60*60*6; exports.maxAge = 1000*60*60*6;
@ -76,7 +95,7 @@ async.waterfall([
function (callback) function (callback)
{ {
//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");
@ -425,9 +444,10 @@ async.waterfall([
}); });
//let the server listen //let the server listen
if (!module.parent) {
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);
}
var onShutdown = false; var onShutdown = false;
var gracefulShutdown = function(err) var gracefulShutdown = function(err)
{ {
@ -447,7 +467,9 @@ async.waterfall([
console.log("graceful shutdown..."); console.log("graceful shutdown...");
//stop the http server //stop the http server
if(!module.parent) {
app.close(); app.close();
}
//do the db shutdown //do the db shutdown
db.db.doShutdown(function() db.db.doShutdown(function()
@ -473,7 +495,7 @@ async.waterfall([
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(ioApp);
//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

18
node/utils/Minify.js Normal file → Executable file
View file

@ -35,6 +35,11 @@ var os = require('os');
var TAR_PATH = path.join(__dirname, 'tar.json'); var TAR_PATH = path.join(__dirname, 'tar.json');
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); 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 * creates the minifed javascript for the given minified name
* @param req the Express request * @param req the Express request
@ -63,7 +68,7 @@ exports.minifyJS = function(req, res, jsFilename)
//find out the highest modification date //find out the highest modification date
function(callback) function(callback)
{ {
var folders2check = ["../static/css","../static/js"]; var folders2check = [__dirname+"/../../static/css",__dirname+"/../../static/js"];
//go trough this two folders //go trough this two folders
async.forEach(folders2check, function(path, callback) async.forEach(folders2check, function(path, callback)
@ -102,7 +107,7 @@ exports.minifyJS = function(req, res, jsFilename)
function(callback) function(callback)
{ {
//check the modification time of the minified js //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") if(err && err.code != "ENOENT")
{ {
@ -127,7 +132,7 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
async.forEach(jsFiles, function (item, callback) 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; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;
@ -152,6 +157,7 @@ exports.minifyJS = function(req, res, jsFilename)
{ {
var filename = item.match(/"[^"]*"/g)[0].substr(1); var filename = item.match(/"[^"]*"/g)[0].substr(1);
filename = filename.substr(0,filename.length-1); filename = filename.substr(0,filename.length-1);
filename = __dirname+"/../"+filename
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
@ -205,7 +211,7 @@ exports.minifyJS = function(req, res, jsFilename)
//write the results plain in a file //write the results plain in a file
function(callback) 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 //write the results compressed in a file
function(callback) function(callback)
@ -219,7 +225,7 @@ exports.minifyJS = function(req, res, jsFilename)
if(ERR(err, callback)) return; 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 //skip this step on windows
@ -262,7 +268,7 @@ exports.minifyJS = function(req, res, jsFilename)
//read all js files //read all js files
async.forEach(jsFiles, function (item, callback) 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; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;

2
node/utils/Settings.js Normal file → Executable file
View file

@ -88,7 +88,7 @@ exports.abiwordAvailable = function()
} }
//read the settings sync //read the settings sync
var settingsStr = fs.readFileSync("../settings.json").toString(); var settingsStr = fs.readFileSync(__dirname+"/../../settings.json").toString();
//remove all comments //remove all comments
settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,"");

4
static/index.html Normal file → Executable file
View file

@ -121,12 +121,12 @@
function go2Name() function go2Name()
{ {
var padname = document.getElementById("padname").value; 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() function go2Random()
{ {
window.location = "p/" + randomPadName(); window.location = window.location + "/p/" + randomPadName();
} }
function randomPadName() function randomPadName()

3
static/js/pad2.js Normal file → Executable file
View file

@ -183,8 +183,9 @@ function handshake()
//find out in which subfolder we are //find out in which subfolder we are
var resource = loc.pathname.substr(1, loc.pathname.indexOf("/p/")) + "socket.io"; var resource = loc.pathname.substr(1, loc.pathname.indexOf("/p/")) + "socket.io";
//connect //connect
console.log('create socket',url,resource)
socket = io.connect(url, { socket = io.connect(url, {
resource: resource, resource: 'socket.io',
'max reconnection attempts': 3 'max reconnection attempts': 3
}); });

2
static/pad.html Normal file → Executable file
View file

@ -13,7 +13,7 @@
var clientVars = {}; var clientVars = {};
// ]]> // ]]>
</script> </script>
<script src="../socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<script src="../minified/pad.js"></script> <script src="../minified/pad.js"></script>
<link href="../static/custom/pad.css" rel="stylesheet"> <link href="../static/custom/pad.css" rel="stylesheet">
<script src="../static/custom/pad.js"></script> <script src="../static/custom/pad.js"></script>