Windshaft-cartodb/lib/api/middlewares/named-map-provider.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
module.exports = function getNamedMapProvider ({ namedMapProviderCache, label, forcedFormat = null }) {
return function getNamedMapProviderMiddleware (req, res, next) {
2019-11-14 18:36:47 +08:00
const { user, token, cache_buster: cacheBuster, api_key: apiKey } = res.locals;
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
2019-11-14 18:36:47 +08:00
const { template_id: templateId, layer: layerFromParams, z, x, y, format } = req.params;
const { layer: layerFromQuery } = req.query;
const params = {
2019-10-22 01:07:24 +08:00
user,
token,
2019-11-14 18:36:47 +08:00
cache_buster: cacheBuster,
api_key: apiKey,
2019-10-22 01:07:24 +08:00
dbuser,
dbname,
dbpassword,
dbhost,
dbport,
2019-11-14 18:36:47 +08:00
template_id: templateId,
2019-10-22 01:07:24 +08:00
layer: (layerFromQuery || layerFromParams),
z,
x,
y,
format
};
if (forcedFormat) {
params.format = forcedFormat;
params.layer = params.layer || 'all';
}
2019-11-14 18:36:47 +08:00
const { config, auth_token: authToken } = req.query;
2019-11-14 18:36:47 +08:00
namedMapProviderCache.get(user, templateId, config, authToken, params, (err, namedMapProvider) => {
if (err) {
err.label = label;
return next(err);
}
res.locals.mapConfigProvider = namedMapProvider;
next();
});
};
};