From de8508ed5782d96973b110d1a897e454cfd8dbe9 Mon Sep 17 00:00:00 2001 From: Simon Tokumine Date: Fri, 7 Oct 2011 15:38:28 +0100 Subject: [PATCH] add map metadata enpoind and move all responsiblity for infowindow and metadata to here --- README.md | 10 ++---- app.js | 64 +++++++++++++++++++++++++++++------ lib/cartodb/carto_data.js | 22 +++++++++++- lib/cartodb/server_options.js | 19 +++++++++++ 4 files changed, 96 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index bc09e35e..a2a3af2b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Windshaft-CartoDB This is the CartoDB map tiler. -Look at lob/cartodb/server_options to see how we configure windshaft +Look at lib/cartodb/server_options to see how we configure windshaft Install @@ -14,10 +14,6 @@ npm install node app.js [development | production] ``` -Dependencies ------------- -Mapnik r - Core features ------------- @@ -25,7 +21,7 @@ Core features * configures windshaft to publish cartodb_id as the interactivity layer * gets the default geometry type from the cartodb redis store * provides an ultra basic infowindow endpoint for windshaft - +* provides an ultra basic map_metadata endpoint for windshaft URLs ---- @@ -60,4 +56,4 @@ Args: * infowindow - returns contents of infowindow from CartoDB. -All GET requests are wrappable with JSONp using callback argument, including the UTFGrid map tile call. \ No newline at end of file +All GET requests are wrappable with JSONP using callback argument, including the UTFGrid map tile call. \ No newline at end of file diff --git a/app.js b/app.js index a1c958c9..5a2a7fd5 100755 --- a/app.js +++ b/app.js @@ -1,22 +1,26 @@ /* -* Windshaft -* =============== -* -* ./app.js [environment] -* -* environments: [development, production] -*/ + * Windshaft-CartoDB + * =============== + * + * ./app.js [environment] + * + * environments: [development, production] + */ // sanity check var ENV = process.argv[2] if (ENV != 'development' && ENV != 'production'){ - console.error("\nnode app.js [environment]"); - console.error("environments: [development, production]\n"); - process.exit(1); + console.error("\nnode app.js [environment]"); + console.error("environments: [development, production]\n"); + process.exit(1); } -var _ = require('underscore'); +var _ = require('underscore') + , Step = require('step') + , cartoData = require('./lib/cartodb/carto_data'); + + // set environment specific variables global.settings = require(__dirname + '/config/settings'); @@ -28,6 +32,44 @@ var serverOptions = require('./lib/cartodb/server_options'); // boot var ws = new Windshaft.Server(serverOptions); + +/** + * Helper to allow access to the layer to be used in the maps infowindow popup. + */ +ws.get(serverOptions.base_url + '/infowindow', function(req, res){ + Step( + function(){ + serverOptions.getInfowindow(req, this); + }, + function(err, data){ + if (err){ + res.send(err.message, 400); + } else { + res.send({infowindow: data}, 200); + } + } + ); +}); + +/** + * Helper to allow access to metadata to be used in embedded maps. + */ +ws.get(serverOptions.base_url + '/map_metadata', function(req, res){ + Step( + function(){ + serverOptions.getMapMetadata(req, this); + }, + function(err, data){ + if (err){ + res.send(err.message, 400); + } else { + res.send({map_metadata: data}, 200); + } + } + ); +}); + + ws.listen(global.environment.windshaft_port); console.log("Windshaft tileserver started on port " + global.environment.windshaft_port); diff --git a/lib/cartodb/carto_data.js b/lib/cartodb/carto_data.js index 601b0159..ec437405 100644 --- a/lib/cartodb/carto_data.js +++ b/lib/cartodb/carto_data.js @@ -144,7 +144,7 @@ module.exports = function() { function(err, data) { if (err) throw err; var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table}); - + that.retrieve(that.table_metadata_db, redisKey, 'infowindow', this); }, function(err, data){ @@ -155,6 +155,26 @@ module.exports = function() { }; + me.getMapMetadata = function(req, callback){ + var that = this; + + Step( + function(){ + that.getDatabase(req, this); + }, + function(err, data) { + if (err) throw err; + var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table}); + + that.retrieve(that.table_metadata_db, redisKey, 'map_metadata', this); + }, + function(err, data){ + if (err) throw err; + callback(err, data); + } + ); + }; + /** * Make a HASH data access call to Redis diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index ea6c6356..c68756aa 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -79,5 +79,24 @@ me.getInfowindow = function(req, callback){ ); }; +/** + * Little helper method to get map metadata and return to client + * @param req + * @param callback + */ +me.getMapMetadata = function(req, callback){ + var that = this; + + Step( + function(){ + that.req2params(req, this); + }, + function(err, data){ + if (err) throw err; + cartoData.getMapMetadata(data, callback); + } + ); +}; + return me; }(); \ No newline at end of file