Create new middlewares to init profiling and another to check JSON content-type
This commit is contained in:
parent
46289f27df
commit
f9ba3c41d3
@ -49,22 +49,18 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m
|
||||
module.exports = MapController;
|
||||
|
||||
MapController.prototype.register = function(app) {
|
||||
app.get(app.base_url_mapconfig, this.composeCreateMapMiddleware());
|
||||
|
||||
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
|
||||
}));
|
||||
const { base_url_mapconfig, base_url_templated } = app;
|
||||
const 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'));
|
||||
};
|
||||
|
||||
MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = false } = {}) {
|
||||
MapController.prototype.composeCreateMapMiddleware = function (useTemplate = false) {
|
||||
const isTemplateInstantiation = useTemplate;
|
||||
const useTemplateHash = useTemplate;
|
||||
const includeQuery = !useTemplate;
|
||||
const label = useTemplate ? 'NAMED MAP LAYERGROUP' : 'ANONYMOUS LAYERGROUP';
|
||||
@ -74,7 +70,9 @@ MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = f
|
||||
cors(),
|
||||
userMiddleware,
|
||||
this.prepareContext,
|
||||
useTemplate ? checkIntantiteLayergroup() : checkCreateLayergroup(),
|
||||
this.initProfiler(isTemplateInstantiation),
|
||||
this.checkJsonContentType(),
|
||||
useTemplate ? this.checkInstantiteLayergroup() : this.checkCreateLayergroup(),
|
||||
useTemplate ? this.getTemplate() : this.prepareAdapterMapConfig(),
|
||||
useTemplate ? this.instantiateLayergroup() : this.createLayergroup(),
|
||||
this.incrementMapViewCount(),
|
||||
@ -89,20 +87,32 @@ MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = f
|
||||
this.setAnalysesMetadataToLayergroup(includeQuery),
|
||||
this.setTurboCartoMetadataToLayergroup(),
|
||||
this.setSurrogateKeyHeader(),
|
||||
sendResponse(),
|
||||
augmentError({ label, addContext })
|
||||
this.sendResponse(),
|
||||
this.augmentError({ label, addContext })
|
||||
];
|
||||
};
|
||||
|
||||
MapController.prototype.initProfiler = function (isTemplateInstantiation) {
|
||||
const operation = isTemplateInstantiation ? 'instance_template' : 'createmap';
|
||||
|
||||
function checkCreateLayergroup () {
|
||||
return function checkCreateLayergroupMiddleware (req, res, next) {
|
||||
req.profiler.start(`windshaft.createmap_${req.method.toLowerCase()}`);
|
||||
return function initProfilerMiddleware (req, res, next) {
|
||||
req.profiler.start(`windshaft-cartodb.${operation}_${req.method.toLowerCase()}`);
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
MapController.prototype.checkJsonContentType = function () {
|
||||
return function checkJsonContentTypeMiddleware(req, res, next) {
|
||||
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') {
|
||||
const { config } = res.locals;
|
||||
|
||||
@ -119,17 +129,10 @@ function checkCreateLayergroup () {
|
||||
|
||||
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') {
|
||||
const { callback, config } = req.query;
|
||||
|
||||
@ -148,7 +151,7 @@ function checkIntantiteLayergroup () {
|
||||
|
||||
return next();
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
MapController.prototype.prepareAdapterMapConfig = function () {
|
||||
return function prepareAdapterMapConfigMiddleware(req, res, next) {
|
||||
@ -191,7 +194,8 @@ MapController.prototype.createLayergroup = function () {
|
||||
const { context, user } = res.locals;
|
||||
const datasource = context.datasource || Datasource.EmptyDatasource();
|
||||
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.analysesResults = context.analysesResults;
|
||||
@ -265,7 +269,7 @@ MapController.prototype.instantiateLayergroup = function () {
|
||||
}.bind(this);
|
||||
};
|
||||
|
||||
function sendResponse() {
|
||||
MapController.prototype.sendResponse = function () {
|
||||
return function sendResponseMiddleware (req, res) {
|
||||
const { layergroup } = res.locals;
|
||||
|
||||
@ -277,7 +281,7 @@ function sendResponse() {
|
||||
res.json(layergroup);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
MapController.prototype.incrementMapViewCount = function () {
|
||||
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;
|
||||
|
||||
return function augmentErrorMiddleware (err, req, res, next) {
|
||||
@ -576,7 +580,7 @@ function augmentError (options) {
|
||||
|
||||
next(err);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
function populateError(err, mapConfig) {
|
||||
var error = new Error(err.message);
|
||||
|
@ -28,7 +28,7 @@ describe('multilayer error cases', function() {
|
||||
}, {}, function(res) {
|
||||
assert.equal(res.statusCode, 400, 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();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user