Use Express to serve static Files

This commit is contained in:
Peter 'Pita' Martischka 2011-05-19 17:36:26 +01:00
parent b0a937d8de
commit 9db2f18b5e
6 changed files with 76 additions and 381 deletions

View File

@ -14,67 +14,90 @@
* limitations under the License.
*/
var http = require('http')
, url = require('url')
, fs = require('fs')
, socketio = require('socket.io')
, sys = require('sys')
, settings = require('./settings')
, db = require('./db')
, async = require('async');
require('joose');
//var http = require('http')
//var url = require('url')
var socketio = require('socket.io')
var settings = require('./settings')
var db = require('./db')
var async = require('async');
var express = require('express');
var path = require('path');
var serverName = "Etherpad-Lite ( http://j.mp/ep-lite )";
async.waterfall([
//initalize the database
function (callback)
{
db.init(callback);
},
//initalize the http server
function (callback)
{
var server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if(path.substring(0,"/static".length) == "/static" || path.substring(0,"/p/".length) == "/p/")
{
if(path.substring(0,"/p/".length) == "/p/")
{
if(path.length < 7)
send404(res, path);
path = "/static/padhtml";
}
sendFile(res, path, __dirname + "/.." + path);
}
else if(path == "/")
{
sendRedirect(res, path, "/p/test");
}
else if(path == "/newpad")
{
sendRedirect(res, path, "/p/" + randomPadName());
}
else if(path == "/ep/pad/reconnect")
{
if(req.headers.referer != null)
sendRedirect(res, path, req.headers.referer);
else
send404(res, path);
}
else
{
send404(res, path);
}
//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);
var filePath = path.normalize(__dirname + "/.." + req.url);
res.sendfile(filePath, { maxAge: 1000*60*60 });
});
server.listen(settings.port);
//serve pad.html under /p
app.get('/p/:pad', function(req, res)
{
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/pad.html");
res.sendfile(filePath, { maxAge: 1000*60*60 });
});
//serve index.html under /
app.get('/', function(req, res)
{
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/index.html");
res.sendfile(filePath, { maxAge: 1000*60*60 });
});
//serve robots.txt
app.get('/robots.txt', function(req, res)
{
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/robots.txt");
res.sendfile(filePath, { maxAge: 1000*60*60 });
});
//serve favicon.ico
app.get('/favicon.ico', function(req, res)
{
res.header("Server", serverName);
var filePath = path.normalize(__dirname + "/../static/favicon.ico");
res.sendfile(filePath, { maxAge: 1000*60*60 });
});
//redirect the newpad requests
app.get('/newpad', function(req, res)
{
res.header("Server", serverName);
res.redirect('/p/' + randomPadName());
});
//let the server listen
app.listen(settings.port);
console.log("Server is listening at port " + settings.port);
var io = socketio.listen(server);
//init socket.io and redirect all requests to the MessageHandler
var io = socketio.listen(app);
var messageHandler = require("./MessageHandler");
messageHandler.setSocketIO(io);
io.on('connection', function(client){
try{
messageHandler.handleConnect(client);
@ -108,67 +131,3 @@ function randomPadName()
}
return randomstring;
}
function sendFile(res, reqPath, path)
{
fs.readFile(path, function(err, data){
if (err){
send404(res, reqPath);
} else {
var contentType = "text/html";
if (path.substring(path.length -3, path.length) == ".js")
contentType = "text/javascript";
else if (path.substring(path.length -4, path.length) == ".css")
contentType = "text/css";
else if (path.substring(path.length -4, path.length) == ".gif")
contentType = "image/gif";
res.writeHead(200, {'Content-Type': contentType});
res.write(data, 'utf8');
res.end();
requestLog(200, reqPath, "-> " + path);
}
});
}
function send404(res, reqPath)
{
res.writeHead(404);
res.write("404 - Not Found");
res.end();
requestLog(404, reqPath, "NOT FOUND!");
}
function sendRedirect(res, reqPath, location)
{
res.writeHead(302, {'Location': location});
res.end();
requestLog(302, reqPath, "-> " + location);
}
function requestLog(code, path, desc)
{
console.log(code +", " + path + ", " + desc);
}
function errorlog(e)
{
var timeStr = new Date().toUTCString() + ": ";
if(typeof e == "string")
{
console.error(timeStr + e);
}
else if(e.stack != null)
{
console.error(timeStr + e.stack);
}
else
{
console.error(timeStr + JSON.stringify(e));
}
}

View File

@ -20,6 +20,7 @@ var fs = require("fs");
exports.port = 9001;
exports.dbType = "sqlite";
exports.dbSettings = { "filename" : "../var/sqlite.db" };
exports.logHTTP = true;
//read the settings sync
var settingsStr = fs.readFileSync("../settings.json").toString();

View File

@ -8,7 +8,8 @@
"socket.io" : ">=0.6.17",
"ueberDB" : ">=0.0.3",
"async" : ">=0.1.9",
"joose" : ">=3.18.0"
"joose" : ">=3.18.0",
"express" : ">=2.3.4"
},
"version" : "0.0.1",
"bin" : {

View File

@ -9,7 +9,7 @@ This file must be valid JSON. But comments are allowed
//the database specific settings
"dbSettings" : {
"filename" : "../var/sqlite.db"
}
},
/* An Example of MySQL Configuration
"dbType" : "mysql",
@ -20,4 +20,6 @@ This file must be valid JSON. But comments are allowed
"database": "store"
}
*/
"logHTTP" : true
}

