Permalink
Browse files

Move to Express.js

Instead of relying on the built-in httpServer and Paperboy, doing the
routing ourselves, we switched to Express.js for maintenance and the
extra functionality (i.e. bodyParser).
  • Loading branch information...
1 parent 87513ae commit 89064844425759d856a0851babbb6f73036797a5 @dv committed Jan 17, 2012
Showing with 39 additions and 40 deletions.
  1. +11 −8 config.js
  2. +28 −32 main.js
View
@@ -3,18 +3,17 @@ var url = require('url');
var redisConfig = url.parse(process.env.REDISTOGO_URL || "");
exports.config = {
- // The directory from which to serve the static files
- webroot: path.join(path.dirname(__filename), ''),
-
// If hostname is in the form of channel.domain.tld
subDomainChannel: false,
+ // Subdomains (or rather, channels) to pass to static serving
+ staticSubDomains: ["www", "localhost"],
+
// The domain used to host this app
- domain: "localhost",
+ domain: "localhost:8000",
- // The subdirectory to listen for webhooks on. If you don't
- // want to enable this, put false.
- hookdir: "hook",
+ // The subdirectory to listen for webhooks on.
+ hookDir: "hook",
// HTTP server details
host: "127.0.0.1",
@@ -42,6 +41,10 @@ exports.config = {
// like on Heroku:
socketOpts: {
"transports": ["xhr-polling"],
- "polling duration": 10 }
+ "polling duration": 10 },
+
+ // The directory from which to serve the static files
+ webRoot: path.join(path.dirname(__filename), '')
+
}
View
@@ -1,30 +1,37 @@
-var paperboy = require('paperboy');
+var _ = require('underscore')._;
+var express = require('express');
var http = require('http');
var url = require('url');
var redis = require('redis');
var config = require('./config.js').config;
+var app = express.createServer();
var sockets;
-function staticRequest(req) {
- return !((req.url.split("/")[1] === config.hookdir)
- || (config.subDomainChannel
- && req.headers["host"].split(":")[0] == config.domain));
-}
-
-function extractChannel(req) {
- if (config.hookdir && req.url.split("/")[1] === config.hookdir) {
- return req.url.split("/")[2];
+app.use(app.router);
+app.use(express.static(config.webRoot)); // Serve static on route fall through
- } else if (config.subDomainChannel && req.headers["host"]) {
- return req.headers["host"].split(".")[0];
- } else {
+// Ignore favicons
+if (config.ignoreFavicon) {
+ app.get('*/favicon.ico', function(req, res) {
return false;
- }
+ });
}
+// Translate subdomain channel into hookdir
+if (config.subDomainChannel) {
+ app.all('*', function(req, res, next) {
+ var subDomain = req.headers.host.slice(0, -config.domain.length).split('.')[0];
+
+ if (subDomain && !_.include(config.staticSubDomains, subDomain)) {
+ req.url = '/' + config.hookDir + '/' + subDomain + req.url;
+ }
+ next();
+ });
+}
-function parseRequest(req, res) {
+// Parse the webhook requests
+app.all('/' + config.hookDir + '/:hookscopechannel/:subpath?', function(req, res) {
var requestUrl = url.parse(req.url, true);
res.writeHead(200, {'Content-Type': 'text/plain'});
@@ -38,7 +45,7 @@ function parseRequest(req, res) {
method: req.method
};
- dataObject.channel = extractChannel(req);
+ dataObject.channel = req.params.hookscopechannel;
if (!dataObject.channel) {
console.log("Received channelless request. Discard.");
@@ -51,32 +58,21 @@ function parseRequest(req, res) {
} else {
console.log("Received request for unknown channel '" + dataObject.channel + "'. Discard.");
}
-}
-
-var server = http.createServer(function (req, res) {
-
- if (config.ignoreFavicon && req.url.split("?")[0].substr(-11, 12) == "favicon.ico") {
- return false;
- }
- if (staticRequest(req)) {
- paperboy.deliver(config.webroot, req, res);
- } else {
- parseRequest(req, res);
- }
});
-server.listen(config.port);
+
+app.listen(config.port);
console.log("Server started on port " + config.port);
-sockets = require('./clients.js').createClients(server, config.socketOpts);
+sockets = require('./clients.js').createClients(app, config.socketOpts);
// Let the client know the webhook-URL.
sockets.on("set channel", function(channel) {
urls = [];
- if (config.hookdir) {
- urls.push("/" + config.hookdir + "/" + channel);
+ if (config.hookDir) {
+ urls.push("/" + config.hookDir + "/" + channel);
}
if (config.subDomainChannel) {

0 comments on commit 8906484

Please sign in to comment.