From c6635f63c10fd3a44436d8ee9634a60ce441778b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 7 Mar 2018 15:39:59 +0100 Subject: [PATCH] Unify layer and tile middlewares --- lib/cartodb/controllers/layergroup.js | 42 +++++---------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 341dc5f3..2b37dade 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -48,7 +48,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), this.prepareContext, getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), - getTile(this.tileBackend), + getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), setLastModifiedHeader(), getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -67,7 +67,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), this.prepareContext, getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), - getTile(this.tileBackend), + getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), setLastModifiedHeader(), getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -87,7 +87,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), this.prepareContext, getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), - layer(this.tileBackend), + getTile(this.tileBackend, 'maplayer_tile'), setCacheControlHeader(), setLastModifiedHeader(), getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -394,37 +394,9 @@ function parseFormat (format = '') { return formatStat; } -function getTile (tileBackend) { - return function tileMiddleware (req, res, next) { - req.profiler.start('windshaft.map_tile'); - - const { mapConfigProvider } = res.locals; - const params = getRequestParams(res.locals); - - tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats = {}) => { - req.profiler.add(stats); - - if (err) { - return next(err); - } - - if (headers) { - res.set(headers); - } - - const formatStat = parseFormat(req.params.format); - - res.statusCode = getStatusCode(tile, formatStat); - res.body = tile; - - next(); - }); - }; -} - -function layer (tileBackend) { - return function layerMiddleware (req, res, next) { - req.profiler.start('windshaft.maplayer_tile'); +function getTile (tileBackend, profileLabel = 'tile') { + return function getTileMiddleware (req, res, next) { + req.profiler.start(`windshaft.${profileLabel}`); const { mapConfigProvider } = res.locals; const params = getRequestParams(res.locals); @@ -647,7 +619,7 @@ function incrementErrorMetrics (statsClient) { next(err); }; -}; +} function tileError () { return function tileErrorMiddleware (err, req, res, next) {