diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 437a37cc..1d419db7 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -41,9 +41,18 @@ NamedMapsController.prototype.tile = function(req, res) { var template; var layergroupConfig; var layergroupId; - var fakeReq; + var params; var cacheChannel; + var layergroupDecorator = { + beforeLayergroupCreate: function(requestMapConfig, callback) { + self.serverOptions.beforeLayergroupCreate(req, requestMapConfig, callback); + }, + afterLayergroupCreate: function(layergroup, response, callback) { + self.serverOptions.afterLayergroupCreate(req, layergroup, response, callback); + } + }; + step( function reqParams() { self.app.req2params(req, this); @@ -106,22 +115,14 @@ NamedMapsController.prototype.tile = function(req, res) { function prepareLayergroup(err, layergroup) { assert.ifError(err); layergroupConfig = layergroup; - fakeReq = { - query: {}, - params: _.extend({}, req.params, { - user: req.params.user - }), - headers: _.clone(req.headers), - context: _.clone(req.context), - method: req.method, - res: res, - profiler: req.profiler - }; - self.serverOptions.setDBParams(cdbUser, fakeReq.params, this); + params = _.extend({}, req.params, { + user: req.params.user + }); + self.serverOptions.setDBParams(cdbUser, params, this); }, function setApiKey(err){ assert.ifError(err); - self.mapBackend.createLayergroup(layergroupConfig, fakeReq, this); + self.mapBackend.createLayergroup(layergroupConfig, params, layergroupDecorator, this); }, function prepareResponse(err, layergroup) { assert.ifError(err); @@ -139,9 +140,12 @@ NamedMapsController.prototype.tile = function(req, res) { assert.ifError(err); req.params.token = layergroupId; - self.mapBackend.getTileOrGrid(req, res, this); + self.mapBackend.getTileOrGrid(req.params, this); }, - function handleImage(err, req, res, tile, headers) { + function handleImage(err, tile, headers, stats) { + if (req.profiler) { + req.profiler.add(stats); + } if (err) { if (!err.error) { err.error = err.message; @@ -344,7 +348,7 @@ NamedMapsController.prototype.jsonp = function(req, res) { req.profiler.start('windshaft-cartodb.instance_template_get'); } step( - function instantiateTemplate() { + function jsonp$instantiateTemplate() { if ( req.query.callback === undefined || req.query.callback.length === 0) { throw new Error('callback parameter should be present and be a function name'); } @@ -370,10 +374,21 @@ NamedMapsController.prototype.instantiateTemplate = function(req, res, template_ this.app.doCORS(res); + var layergroupDecorator = { + beforeLayergroupCreate: function(requestMapConfig, callback) { + self.serverOptions.beforeLayergroupCreate(req, requestMapConfig, callback); + }, + afterLayergroupCreate: function(layergroup, response, callback) { + self.serverOptions.afterLayergroupCreate(req, layergroup, response, callback); + } + }; + var template; var layergroup; - var fakereq; // used for call to createLayergroup var cdbuser = cdbRequest.userByReq(req); + var params = { + user: req.params.user + }; // Format of template_id: []@ var tpl_id = templateName(req.params.template_id); var auth_token = req.query.auth_token; @@ -416,18 +431,7 @@ NamedMapsController.prototype.instantiateTemplate = function(req, res, template_ if ( req.profiler ) req.profiler.done('TemplateMaps_instance'); if ( err ) throw err; layergroup = instance; - fakereq = { - query: {}, - params: { - user: req.params.user - }, - headers: _.clone(req.headers), - context: _.clone(req.context), - method: req.method, - res: res, - profiler: req.profiler - }; - self.serverOptions.setDBParams(cdbuser, fakereq.params, this); + self.serverOptions.setDBParams(cdbuser, params, this); }, function setApiKey(err){ if ( req.profiler ) req.profiler.done('setDBParams'); @@ -437,8 +441,8 @@ NamedMapsController.prototype.instantiateTemplate = function(req, res, template_ function createLayergroup(err, val) { if ( req.profiler ) req.profiler.done('getUserMapKey'); if ( err ) throw err; - fakereq.params.api_key = val; - self.mapBackend.createLayergroup(layergroup, fakereq, this); + params.api_key = val; + self.mapBackend.createLayergroup(layergroup, params, layergroupDecorator, this); }, function prepareResponse(err, layergroup) { if ( err ) { diff --git a/lib/cartodb/controllers/named_static_maps.js b/lib/cartodb/controllers/named_static_maps.js index f9eeceaa..1a64762f 100644 --- a/lib/cartodb/controllers/named_static_maps.js +++ b/lib/cartodb/controllers/named_static_maps.js @@ -36,8 +36,18 @@ NamedStaticMapsController.prototype.named = function(req, res) { var layergroupConfig; var layergroupId; var fakeReq; + var params; var cacheChannel; + var layergroupDecorator = { + beforeLayergroupCreate: function(requestMapConfig, callback) { + self.serverOptions.beforeLayergroupCreate(req, requestMapConfig, callback); + }, + afterLayergroupCreate: function(layergroup, response, callback) { + self.serverOptions.afterLayergroupCreate(req, layergroup, response, callback); + } + }; + step( function reqParams() { self.app.req2params(req, this); @@ -100,22 +110,14 @@ NamedStaticMapsController.prototype.named = function(req, res) { function prepareLayergroup(err, layergroup) { assert.ifError(err); layergroupConfig = layergroup; - fakeReq = { - query: {}, - params: { - user: req.params.user - }, - headers: _.clone(req.headers), - context: _.clone(req.context), - method: req.method, - res: res, - profiler: req.profiler + params = { + user: req.params.user }; - self.serverOptions.setDBParams(cdbUser, fakeReq.params, this); + self.serverOptions.setDBParams(cdbUser, params, this); }, function setApiKey(err){ assert.ifError(err); - self.mapBackend.createLayergroup(layergroupConfig, fakeReq, this); + self.mapBackend.createLayergroup(layergroupConfig, params, layergroupDecorator, this); }, function prepareResponse(err, layergroup) { assert.ifError(err); @@ -165,21 +167,18 @@ NamedStaticMapsController.prototype.named = function(req, res) { function getImage(err, imageOpts) { assert.ifError(err); - var staticImageReq = { - headers: _.clone(fakeReq.headers), - params: _.extend(_.clone(fakeReq.params), { - token: layergroupId, - format: req.params.format - }) - }; + var imageParams = _.extend({}, params, { + token: layergroupId, + format: req.params.format + }); var width = +req.params.width; var height = +req.params.height; if (!_.isUndefined(imageOpts.zoom) && imageOpts.center) { - self.staticMapBackend.getImage(staticImageReq, width, height, imageOpts.zoom, imageOpts.center, this); + self.staticMapBackend.getImage(imageParams, width, height, imageOpts.zoom, imageOpts.center, this); } else { - self.staticMapBackend.getImage(staticImageReq, width, height, imageOpts.bounds, this); + self.staticMapBackend.getImage(imageParams, width, height, imageOpts.bounds, this); } }, function handleImage(err, image, headers, stats) {