Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow subdomains to be used for tiles.
- Loading branch information
1 parent
a060946
commit 9701af2
Showing
2 changed files
with
39 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
|