Do not attach middleware to LayergroupController classs

This commit is contained in:
Daniel García Aubert 2018-03-07 15:20:47 +01:00
parent 90aaed0f2c
commit c8e8317ea4

View File

@ -47,17 +47,17 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.tile(this.tileBackend), tile(this.tileBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.incrementSuccessMetrics(global.statsClient), incrementSuccessMetrics(global.statsClient),
this.sendResponse(), sendResponse(),
this.incrementErrorMetrics(global.statsClient), incrementErrorMetrics(global.statsClient),
this.tileError(), tileError(),
vectorError() vectorError()
); );
@ -66,17 +66,17 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.tile(this.tileBackend), tile(this.tileBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.incrementSuccessMetrics(global.statsClient), incrementSuccessMetrics(global.statsClient),
this.sendResponse(), sendResponse(),
this.incrementErrorMetrics(global.statsClient), incrementErrorMetrics(global.statsClient),
this.tileError(), tileError(),
vectorError() vectorError()
); );
@ -86,17 +86,17 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
validateLayerRoute(), validateLayerRoute(),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.layer(this.tileBackend), layer(this.tileBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.incrementSuccessMetrics(global.statsClient), incrementSuccessMetrics(global.statsClient),
this.sendResponse(), sendResponse(),
this.incrementErrorMetrics(global.statsClient), incrementErrorMetrics(global.statsClient),
this.tileError(), tileError(),
vectorError() vectorError()
); );
@ -105,14 +105,14 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.attributes(this.attributesBackend), attributes(this.attributesBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
const forcedFormat = 'png'; const forcedFormat = 'png';
@ -123,14 +123,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(['layer']), allowQueryParams(['layer']),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
this.center(this.previewBackend), center(this.previewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
app.get( app.get(
@ -139,14 +139,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(['layer']), allowQueryParams(['layer']),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
this.bbox(this.previewBackend), bbox(this.previewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
// Undocumented/non-supported API endpoint methods. // Undocumented/non-supported API endpoint methods.
@ -173,14 +173,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.getDataview(this.dataviewBackend), getDataview(this.dataviewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
app.get( app.get(
@ -189,14 +189,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.getDataview(this.dataviewBackend), getDataview(this.dataviewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
app.get( app.get(
@ -205,14 +205,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.dataviewSearch(this.dataviewBackend), dataviewSearch(this.dataviewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
app.get( app.get(
@ -221,14 +221,14 @@ LayergroupController.prototype.register = function(app) {
userMiddleware, userMiddleware,
allowQueryParams(allowedDataviewQueryParams), allowQueryParams(allowedDataviewQueryParams),
this.prepareContext, this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.dataviewSearch(this.dataviewBackend), dataviewSearch(this.dataviewBackend),
this.setCacheControlHeader(), setCacheControlHeader(),
this.setLastModifiedHeader(), setLastModifiedHeader(),
this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), getAffectedTables(this.layergroupAffectedTables, this.pgConnection),
this.setCacheChannelHeader(), setCacheChannelHeader(),
this.setSurrogateKeyHeader(this.surrogateKeysCache), setSurrogateKeyHeader(this.surrogateKeysCache),
this.sendResponse() sendResponse()
); );
app.get( app.get(
@ -236,8 +236,8 @@ LayergroupController.prototype.register = function(app) {
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
this.analysisNodeStatus(this.analysisStatusBackend), analysisNodeStatus(this.analysisStatusBackend),
this.sendResponse() sendResponse()
); );
}; };
@ -251,7 +251,7 @@ function validateLayerRoute () {
}; };
} }
LayergroupController.prototype.analysisNodeStatus = function (analysisStatusBackend) { function analysisNodeStatus (analysisStatusBackend) {
return function analysisNodeStatusMiddleware(req, res, next) { return function analysisNodeStatusMiddleware(req, res, next) {
analysisStatusBackend.getNodeStatus(res.locals, (err, nodeStatus, stats = {}) => { analysisStatusBackend.getNodeStatus(res.locals, (err, nodeStatus, stats = {}) => {
req.profiler.add(stats); req.profiler.add(stats);
@ -271,7 +271,7 @@ LayergroupController.prototype.analysisNodeStatus = function (analysisStatusBack
next(); next();
}); });
}; };
}; }
function getRequestParams(locals) { function getRequestParams(locals) {
const params = Object.assign({}, locals); const params = Object.assign({}, locals);
@ -282,7 +282,7 @@ function getRequestParams(locals) {
return params; return params;
} }
LayergroupController.prototype.getMapStoreMapConfigProvider = function (mapStore, userLimitsApi, forcedFormat = null) { function getMapStoreMapConfigProvider (mapStore, userLimitsApi, forcedFormat = null) {
return function getMapStoreMapConfigProviderMiddleware (req, res, next) { return function getMapStoreMapConfigProviderMiddleware (req, res, next) {
const { user } = res.locals; const { user } = res.locals;
@ -306,9 +306,9 @@ LayergroupController.prototype.getMapStoreMapConfigProvider = function (mapStore
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.getDataview = function (dataviewBackend) { function getDataview (dataviewBackend) {
return function getDataviewMiddleware (req, res, next) { return function getDataviewMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals; const { user, mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals); const params = getRequestParams(res.locals);
@ -326,9 +326,9 @@ LayergroupController.prototype.getDataview = function (dataviewBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.dataviewSearch = function (dataviewBackend) { function dataviewSearch (dataviewBackend) {
return function dataviewSearchMiddleware (req, res, next) { return function dataviewSearchMiddleware (req, res, next) {
const { user, dataviewName, mapConfigProvider } = res.locals; const { user, dataviewName, mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals); const params = getRequestParams(res.locals);
@ -346,9 +346,9 @@ LayergroupController.prototype.dataviewSearch = function (dataviewBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.attributes = function (attributesBackend) { function attributes (attributesBackend) {
return function attributesMiddleware (req, res, next) { return function attributesMiddleware (req, res, next) {
req.profiler.start('windshaft.maplayer_attribute'); req.profiler.start('windshaft.maplayer_attribute');
@ -368,7 +368,7 @@ LayergroupController.prototype.attributes = function (attributesBackend) {
next(); next();
}); });
}; };
}; }
function getStatusCode(tile, format){ function getStatusCode(tile, format){
return tile.length === 0 && format === 'mvt'? 204 : 200; return tile.length === 0 && format === 'mvt'? 204 : 200;
@ -394,7 +394,7 @@ function parseFormat (format = '') {
return formatStat; return formatStat;
} }
LayergroupController.prototype.tile = function (tileBackend) { function tile (tileBackend) {
return function tileMiddleware (req, res, next) { return function tileMiddleware (req, res, next) {
req.profiler.start('windshaft.map_tile'); req.profiler.start('windshaft.map_tile');
@ -420,9 +420,9 @@ LayergroupController.prototype.tile = function (tileBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.layer = function (tileBackend) { function layer (tileBackend) {
return function layerMiddleware (req, res, next) { return function layerMiddleware (req, res, next) {
req.profiler.start('windshaft.maplayer_tile'); req.profiler.start('windshaft.maplayer_tile');
@ -448,9 +448,9 @@ LayergroupController.prototype.layer = function (tileBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.center = function (previewBackend) { function center (previewBackend) {
return function centerMiddleware (req, res, next) { return function centerMiddleware (req, res, next) {
const width = +req.params.width; const width = +req.params.width;
const height = +req.params.height; const height = +req.params.height;
@ -483,9 +483,9 @@ LayergroupController.prototype.center = function (previewBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.bbox = function (previewBackend) { function bbox (previewBackend) {
return function bboxMiddleware (req, res, next) { return function bboxMiddleware (req, res, next) {
const width = +req.params.width; const width = +req.params.width;
const height = +req.params.height; const height = +req.params.height;
@ -518,9 +518,9 @@ LayergroupController.prototype.bbox = function (previewBackend) {
next(); next();
}); });
}; };
}; }
LayergroupController.prototype.setLastModifiedHeader = function () { function setLastModifiedHeader () {
return function setLastModifiedHeaderMiddleware (req, res, next) { return function setLastModifiedHeaderMiddleware (req, res, next) {
let { cache_buster: cacheBuster } = res.locals; let { cache_buster: cacheBuster } = res.locals;
@ -532,17 +532,17 @@ LayergroupController.prototype.setLastModifiedHeader = function () {
next(); next();
}; };
}; }
LayergroupController.prototype.setCacheControlHeader = function () { function setCacheControlHeader () {
return function setCacheControlHeaderMiddleware (req, res, next) { return function setCacheControlHeaderMiddleware (req, res, next) {
res.set('Cache-Control', 'public,max-age=31536000'); res.set('Cache-Control', 'public,max-age=31536000');
next(); next();
}; };
}; }
LayergroupController.prototype.getAffectedTables = function (layergroupAffectedTables, pgConnection) { function getAffectedTables (layergroupAffectedTables, pgConnection) {
return function getAffectedTablesMiddleware (req, res, next) { return function getAffectedTablesMiddleware (req, res, next) {
const { user, dbname, token, mapconfig } = res.locals; const { user, dbname, token, mapconfig } = res.locals;
@ -583,9 +583,9 @@ LayergroupController.prototype.getAffectedTables = function (layergroupAffectedT
}); });
}); });
}; };
}; }
LayergroupController.prototype.setCacheChannelHeader = function () { function setCacheChannelHeader () {
return function setCacheChannelHeaderMiddleware (req, res, next) { return function setCacheChannelHeaderMiddleware (req, res, next) {
const { affectedTables } = res.locals; const { affectedTables } = res.locals;
@ -595,9 +595,9 @@ LayergroupController.prototype.setCacheChannelHeader = function () {
next(); next();
}; };
}; }
LayergroupController.prototype.setSurrogateKeyHeader = function (surrogateKeysCache) { function setSurrogateKeyHeader (surrogateKeysCache) {
return function setSurrogateKeyHeaderMiddleware (req, res, next) { return function setSurrogateKeyHeaderMiddleware (req, res, next) {
const { affectedTables } = res.locals; const { affectedTables } = res.locals;
@ -607,9 +607,9 @@ LayergroupController.prototype.setSurrogateKeyHeader = function (surrogateKeysCa
next(); next();
}; };
}; }
LayergroupController.prototype.incrementSuccessMetrics = function (statsClient) { function incrementSuccessMetrics (statsClient) {
return function incrementSuccessMetricsMiddleware (req, res, next) { return function incrementSuccessMetricsMiddleware (req, res, next) {
const formatStat = parseFormat(req.params.format); const formatStat = parseFormat(req.params.format);
@ -618,9 +618,9 @@ LayergroupController.prototype.incrementSuccessMetrics = function (statsClient)
next(); next();
}; };
}; }
LayergroupController.prototype.sendResponse = function () { function sendResponse () {
return function sendResponseMiddleware (req, res) { return function sendResponseMiddleware (req, res) {
req.profiler.done('res'); req.profiler.done('res');
@ -636,9 +636,9 @@ LayergroupController.prototype.sendResponse = function () {
res.send(res.body); res.send(res.body);
} }
}; };
}; }
LayergroupController.prototype.incrementErrorMetrics = function (statsClient) { function incrementErrorMetrics (statsClient) {
return function incrementErrorMetricsMiddleware (err, req, res, next) { return function incrementErrorMetricsMiddleware (err, req, res, next) {
const formatStat = parseFormat(req.params.format); const formatStat = parseFormat(req.params.format);
@ -649,7 +649,7 @@ LayergroupController.prototype.incrementErrorMetrics = function (statsClient) {
}; };
}; };
LayergroupController.prototype.tileError = function () { function tileError () {
return function tileErrorMiddleware (err, req, res, next) { return function tileErrorMiddleware (err, req, res, next) {
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68 // See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
let errMsg = err.message ? ( '' + err.message ) : ( '' + err ); let errMsg = err.message ? ( '' + err.message ) : ( '' + err );
@ -666,4 +666,4 @@ LayergroupController.prototype.tileError = function () {
next(err); next(err);
}; };
}; }