From afff06c7e6d29721dc80bd4ccfad32c4fdbc1e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Mar 2018 15:33:20 +0100 Subject: [PATCH] Extract db-conn-setup middleware for prepare-context --- lib/cartodb/controllers/analyses.js | 5 ++++- lib/cartodb/controllers/layergroup.js | 12 ++++++++++++ lib/cartodb/controllers/map.js | 2 ++ lib/cartodb/controllers/named_maps.js | 6 +++++- lib/cartodb/middleware/context/index.js | 6 ++---- .../middleware/{context => }/db-conn-setup.js | 0 lib/cartodb/server.js | 7 ++++--- test/unit/cartodb/prepare-context.test.js | 2 +- 8 files changed, 30 insertions(+), 10 deletions(-) rename lib/cartodb/middleware/{context => }/db-conn-setup.js (100%) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 1a78208f..a04489c4 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,9 +1,11 @@ var PSQL = require('cartodb-psql'); var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); +const dbConnSetup = require('../middleware/db-conn-setup'); -function AnalysesController(prepareContext) { +function AnalysesController(prepareContext, pgConnection) { this.prepareContext = prepareContext; + this.pgConnection = pgConnection; } module.exports = AnalysesController; @@ -14,6 +16,7 @@ AnalysesController.prototype.register = function (app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createPGClient(), getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }), getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 2d0bf710..30c425f4 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -2,6 +2,7 @@ const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); +const dbConnSetup = require('../middleware/db-conn-setup'); const DataviewBackend = require('../backends/dataview'); const AnalysisStatusBackend = require('../backends/analysis-status'); const MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider'); @@ -55,6 +56,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), @@ -74,6 +76,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), @@ -94,6 +97,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'maplayer_tile'), setCacheControlHeader(), @@ -113,6 +117,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getFeatureAttributes(this.attributesBackend), setCacheControlHeader(), @@ -131,6 +136,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByCenter(this.previewBackend), setCacheControlHeader(), @@ -147,6 +153,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByBoundingBox(this.previewBackend), setCacheControlHeader(), @@ -181,6 +188,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), setCacheControlHeader(), @@ -197,6 +205,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), setCacheControlHeader(), @@ -213,6 +222,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), @@ -229,6 +239,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), @@ -244,6 +255,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), analysisNodeStatus(this.analysisStatusBackend), sendResponse() ); diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 87e8b6be..38f3845f 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -7,6 +7,7 @@ const ResourceLocator = require('../models/resource-locator'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); +const dbConnSetup = require('../middleware/db-conn-setup'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); @@ -69,6 +70,7 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal userMiddleware(), allowQueryParams(['aggregation']), this.prepareContext, + dbConnSetup(this.pgConnection), initProfiler(isTemplateInstantiation), checkJsonContentType(), this.getCreateMapMiddlewares(useTemplate), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index cda71346..720f79c1 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -1,6 +1,7 @@ const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); +const dbConnSetup = require('../middleware/db-conn-setup'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); @@ -28,7 +29,7 @@ function getRequestParams(locals) { } function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend, - surrogateKeysCache, tablesExtentApi, metadataBackend) { + surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection) { this.namedMapProviderCache = namedMapProviderCache; this.tileBackend = tileBackend; this.previewBackend = previewBackend; @@ -36,6 +37,7 @@ function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, this.tablesExtentApi = tablesExtentApi; this.metadataBackend = metadataBackend; this.prepareContext = prepareContext; + this.pgConnection = pgConnection; } module.exports = NamedMapsController; @@ -48,6 +50,7 @@ NamedMapsController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, label: 'NAMED_MAP_TILE' @@ -72,6 +75,7 @@ NamedMapsController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), this.prepareContext, + dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, label: 'STATIC_VIZ_MAP', forcedFormat: 'png' diff --git a/lib/cartodb/middleware/context/index.js b/lib/cartodb/middleware/context/index.js index 70465895..390dcb5e 100644 --- a/lib/cartodb/middleware/context/index.js +++ b/lib/cartodb/middleware/context/index.js @@ -3,15 +3,13 @@ const cleanUpQueryParams = require('./clean-up-query-params'); const layergroupToken = require('./layergroup-token'); const credentials = require('./credentials'); const authorize = require('./authorize'); -const dbConnSetup = require('./db-conn-setup'); -module.exports = function prepareContextMiddleware(authApi, pgConnection) { +module.exports = function prepareContextMiddleware(authApi) { return [ locals(), cleanUpQueryParams(), layergroupToken(), credentials(), - authorize(authApi), - dbConnSetup(pgConnection) + authorize(authApi) ]; }; diff --git a/lib/cartodb/middleware/context/db-conn-setup.js b/lib/cartodb/middleware/db-conn-setup.js similarity index 100% rename from lib/cartodb/middleware/context/db-conn-setup.js rename to lib/cartodb/middleware/db-conn-setup.js diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 532d155b..d2187930 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -217,7 +217,7 @@ module.exports = function(serverOptions) { const prepareContext = typeof serverOptions.req2params === 'function' ? serverOptions.req2params : - prepareContextMiddleware(authApi, pgConnection); + prepareContextMiddleware(authApi); /******************************************************************************************************************* * Routing @@ -256,12 +256,13 @@ module.exports = function(serverOptions) { previewBackend, surrogateKeysCache, tablesExtentApi, - metadataBackend + metadataBackend, + pgConnection ).register(app); new controller.NamedMapsAdmin(authApi, templateMaps).register(app); - new controller.Analyses(prepareContext).register(app); + new controller.Analyses(prepareContext, pgConnection).register(app); new controller.ServerInfo(versions).register(app); diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index 283c4fdc..19afce21 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -9,7 +9,7 @@ var TemplateMaps = require('../../../lib/cartodb/backends/template_maps'); const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params'); const authorizeMiddleware = require('../../../lib/cartodb/middleware/context/authorize'); -const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/context/db-conn-setup'); +const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup'); const credentialsMiddleware = require('../../../lib/cartodb/middleware/context/credentials'); const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals');