Skip to content

Commit

Permalink
Add host info middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
Young Hahn committed Apr 7, 2011
1 parent 02db8e3 commit 04a8c41
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 69 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ Commandline options:
start server
--config=PATH Pass options via JSON config file at PATH.
--uiPort=PORT UI server port. Defaults to 8888.
--uiHost=HOST UI server hostname. Defaults to localhost.
--tilePort=PORT Tile server port. Defaults to 8888.
--tileHost=HOST Tile server hostname(s). Defaults to localhost.
--tiles=PATH Path to tiles directory.


Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ module.exports = function(options) {

options = options || {};
options.uiPort = options.uiPort || 8888;
options.uiHost = options.uiHost || false;
options.tilePort = options.tilePort || 8888;
options.tileHost = options.tileHost ? options.tileHost.split(',') : [];
options.tiles = options.tiles || path.join(process.cwd(), 'tiles');
// @TODO: how to alter these hashes with commandline options?
// Default tile response headers. Sets max-age to one hour.
Expand All @@ -41,6 +39,11 @@ module.exports = function(options) {
exports.tileServer = express.createServer();
}

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

// Bootstrap.
require('tilestream/server/bootstrap')(options);
require('tilestream/server/ui-server')(exports.uiServer, options);
Expand All @@ -59,9 +62,7 @@ module.exports = function(options) {
options: {
'--config=PATH': 'Pass options via JSON config file at PATH.',
'--uiPort=PORT': 'UI server port. Defaults to 8888.',
'--uiHost=HOST': 'UI server hostname. Defaults to localhost.',
'--tilePort=PORT': 'Tile server port. Defaults to 8888.',
'--tileHost=HOST': 'Tile server hostname(s). Defaults to localhost.',
'--tiles=PATH': 'Path to tiles directory.'
},
command: function(argv, callback) {
Expand Down
15 changes: 12 additions & 3 deletions mvc/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ Bones.controllers.Router = Backbone.Controller.extend({
Collection: Bones.models.Tilesets,
Model: Bones.models.Tileset,
initialize: function(options) {
_.bindAll(this, 'list', 'map');
_.bindAll(this, 'list', 'map', 'getOptions');
},
getOptions: function(response) {
var options = {};
if (response.req && response.req.model && response.req.model.options) {
options = response.req.model.options;
} else if (Bones.settings) {
options = Bones.settings;
}
return options;
},
routes: {
'': 'list',
Expand All @@ -25,7 +34,7 @@ Bones.controllers.Router = Backbone.Controller.extend({
},
list: function(response) {
var that = this;
var options = (response.req && response.req.model && response.req.model.options) || {};
var options = this.getOptions(response);
(new this.Collection([], options)).fetch({
success: function(collection) {
var view = new Bones.views.Maps({ collection: collection });
Expand All @@ -42,7 +51,7 @@ Bones.controllers.Router = Backbone.Controller.extend({
},
map: function(id, response) {
var that = this;
var options = (response.req && response.req.model && response.req.model.options) || {};
var options = this.getOptions(response);
(new this.Model({ id: id }, options)).fetch({
success: function(model) {
var view = new Bones.views.Map({ model: model });
Expand Down
52 changes: 14 additions & 38 deletions mvc/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,21 @@ Bones.models = Bones.models || {};
// A single tileset, corresponding to an `.mbtiles` file. `model.id` is the
// file basename, e.g. `foo.mbtiles` has an `id` of `foo`.
Bones.models.Tileset = Backbone.Model.extend({
initialize: function(attributes) {
Backbone.Model.prototype.initialize.call(this, attributes);
// Convert representation of baselayer into a true Tileset model.
if (typeof this.get('baselayer') !== 'undefined') {
this.set({ baselayer: new Tileset(this.get('baselayer')) });
}
},
parse: function(response){
var model = Backbone.Model.prototype.parse.call(this, response);
// Convert representation of baselayer into a true Tileset model.
if (typeof model.baselayer !== 'undefined') {
model.baselayer = new Tileset(model.baselayer);
initialize: function(attributes, options) {
options = options || {};
if (this.collection) {
this.uiHost = this.collection.uiHost;
this.tileHost = this.collection.tileHost;
} else {
this.uiHost = options.uiHost;
this.tileHost = options.tileHost;
}
return model;
},
url: function() {
return '/api/Tileset/' + this.id;
},
// Return the base URLs of TileStream tile servers including a single
// trailing slash, e.g. http://localhost:8889/ in an Array.
layerURL: function() {
// Servers defined in `Bones.settings`.
if (Bones.settings.tileHost.length) {
return Bones.settings.tileHost;
// Autodetect server from window object.
} else if (window.location && window.location.hostname) {
// Attempt to autodetect URL.
var baseURL = window.location.protocol + '//' + window.location.hostname + ':' + Bones.settings.tilePort;
var args = window.location.pathname.split('/');
// Path already ends with trailing slash.
if (args[args.length - 1] === '') {
return [baseURL + args.join('/')];
// index.html or similar trailing filename.
} else if (args[args.length - 1].indexOf('.') !== -1) {
args.pop();
return [baseURL + args.join('/') + '/'];
// Path beyond domain.
} else {
return [baseURL + args.join('/') + '/'];
}
// Server side, *TODO* needs a solution.
} else {
return ['http://localhost:8888/'];
}
return this.tileHost;
},
// Get ZXY of tile of tileset's center and minzoom. From [OSM wiki][1].
// [1]: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#lon.2Flat_to_tile_numbers_2
Expand Down Expand Up @@ -94,6 +65,11 @@ Bones.models.Tileset = Backbone.Model.extend({
// --------
// Collection of all tileset models.
Bones.models.Tilesets = Backbone.Collection.extend({
initialize: function(models, options) {
options = options || {};
this.uiHost = options.uiHost;
this.tileHost = options.tileHost;
},
model: Bones.models.Tileset,
url: '/api/Tileset',
comparator: function(model) {
Expand Down
16 changes: 16 additions & 0 deletions server/host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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();
};
};

22 changes: 8 additions & 14 deletions server/ui-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,17 @@ module.exports = function(server, settings) {
]));
server.get('/theme/default/style.css', mirror.file('openlayers_slim/theme/default/style.css'));

// Settings endpoint. Filter settings down to only those that should be
// accessible by the client.
// Settings endpoint. Send information that need to be shared between
// server/client.
server.get('/settings.js', function(req, res, next) {
var pub = ['uiHost', 'tileHost', 'uiPort', 'tilePort', 'features'],
filtered = {};
_(settings).each(function(val, key) {
_(pub).include(key) && (filtered[key] = val);
});
filtered.uiHost = filtered.uiHost
? filtered.uiHost
: 'http://' + req.headers.host + '/';
filtered.tileHost = filtered.tileHost.length
? filtered.tileHost
: ['http://' + req.headers.host + '/'];
var pub = {
uiHost: req.uiHost,
tileHost: req.tileHost,
features: settings.features
};
res.send(
'var Bones = Bones || {};\n' +
'Bones.settings = ' + JSON.stringify(filtered) + ';',
'Bones.settings = ' + JSON.stringify(pub) + ';',
{ 'Content-Type': 'text/javascript' }
);
});
Expand Down
7 changes: 1 addition & 6 deletions server/wax.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ module.exports = function(app, settings) {
// - `center` - List in the form [<lon>, <lat>, <zoom>]
// center[]=66.5&center[]=55.8&&center[]=2
app.get('/wax.json', load, function(req, res, next) {
var hosts = {
uiHost: settings.uiHost || 'http://' + req.headers.host + '/',
tileHost: settings.tileHost.length
? settings.tileHost
: ['http://' + req.headers.host + '/']
};
var hosts = { uiHost: req.uiHost, tileHost: req.tileHost };
res.send(Waxer[req.query.api].generate(res.layers, req.query, hosts));
});

Expand Down
2 changes: 0 additions & 2 deletions test/tilestream.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var assert = require('assert'),
tilestream = require('tilestream')({
tiles: __dirname + '/fixtures/tiles',
uiHost: 'http://locahost:8888',
tileHost: 'http://localhost:8888',
uiPort: 8888,
tilePort: 8888
});
Expand Down

0 comments on commit 04a8c41

Please sign in to comment.