Skip to content

Commit

Permalink
back to using httpolyglot to serve both http and https
Browse files Browse the repository at this point in the history
latest version supports node v0.12

closes #401
  • Loading branch information
andrewrk committed Feb 19, 2015
1 parent dfd0fd8 commit bcb7a6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var http = require('http');
var https = require('https');
var httpolyglot = require('httpolyglot');
var assert = require('assert');
var yawl = require('yawl');
var fs = require('fs');
Expand Down Expand Up @@ -116,6 +116,11 @@ function main() {
}

var app = express();
var sslEnabled = !!(config.sslKey && config.sslCert);
if (sslEnabled) {
app.use(redirectHttpToHttps);
}

var pend = new Pend();
pend.go(function(cb) {
var options = {
Expand Down Expand Up @@ -144,7 +149,7 @@ function main() {
if (err) throw err;
log.debug("Player initialization complete.");

startServer(app, db, player, config);
startServer(app, db, player, config, sslEnabled);
});
});
});
Expand Down Expand Up @@ -407,7 +412,16 @@ function createHasPermMiddleware(playerServer, permName) {
};
}

function startServer(app, db, player, config) {
function redirectHttpToHttps(req, resp, next) {
if (req.socket.encrypted) return next();
resp.writeHead(301, {
Location: "https://" + req.headers.host + req.url,
Connection: "close",
});
resp.end();
}

function startServer(app, db, player, config, sslEnabled) {
var playerServer = new PlayerServer({
config: config,
player: player,
Expand All @@ -431,7 +445,7 @@ function startServer(app, db, player, config) {
});

function createHttpServer(cb) {
if (!config.sslKey || !config.sslCert) {
if (!sslEnabled) {
log.warn("WARNING: SSL disabled, using HTTP.");
httpServer = http.createServer(app);
httpProtocol = 'http';
Expand Down Expand Up @@ -499,7 +513,7 @@ function startServer(app, db, player, config) {
});
});
pend.wait(function() {
httpServer = https.createServer(options, app);
httpServer = httpolyglot.createServer(options, app);
cb();
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"findit2": "~2.2.3",
"googleapis": "~1.1.3",
"groove": "~2.3.1",
"httpolyglot": "~0.1.0",
"httpolyglot": "~0.1.1",
"keese": "~1.1.1",
"lastfm": "~0.9.2",
"leveldown": "~1.0.0",
Expand Down

0 comments on commit bcb7a6d

Please sign in to comment.