2011-03-26 14:52:56 +00:00
|
|
|
/**
|
2011-05-30 14:53:11 +00: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-03-26 14:52:56 +00:00
|
|
|
* 2011 Peter 'Pita' Martischka
|
|
|
|
*
|
|
|
|
* 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-05-16 14:30:21 +00:00
|
|
|
require('joose');
|
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
var socketio = require('socket.io');
|
|
|
|
var settings = require('./settings');
|
2011-06-20 10:44:04 +00:00
|
|
|
var socketIORouter = require("./SocketIORouter");
|
2011-05-28 17:09:17 +00:00
|
|
|
var db = require('./db');
|
2011-05-19 16:36:26 +00:00
|
|
|
var async = require('async');
|
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
2011-05-28 17:09:17 +00:00
|
|
|
var minify = require('./minify');
|
2011-05-19 16:36:26 +00:00
|
|
|
|
|
|
|
var serverName = "Etherpad-Lite ( http://j.mp/ep-lite )";
|
|
|
|
|
2011-05-14 17:57:07 +00:00
|
|
|
async.waterfall([
|
2011-05-19 16:36:26 +00:00
|
|
|
//initalize the database
|
2011-05-14 17:57:07 +00:00
|
|
|
function (callback)
|
|
|
|
{
|
|
|
|
db.init(callback);
|
|
|
|
},
|
2011-05-19 16:36:26 +00:00
|
|
|
//initalize the http server
|
2011-05-14 17:57:07 +00:00
|
|
|
function (callback)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2011-05-19 16:36:26 +00:00
|
|
|
//create server
|
|
|
|
var app = express.createServer();
|
|
|
|
|
|
|
|
//set logging
|
|
|
|
if(settings.logHTTP)
|
|
|
|
app.use(express.logger({ format: ':date: :status, :method :url' }));
|
|
|
|
|
|
|
|
//serve static files
|
|
|
|
app.get('/static/*', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
2011-05-28 17:09:17 +00:00
|
|
|
var filePath = path.normalize(__dirname + "/.." + req.url.split("?")[0]);
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-05-14 17:57:07 +00:00
|
|
|
});
|
2011-05-19 16:36:26 +00:00
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
//serve minified files
|
|
|
|
app.get('/minified/:id', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
|
|
|
if(id == "pad.js")
|
|
|
|
{
|
|
|
|
minify.padJS(req,res);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.send('404 - Not Found', 404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//serve pad.html under /p
|
|
|
|
app.get('/p/:pad', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/pad.html");
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
2011-06-20 10:44:04 +00:00
|
|
|
//serve timeslider.html under /p/$padname/timeslider
|
|
|
|
app.get('/p/:pad/timeslider', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/timeslider.html");
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-06-20 10:44:04 +00:00
|
|
|
});
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//serve index.html under /
|
|
|
|
app.get('/', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/index.html");
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//serve robots.txt
|
|
|
|
app.get('/robots.txt', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/robots.txt");
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//serve favicon.ico
|
|
|
|
app.get('/favicon.ico', function(req, res)
|
|
|
|
{
|
|
|
|
res.header("Server", serverName);
|
|
|
|
var filePath = path.normalize(__dirname + "/../static/favicon.ico");
|
2011-06-27 21:10:18 +00:00
|
|
|
res.sendfile(filePath);
|
2011-05-19 16:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//let the server listen
|
|
|
|
app.listen(settings.port);
|
2011-05-14 17:57:07 +00:00
|
|
|
console.log("Server is listening at port " + settings.port);
|
|
|
|
|
2011-05-19 16:36:26 +00:00
|
|
|
//init socket.io and redirect all requests to the MessageHandler
|
|
|
|
var io = socketio.listen(app);
|
2011-06-20 10:44:04 +00:00
|
|
|
|
|
|
|
var padMessageHandler = require("./PadMessageHandler");
|
|
|
|
var timesliderMessageHandler = require("./TimesliderMessageHandler");
|
|
|
|
|
|
|
|
//Initalize the Socket.IO Router
|
|
|
|
socketIORouter.setSocketIO(io);
|
|
|
|
socketIORouter.addComponent("pad", padMessageHandler);
|
|
|
|
socketIORouter.addComponent("timeslider", timesliderMessageHandler);
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-05-14 17:57:07 +00:00
|
|
|
callback(null);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2011-05-14 17:57:07 +00:00
|
|
|
]);
|