View File

@ -1,268 +0,0 @@
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="Content-Language" content="en-us"/>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
<title>Etherpad Lite v0.0.1: test</title>
<!-- <base href="http://localhost:9001/" />-->
<!-- CSS -->
<link href="/static/css/pad_lite.css" rel="stylesheet" type="text/css"/>
<!-- javascript -->
<script type="text/javascript">
// <![CDATA[
var clientVars = {}; // ]]>
</script>
<!-- <script type="text/javascript" src="/static/js/client.js"></script>-->
<script type="text/javascript" src="/static/js/plugins.js"></script>
<script type="text/javascript" src="/static/js/undo-xpopup.js"></script>
<!--
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/json.js"></script>
<script type="text/javascript" src="/static/js/json2.js"></script>
<script type="text/javascript" src="/static/js/colorutils.js"></script>
<script type="text/javascript" src="/static/js/draggable.js"></script>
<script type="text/javascript" src="/static/js/pad_utils.js"></script>
<script type="text/javascript" src="/static/js/pad_cookie.js"></script>
<script type="text/javascript" src="/static/js/pad_editor.js"></script>
<script type="text/javascript" src="/static/js/pad_editbar.js"></script>
<script type="text/javascript" src="/static/js/pad_docbar.js"></script>
<script type="text/javascript" src="/static/js/pad_modals.js"></script>
<script type="text/javascript" src="/static/js/ace.js"></script>
<script type="text/javascript" src="/static/js/collab_client.js"></script>
<script type="text/javascript" src="/static/js/pad_userlist.js"></script>
<script type="text/javascript" src="/static/js/pad_impexp.js"></script>
<script type="text/javascript" src="/static/js/pad_savedrevs.js"></script>
<script type="text/javascript" src="/static/js/pad_connectionstatus.js"></script>
<script type="text/javascript" src="/static/js/pad2.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
</head>
<body id="padbody" class="limwidth nonpropad nonprouser">
<div id="padeditor">
<div id="editbar" class="disabledtoolbar">
<table cellpadding="0" cellspacing="0" border="0" id="editbartable">
<tr>
<td>
<img src="/static/img/jun09/pad/editbar_groupleft.gif" width="2" height="24">
</td>
<td class="editbarbutton editbargroupsfirst">
<a href="javascript:void (window.pad&&pad.editbarClick('bold'));" title="Bold (ctrl-B)"><img src="/static/img/jun09/pad/editbar_bold.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('italic'));" title="Italics (ctrl-I)"><img src="/static/img/jun09/pad/editbar_italic.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('underline'));" title="Underline (ctrl-U)"><img src="/static/img/jun09/pad/editbar_underline.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('strikethrough'));" title="Strikethrough"><img src="/static/img/jun09/pad/editbar_strikethrough.gif"></a>
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupright.gif" width="2" height="24">
</td>
<td>
&nbsp;&nbsp;
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupleft.gif" width="2" height="24">
</td>
<td class="editbarbutton editbargroupsfirst">
<a href="javascript:void (window.pad&&pad.editbarClick('insertunorderedlist'));" title="Toggle Bullet List"><img src="/static/img/jun09/pad/editbar_insertunorderedlist.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('indent'));" title="Indent List"><img src="/static/img/jun09/pad/editbar_indent.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('outdent'));" title="Unindent List"><img src="/static/img/jun09/pad/editbar_outdent.gif"></a>
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupright.gif" width="2" height="24">
</td>
<td width="100%">
&nbsp;
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" id="editbarsavetable">
<tr>
<td>
<img src="/static/img/jun09/pad/editbar_groupleft.gif" width="2" height="24">
</td>
<td class="editbarbutton editbargroupsfirst">
<a href="javascript:void (window.pad&&pad.editbarClick('showusers'));" title="Show connected users"><img id="showusersicon" src="/static/img/mar11/editbar_showusers.gif"></a>
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupright.gif" width="2" height="24">
</td>
<td>
&nbsp;&nbsp;
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupleft.gif" width="2" height="24">
</td>
<td class="editbarbutton editbargroupsfirst">
<a href="javascript:void (window.pad&&pad.editbarClick('clearauthorship'));" title="Clear Authorship Colors">
<img src="/static/img/jun09/pad/editbar_clearauthorship.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('undo'));" title="Undo (ctrl-Z)"><img src="/static/img/jun09/pad/editbar_undo.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('redo'));" title="Redo (ctrl-Y)"><img src="/static/img/jun09/pad/editbar_redo.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('save'));" title="Save Revision"><img src="/static/img/jun09/pad/editbar_save.gif"></a>
</td>
<td class="editbarbutton">
<a href="javascript:void (window.pad&&pad.editbarClick('embed'));" title="Embed this pad"><img src="/static/img/mar11/editbar_embed.gif"></a>
</td>
<td>
<!-- The below is completely temporary and should be deleted CAKE -->
<script type="text/javascript">
function shinyshiny(){
$('#showusersicon').fadeOut('fast');
$('#showusersicon').fadeIn('fast');
$('#showusersicon').fadeOut('fast');
$('#showusersicon').fadeIn('fast');
}
</script>
<input type="button" onclick="shinyshiny();" value="shiny">
<!-- end of temporary section designed to be deleted -->
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupright.gif" width="2" height="24">
</td>
<td>
&nbsp;&nbsp;
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupleft.gif" width="2" height="24">
</td>
<td class="editbarbutton editbargroupsfirst">
<select id="viewzoommenu">
<option value="z85">85%</option>
<option value="z100">100%</option>
<option value="z115">115%</option>
<option value="z150">150%</option>
<option value="z200">200%</option>
<option value="z300">300%</option>
</select>
</td>
<td>
<img src="/static/img/jun09/pad/editbar_groupright.gif" width="2" height="24">
</td>
</tr>
</table>
<div id="users">
<ul>
<li><a onclick="showColorPicker();" title="Change my name/color">John</a></li>
<li>Joe</li>
</ul>
<!-- some example code so I can make the css -->
<div id="mycolorpicker">
<ul id="colorpickerswatches">
</ul>
<div id="mycolorpickersave">
<a onclick="closeColorPicker()">Save</a>
</div>
<div id="mycolorpickercancel">
<a onclick="closeColorPicker()">Cancel</a>
</div>
</div>
</div>
</div>
<div id="embed">
</div>
</div>
<div id="editorcontainerbox">
<div id="editorcontainer">
<!-- -->
</div>
<div id="editorloadingbox">
Loading Etherpad Lite...
</div>
</div>
<!-- /padeditor -->
<div id="modaloverlay">
<div id="modaloverlay-inner">
<!-- -->
</div>
</div>
<div id="mainmodals">
<div id="connectionbox" class="modaldialog">
<div id="connectionboxinner" class="modaldialog-inner">
<div class="connecting">
Connecting...
</div>
<div class="reconnecting">
Reestablishing connection...
</div>
<div class="disconnected">
<h2 class="h2_disconnect">Disconnected.</h2>
<h2 class="h2_userdup">Opened in another window.</h2>
<h2 class="h2_unauth">No Authorization.</h2>
<div id="disconnected_looping">
<p>
<b>We're having trouble talking to the EtherPad lite synchronization server.</b>
You may be connecting through an incompatible firewall or proxy server.
</p>
</div>
<div id="disconnected_initsocketfail">
<p>
<b>We were unable to connect to the EtherPad lite synchronization server.</b>
This may be due to an incompatibility with your web browser or internet connection.
</p>
</div>
<div id="disconnected_userdup">
<p>
<b>You seem to have opened this pad in another browser window.</b>
If you'd like to use this window instead, you can reconnect.
</p>
</div>
<div id="disconnected_unknown">
<p>
<b>Lost connection with the EtherPad lite synchronization server.</b> This may be due to a loss of network connectivity.
</p>
</div>
<div id="disconnected_slowcommit">
<p>
<b>Server not responding.</b> This may be due to network connectivity issues or high load on the server.
</p>
</div>
<div id="disconnected_unauth">
<p>
Your browser's credentials or permissions have changed while viewing this pad. Try reconnecting.
</p>
</div>
<div id="reconnect_advise">
<p>
If this continues to happen, please <a target="_blank" href="/ep/support">let us know</a>
(opens in new window).
</p>
</div>
<div id="reconnect_form">
<button id="forcereconnect">Reconnect Now</button>
</div>
</div>
</div>
</div>
</div>
<form id="reconnectform" method="post" action="/ep/pad/reconnect" accept-charset="UTF-8" style="display: none;">
<input type="hidden" class="padId" name="padId"/>
<input type="hidden" class="diagnosticInfo" name="diagnosticInfo"/>
<input type="hidden" class="missedChanges" name="missedChanges"/>
</form>
</body>
</html>