Extract rest of the middlewares from prepare contex

This commit is contained in:
Daniel García Aubert 2018-03-15 19:38:11 +01:00
parent 30dab7df9f
commit 7ff7b0c2d1
12 changed files with 87 additions and 53 deletions

View File

@ -1,11 +1,14 @@
var PSQL = require('cartodb-psql'); var PSQL = require('cartodb-psql');
var cors = require('../middleware/cors'); var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user'); var userMiddleware = require('../middleware/user');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const authorize = require('../middleware/authorize'); const authorize = require('../middleware/authorize');
const dbConnSetup = require('../middleware/db-conn-setup'); const dbConnSetup = require('../middleware/db-conn-setup');
function AnalysesController(prepareContext, pgConnection, authApi) { function AnalysesController(pgConnection, authApi) {
this.prepareContext = prepareContext;
this.pgConnection = pgConnection; this.pgConnection = pgConnection;
this.authApi = authApi; this.authApi = authApi;
} }
@ -17,7 +20,10 @@ AnalysesController.prototype.register = function (app) {
`${app.base_url_mapconfig}/analyses/catalog`, `${app.base_url_mapconfig}/analyses/catalog`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createPGClient(), createPGClient(),

View File

@ -2,6 +2,10 @@ const cors = require('../middleware/cors');
const userMiddleware = require('../middleware/user'); const userMiddleware = require('../middleware/user');
const allowQueryParams = require('../middleware/allow-query-params'); const allowQueryParams = require('../middleware/allow-query-params');
const vectorError = require('../middleware/vector-error'); const vectorError = require('../middleware/vector-error');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup'); const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize'); const authorize = require('../middleware/authorize');
const DataviewBackend = require('../backends/dataview'); const DataviewBackend = require('../backends/dataview');
@ -30,7 +34,7 @@ const SUPPORTED_FORMATS = {
* @param {AnalysisBackend} analysisBackend * @param {AnalysisBackend} analysisBackend
* @constructor * @constructor
*/ */
function LayergroupController(prepareContext, pgConnection, mapStore, tileBackend, previewBackend, attributesBackend, function LayergroupController(pgConnection, mapStore, tileBackend, previewBackend, attributesBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend, authApi) { surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend, authApi) {
this.pgConnection = pgConnection; this.pgConnection = pgConnection;
this.mapStore = mapStore; this.mapStore = mapStore;
@ -43,8 +47,6 @@ function LayergroupController(prepareContext, pgConnection, mapStore, tileBacken
this.dataviewBackend = new DataviewBackend(analysisBackend); this.dataviewBackend = new DataviewBackend(analysisBackend);
this.analysisStatusBackend = new AnalysisStatusBackend(); this.analysisStatusBackend = new AnalysisStatusBackend();
this.prepareContext = prepareContext;
this.authApi = authApi; this.authApi = authApi;
} }
@ -57,7 +59,10 @@ LayergroupController.prototype.register = function(app) {
`${basePath}/:token/:z/:x/:y@:scale_factor?x.:format`, `${basePath}/:token/:z/:x/:y@:scale_factor?x.:format`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -78,7 +83,10 @@ LayergroupController.prototype.register = function(app) {
`${basePath}/:token/:z/:x/:y.:format`, `${basePath}/:token/:z/:x/:y.:format`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -100,7 +108,10 @@ LayergroupController.prototype.register = function(app) {
distinguishLayergroupFromStaticRoute(), distinguishLayergroupFromStaticRoute(),
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -121,7 +132,10 @@ LayergroupController.prototype.register = function(app) {
`${basePath}/:token/:layer/attributes/:fid`, `${basePath}/:token/:layer/attributes/:fid`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -141,7 +155,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(['layer']), allowQueryParams(['layer']),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
@ -159,7 +176,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(['layer']), allowQueryParams(['layer']),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
@ -195,7 +215,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -213,7 +236,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -231,7 +257,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -249,7 +278,10 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
@ -266,7 +298,10 @@ LayergroupController.prototype.register = function(app) {
`${basePath}/:token/analysis/node/:nodeId`, `${basePath}/:token/analysis/node/:nodeId`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
analysisNodeStatus(this.analysisStatusBackend), analysisNodeStatus(this.analysisStatusBackend),

View File

@ -7,6 +7,10 @@ const ResourceLocator = require('../models/resource-locator');
const cors = require('../middleware/cors'); const cors = require('../middleware/cors');
const userMiddleware = require('../middleware/user'); const userMiddleware = require('../middleware/user');
const allowQueryParams = require('../middleware/allow-query-params'); const allowQueryParams = require('../middleware/allow-query-params');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup'); const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize'); const authorize = require('../middleware/authorize');
const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
@ -27,7 +31,7 @@ const LayergroupMetadata = require('../utils/layergroup-metadata');
* @param {StatsBackend} statsBackend * @param {StatsBackend} statsBackend
* @constructor * @constructor
*/ */
function MapController(prepareContext, pgConnection, templateMaps, mapBackend, metadataBackend, function MapController(pgConnection, templateMaps, mapBackend, metadataBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter, surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter,
statsBackend, authApi) { statsBackend, authApi) {
this.pgConnection = pgConnection; this.pgConnection = pgConnection;
@ -43,7 +47,6 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m
this.layergroupMetadata = new LayergroupMetadata(resourceLocator); this.layergroupMetadata = new LayergroupMetadata(resourceLocator);
this.statsBackend = statsBackend; this.statsBackend = statsBackend;
this.prepareContext = prepareContext;
this.authApi = authApi; this.authApi = authApi;
} }
@ -71,7 +74,10 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(['aggregation']), allowQueryParams(['aggregation']),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
initProfiler(isTemplateInstantiation), initProfiler(isTemplateInstantiation),

View File

@ -1,6 +1,10 @@
const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
const cors = require('../middleware/cors'); const cors = require('../middleware/cors');
const userMiddleware = require('../middleware/user'); const userMiddleware = require('../middleware/user');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup'); const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize'); const authorize = require('../middleware/authorize');
const allowQueryParams = require('../middleware/allow-query-params'); const allowQueryParams = require('../middleware/allow-query-params');
@ -29,7 +33,7 @@ function getRequestParams(locals) {
return params; return params;
} }
function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend, function NamedMapsController(namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection, authApi) { surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection, authApi) {
this.namedMapProviderCache = namedMapProviderCache; this.namedMapProviderCache = namedMapProviderCache;
this.tileBackend = tileBackend; this.tileBackend = tileBackend;
@ -37,7 +41,6 @@ function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend,
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
this.tablesExtentApi = tablesExtentApi; this.tablesExtentApi = tablesExtentApi;
this.metadataBackend = metadataBackend; this.metadataBackend = metadataBackend;
this.prepareContext = prepareContext;
this.pgConnection = pgConnection; this.pgConnection = pgConnection;
this.authApi = authApi; this.authApi = authApi;
} }
@ -51,7 +54,10 @@ NamedMapsController.prototype.register = function(app) {
`${base_url_templated}/:template_id/:layer/:z/:x/:y.(:format)`, `${base_url_templated}/:template_id/:layer/:z/:x/:y.(:format)`,
cors(), cors(),
userMiddleware(), userMiddleware(),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
getNamedMapProvider({ getNamedMapProvider({
@ -77,7 +83,10 @@ NamedMapsController.prototype.register = function(app) {
cors(), cors(),
userMiddleware(), userMiddleware(),
allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
this.prepareContext, locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
getNamedMapProvider({ getNamedMapProvider({

View File

@ -1,8 +1,8 @@
const { templateName } = require('../backends/template_maps'); const { templateName } = require('../backends/template_maps');
const cors = require('../middleware/cors'); const cors = require('../middleware/cors');
const userMiddleware = require('../middleware/user'); const userMiddleware = require('../middleware/user');
const localsMiddleware = require('../middleware/context/locals'); const localsMiddleware = require('../middleware/locals');
const credentialsMiddleware = require('../middleware/context/credentials'); const credentialsMiddleware = require('../middleware/credentials');
/** /**
* @param {AuthApi} authApi * @param {AuthApi} authApi

View File

@ -1,13 +0,0 @@
const locals = require('./locals');
const cleanUpQueryParams = require('./clean-up-query-params');
const layergroupToken = require('./layergroup-token');
const credentials = require('./credentials');
module.exports = function prepareContextMiddleware() {
return [
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
];
};

View File

@ -1,4 +1,4 @@
const LayergroupToken = require('../../models/layergroup-token'); const LayergroupToken = require('../models/layergroup-token');
const authErrorMessageTemplate = function (signer, user) { const authErrorMessageTemplate = function (signer, user) {
return `Cannot use map signature of user "${signer}" on db of user "${user}"`; return `Cannot use map signature of user "${signer}" on db of user "${user}"`;
}; };

View File

@ -49,8 +49,6 @@ var StatsBackend = require('./backends/stats');
const lzmaMiddleware = require('./middleware/lzma'); const lzmaMiddleware = require('./middleware/lzma');
const errorMiddleware = require('./middleware/error-middleware'); const errorMiddleware = require('./middleware/error-middleware');
const prepareContextMiddleware = require('./middleware/context');
module.exports = function(serverOptions) { module.exports = function(serverOptions) {
// Make stats client globally accessible // Make stats client globally accessible
global.statsClient = StatsClient.getInstance(serverOptions.statsd); global.statsClient = StatsClient.getInstance(serverOptions.statsd);
@ -215,16 +213,11 @@ module.exports = function(serverOptions) {
var versions = getAndValidateVersions(serverOptions); var versions = getAndValidateVersions(serverOptions);
const prepareContext = typeof serverOptions.req2params === 'function' ?
serverOptions.req2params :
prepareContextMiddleware();
/******************************************************************************************************************* /*******************************************************************************************************************
* Routing * Routing
******************************************************************************************************************/ ******************************************************************************************************************/
new controller.Layergroup( new controller.Layergroup(
prepareContext,
pgConnection, pgConnection,
mapStore, mapStore,
tileBackend, tileBackend,
@ -238,7 +231,6 @@ module.exports = function(serverOptions) {
).register(app); ).register(app);
new controller.Map( new controller.Map(
prepareContext,
pgConnection, pgConnection,
templateMaps, templateMaps,
mapBackend, mapBackend,
@ -252,7 +244,6 @@ module.exports = function(serverOptions) {
).register(app); ).register(app);
new controller.NamedMaps( new controller.NamedMaps(
prepareContext,
namedMapProviderCache, namedMapProviderCache,
tileBackend, tileBackend,
previewBackend, previewBackend,
@ -265,7 +256,7 @@ module.exports = function(serverOptions) {
new controller.NamedMapsAdmin(authApi, templateMaps).register(app); new controller.NamedMapsAdmin(authApi, templateMaps).register(app);
new controller.Analyses(prepareContext, pgConnection, authApi).register(app); new controller.Analyses(pgConnection, authApi).register(app);
new controller.ServerInfo(versions).register(app); new controller.ServerInfo(versions).register(app);

View File

@ -7,11 +7,11 @@ var PgConnection = require('../../../lib/cartodb/backends/pg_connection');
var AuthApi = require('../../../lib/cartodb/api/auth_api'); var AuthApi = require('../../../lib/cartodb/api/auth_api');
var TemplateMaps = require('../../../lib/cartodb/backends/template_maps'); var TemplateMaps = require('../../../lib/cartodb/backends/template_maps');
const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params'); const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/clean-up-query-params');
const authorizeMiddleware = require('../../../lib/cartodb/middleware/authorize'); const authorizeMiddleware = require('../../../lib/cartodb/middleware/authorize');
const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup'); const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup');
const credentialsMiddleware = require('../../../lib/cartodb/middleware/context/credentials'); const credentialsMiddleware = require('../../../lib/cartodb/middleware/credentials');
const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals'); const localsMiddleware = require('../../../lib/cartodb/middleware/locals');
var windshaft = require('windshaft'); var windshaft = require('windshaft');