Skip to content

Commit

Permalink
Allow subdomains to be used for tiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrynearson committed Apr 8, 2011
1 parent a060946 commit 9701af2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -41,8 +41,8 @@ module.exports = function(options) {

// Add host info middleware.
var host = require('./server/host')(options);
exports.uiServer.use(host);
exports.tileServer.use(host);
exports.uiServer.use(host.middleware);
exports.tileServer.use(host.middleware);

// Bootstrap.
require('tilestream/server/bootstrap')(options);
Expand All @@ -63,6 +63,7 @@ module.exports = function(options) {
'--config=PATH': 'Pass options via JSON config file at PATH.',
'--uiPort=PORT': 'UI server port. Defaults to 8888.',
'--tilePort=PORT': 'Tile server port. Defaults to 8888.',
'--subdomains=LIST': 'Comma separated list of subdomains to use for tiles.',
'--tiles=PATH': 'Path to tiles directory.'
},
command: function(argv, callback) {
Expand Down
46 changes: 36 additions & 10 deletions server/host.js
@@ -1,16 +1,42 @@
var _ = require('underscore')._;

// Middleware, retrieves host information from request headers and passes them
// to the rest of the stack at `req.uiHost` and `req.tileHost`.
module.exports = function(settings) {
return function(req, res, next) {
if (req.headers && req.headers.host && !req.uiHost) {
req.uiHost = 'http://' + req.headers.host + '/';
req.tileHost = ['http://' + req.headers.host + '/'];
req.model = req.model || {};
req.model.options = req.model.options || {};
req.model.options.uiHost = req.uiHost;
req.model.options.tileHost = req.tileHost;
}
next();
var host = {
removeTileSubdomain: function(host) {
// If subdomain already exists on the request host, remove it.
var subdomains = settings.subdomains.split(',');
var hostComponents = host.split('.');
if (_.include(subdomains, hostComponents.shift())) {
return hostComponents.join('.');
} else {
return host;
}
},
middleware: _(function(req, res, next) {
if (req.headers && req.headers.host && !req.uiHost) {
req.uiHost = 'http://' + req.headers.host + '/';
if (settings.subdomains) {
// Add subdomains for tiles.
var basehost = host.removeTileSubdomain(req.headers.host);
var subdomains = settings.subdomains.split(',');
req.tileHost = [];
_.each(subdomains, function(subdomain) {
req.tileHost.push('http://' + subdomain + '.' + basehost + '/');
});
} else {
// Use the same host for UI and tiles.
req.tileHost = ['http://' + req.headers.host + '/'];
}
req.model = req.model || {};
req.model.options = req.model.options || {};
req.model.options.uiHost = req.uiHost;
req.model.options.tileHost = req.tileHost;
}
next();
}).bind(this)
};
return host;
};

0 comments on commit 9701af2

Please sign in to comment.