Using express.static instead of custom handler
This commit is contained in:
parent
a266f0bee1
commit
08064ae035
46
server.js
46
server.js
@ -13,47 +13,25 @@ var config = require('./config');
|
|||||||
config.websocketPort = config.websocketPort || config.httpPort;
|
config.websocketPort = config.websocketPort || config.httpPort;
|
||||||
|
|
||||||
// support multiple storage back ends
|
// support multiple storage back ends
|
||||||
var Storage = require(config.storage||'./storage/mongo');
|
var Storage = require(config.storage || './storage/mongo');
|
||||||
|
|
||||||
var app = Express();
|
var app = Express();
|
||||||
app.use(Express.static(__dirname + '/www'));
|
app.use(Express.static(__dirname + '/www'));
|
||||||
|
|
||||||
Fs.exists(__dirname + "/customize", function (e) {
|
Fs.exists(__dirname + "/customize", function(e) {
|
||||||
if (e) { return; }
|
if (e) { return; }
|
||||||
console.log("Cryptpad is customizable, see customize.dist/readme.md for details");
|
console.log("Cryptpad is customizable, see customize.dist/readme.md for details");
|
||||||
});
|
});
|
||||||
|
|
||||||
var staticOpts = {
|
|
||||||
index: 'index.html'
|
|
||||||
};
|
|
||||||
|
|
||||||
var handleFile = function (target, res, fallback, next) {
|
|
||||||
var stream = Fs.createReadStream(target).on('error', function (e) {
|
|
||||||
if (fallback) {
|
|
||||||
handleFile(fallback, res, undefined, next);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
}).on('end', function () {
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
stream.pipe(res);
|
|
||||||
};
|
|
||||||
|
|
||||||
app.use("/customize", Express.static(__dirname + '/customize'));
|
app.use("/customize", Express.static(__dirname + '/customize'));
|
||||||
app.use("/customize", Express.static(__dirname + '/customize.dist'));
|
app.use("/customize", Express.static(__dirname + '/customize.dist'));
|
||||||
app.use(/^\/[^\/]*$/, function(req, res, next) {
|
app.use(/^\/[^\/]*$/, Express.static('customize'));
|
||||||
var file = req.originalUrl.slice(1) || 'index.html';
|
app.use(/^\/[^\/]*$/, Express.static('customize.dist'));
|
||||||
handleFile(__dirname + '/customize' + file, // try piping this file first
|
|
||||||
res, __dirname + '/customize.dist/' + file, // if it doesn't exist
|
|
||||||
next); // finally, fall through
|
|
||||||
});
|
|
||||||
|
|
||||||
var httpsOpts;
|
var httpsOpts;
|
||||||
if (config.privKeyAndCertFiles) {
|
if (config.privKeyAndCertFiles) {
|
||||||
var privKeyAndCerts = '';
|
var privKeyAndCerts = '';
|
||||||
config.privKeyAndCertFiles.forEach(function (file) {
|
config.privKeyAndCertFiles.forEach(function(file) {
|
||||||
privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file);
|
privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file);
|
||||||
});
|
});
|
||||||
var array = privKeyAndCerts.split('\n-----BEGIN ');
|
var array = privKeyAndCerts.split('\n-----BEGIN ');
|
||||||
@ -74,30 +52,30 @@ if (config.privKeyAndCertFiles) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/api/config', function(req, res){
|
app.get('/api/config', function(req, res) {
|
||||||
var host = req.headers.host.replace(/\:[0-9]+/, '');
|
var host = req.headers.host.replace(/\:[0-9]+/, '');
|
||||||
res.setHeader('Content-Type', 'text/javascript');
|
res.setHeader('Content-Type', 'text/javascript');
|
||||||
res.send('define(' + JSON.stringify({
|
res.send('define(' + JSON.stringify({
|
||||||
websocketURL:'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' +
|
websocketURL: 'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' +
|
||||||
config.websocketPort + '/cryptpad_websocket',
|
config.websocketPort + '/cryptpad_websocket',
|
||||||
webrtcURL:'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' +
|
webrtcURL: 'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' +
|
||||||
config.websocketPort + '/cryptpad_webrtc',
|
config.websocketPort + '/cryptpad_webrtc',
|
||||||
}) + ');');
|
}) + ');');
|
||||||
});
|
});
|
||||||
|
|
||||||
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
|
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
|
||||||
|
|
||||||
httpServer.listen(config.httpPort,config.httpAddress,function(){
|
httpServer.listen(config.httpPort, config.httpAddress, function() {
|
||||||
console.log('listening on %s',config.httpPort);
|
console.log('listening on %s', config.httpPort);
|
||||||
});
|
});
|
||||||
|
|
||||||
var wsConfig = { server: httpServer };
|
var wsConfig = { server: httpServer };
|
||||||
if (config.websocketPort !== config.httpPort) {
|
if (config.websocketPort !== config.httpPort) {
|
||||||
console.log("setting up a new websocket server");
|
console.log("setting up a new websocket server");
|
||||||
wsConfig = { port: config.websocketPort};
|
wsConfig = { port: config.websocketPort };
|
||||||
}
|
}
|
||||||
var wsSrv = new WebSocketServer(wsConfig);
|
var wsSrv = new WebSocketServer(wsConfig);
|
||||||
Storage.create(config, function (store) {
|
Storage.create(config, function(store) {
|
||||||
console.log('DB connected');
|
console.log('DB connected');
|
||||||
NetfluxSrv.run(store, wsSrv, config);
|
NetfluxSrv.run(store, wsSrv, config);
|
||||||
WebRTCSrv.run(wsSrv);
|
WebRTCSrv.run(wsSrv);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user