Create new middlewares to init profiling and another to check JSON content-type

This commit is contained in:
Daniel García Aubert 2017-11-05 18:55:23 +01:00
parent 46289f27df
commit f9ba3c41d3
2 changed files with 40 additions and 36 deletions

View File

@ -49,22 +49,18 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m
module.exports = MapController; module.exports = MapController;
MapController.prototype.register = function(app) { MapController.prototype.register = function(app) {
app.get(app.base_url_mapconfig, this.composeCreateMapMiddleware()); const { base_url_mapconfig, base_url_templated } = app;
const useTemplate = true;
app.post(app.base_url_mapconfig, this.composeCreateMapMiddleware());
app.get(app.base_url_templated + '/:template_id/jsonp', this.composeCreateMapMiddleware({
useTemplate: true
}));
app.post(app.base_url_templated + '/:template_id', this.composeCreateMapMiddleware({
useTemplate: true
}));
app.get(base_url_mapconfig, this.composeCreateMapMiddleware());
app.post(base_url_mapconfig, this.composeCreateMapMiddleware());
app.get(`${base_url_templated}/:template_id/jsonp`, this.composeCreateMapMiddleware(useTemplate));
app.post(`${base_url_templated}/:template_id`, this.composeCreateMapMiddleware(useTemplate));
app.options(app.base_url_mapconfig, cors('Content-Type')); app.options(app.base_url_mapconfig, cors('Content-Type'));
}; };
MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = false } = {}) { MapController.prototype.composeCreateMapMiddleware = function (useTemplate = false) {
const isTemplateInstantiation = useTemplate;
const useTemplateHash = useTemplate; const useTemplateHash = useTemplate;
const includeQuery = !useTemplate; const includeQuery = !useTemplate;
const label = useTemplate ? 'NAMED MAP LAYERGROUP' : 'ANONYMOUS LAYERGROUP'; const label = useTemplate ? 'NAMED MAP LAYERGROUP' : 'ANONYMOUS LAYERGROUP';
@ -74,7 +70,9 @@ MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = f
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
useTemplate ? checkIntantiteLayergroup() : checkCreateLayergroup(), this.initProfiler(isTemplateInstantiation),
this.checkJsonContentType(),
useTemplate ? this.checkInstantiteLayergroup() : this.checkCreateLayergroup(),
useTemplate ? this.getTemplate() : this.prepareAdapterMapConfig(), useTemplate ? this.getTemplate() : this.prepareAdapterMapConfig(),
useTemplate ? this.instantiateLayergroup() : this.createLayergroup(), useTemplate ? this.instantiateLayergroup() : this.createLayergroup(),
this.incrementMapViewCount(), this.incrementMapViewCount(),
@ -89,20 +87,32 @@ MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = f
this.setAnalysesMetadataToLayergroup(includeQuery), this.setAnalysesMetadataToLayergroup(includeQuery),
this.setTurboCartoMetadataToLayergroup(), this.setTurboCartoMetadataToLayergroup(),
this.setSurrogateKeyHeader(), this.setSurrogateKeyHeader(),
sendResponse(), this.sendResponse(),
augmentError({ label, addContext }) this.augmentError({ label, addContext })
]; ];
}; };
MapController.prototype.initProfiler = function (isTemplateInstantiation) {
const operation = isTemplateInstantiation ? 'instance_template' : 'createmap';
function checkCreateLayergroup () { return function initProfilerMiddleware (req, res, next) {
return function checkCreateLayergroupMiddleware (req, res, next) { req.profiler.start(`windshaft-cartodb.${operation}_${req.method.toLowerCase()}`);
req.profiler.start(`windshaft.createmap_${req.method.toLowerCase()}`); next();
};
};
MapController.prototype.checkJsonContentType = function () {
return function checkJsonContentTypeMiddleware(req, res, next) {
if (req.method === 'POST' && !req.is('application/json')) { if (req.method === 'POST' && !req.is('application/json')) {
return next(new Error('layergroup POST data must be of type application/json')); return next(new Error('POST data must be of type application/json'));
} }
next();
};
};
MapController.prototype.checkCreateLayergroup = function () {
return function checkCreateLayergroupMiddleware (req, res, next) {
if (req.method === 'GET') { if (req.method === 'GET') {
const { config } = res.locals; const { config } = res.locals;
@ -119,17 +129,10 @@ function checkCreateLayergroup () {
return next(); return next();
}; };
} };
function checkIntantiteLayergroup () {
return function checkIntantiteLayergroupMiddleware(req, res, next) {
// jshint maxcomplexity: 7
req.profiler.start(`windshaft-cartodb.instance_template_${req.method.toLowerCase()}`);
if (req.method === 'POST' && !req.is('application/json')) {
return next(new Error('Template POST data must be of type application/json'));
}
MapController.prototype.checkInstantiteLayergroup = function () {
return function checkInstantiteLayergroupMiddleware(req, res, next) {
if (req.method === 'GET') { if (req.method === 'GET') {
const { callback, config } = req.query; const { callback, config } = req.query;
@ -148,7 +151,7 @@ function checkIntantiteLayergroup () {
return next(); return next();
}; };
} };
MapController.prototype.prepareAdapterMapConfig = function () { MapController.prototype.prepareAdapterMapConfig = function () {
return function prepareAdapterMapConfigMiddleware(req, res, next) { return function prepareAdapterMapConfigMiddleware(req, res, next) {
@ -191,7 +194,8 @@ MapController.prototype.createLayergroup = function () {
const { context, user } = res.locals; const { context, user } = res.locals;
const datasource = context.datasource || Datasource.EmptyDatasource(); const datasource = context.datasource || Datasource.EmptyDatasource();
const mapconfig = new MapConfig(requestMapConfig, datasource); const mapconfig = new MapConfig(requestMapConfig, datasource);
const mapconfigProvider = new CreateLayergroupMapConfigProvider(mapconfig, user, this.userLimitsApi, res.locals); const mapconfigProvider =
new CreateLayergroupMapConfigProvider(mapconfig, user, this.userLimitsApi, res.locals);
res.locals.mapconfig = mapconfig; res.locals.mapconfig = mapconfig;
res.locals.analysesResults = context.analysesResults; res.locals.analysesResults = context.analysesResults;
@ -265,7 +269,7 @@ MapController.prototype.instantiateLayergroup = function () {
}.bind(this); }.bind(this);
}; };
function sendResponse() { MapController.prototype.sendResponse = function () {
return function sendResponseMiddleware (req, res) { return function sendResponseMiddleware (req, res) {
const { layergroup } = res.locals; const { layergroup } = res.locals;
@ -277,7 +281,7 @@ function sendResponse() {
res.json(layergroup); res.json(layergroup);
} }
}; };
} };
MapController.prototype.incrementMapViewCount = function () { MapController.prototype.incrementMapViewCount = function () {
return function incrementMapViewCountMiddleware(req, res, next) { return function incrementMapViewCountMiddleware(req, res, next) {
@ -562,7 +566,7 @@ MapController.prototype.addWidgetsUrl = function(username, layergroup, mapConfig
} }
}; };
function augmentError (options) { MapController.prototype.augmentError = function (options) {
const { addContext = false, label = 'MAPS CONTROLLER' } = options; const { addContext = false, label = 'MAPS CONTROLLER' } = options;
return function augmentErrorMiddleware (err, req, res, next) { return function augmentErrorMiddleware (err, req, res, next) {
@ -576,7 +580,7 @@ function augmentError (options) {
next(err); next(err);
}; };
} };
function populateError(err, mapConfig) { function populateError(err, mapConfig) {
var error = new Error(err.message); var error = new Error(err.message);

View File

@ -28,7 +28,7 @@ describe('multilayer error cases', function() {
}, {}, function(res) { }, {}, function(res) {
assert.equal(res.statusCode, 400, res.body); assert.equal(res.statusCode, 400, res.body);
var parsedBody = JSON.parse(res.body); var parsedBody = JSON.parse(res.body);
assert.deepEqual(parsedBody.errors, ["layergroup POST data must be of type application/json"]); assert.deepEqual(parsedBody.errors, ["POST data must be of type application/json"]);
done(); done();
}); });
}); });