2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-10 00:02:13 +08:00
|
|
|
const windshaft = require('windshaft');
|
|
|
|
const MapConfig = windshaft.model.MapConfig;
|
|
|
|
const Datasource = windshaft.model.Datasource;
|
2018-04-07 00:20:33 +08:00
|
|
|
const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
|
|
|
|
const credentials = require('../middlewares/credentials');
|
|
|
|
const dbConnSetup = require('../middlewares/db-conn-setup');
|
|
|
|
const authorize = require('../middlewares/authorize');
|
2018-04-09 22:18:30 +08:00
|
|
|
const initProfiler = require('../middlewares/init-profiler');
|
|
|
|
const checkJsonContentType = require('../middlewares/check-json-content-type');
|
|
|
|
const incrementMapViewCount = require('../middlewares/increment-map-view-count');
|
|
|
|
const augmentLayergroupData = require('../middlewares/augment-layergroup-data');
|
2018-04-07 00:20:33 +08:00
|
|
|
const cacheControlHeader = require('../middlewares/cache-control-header');
|
|
|
|
const cacheChannelHeader = require('../middlewares/cache-channel-header');
|
|
|
|
const surrogateKeyHeader = require('../middlewares/surrogate-key-header');
|
|
|
|
const lastModifiedHeader = require('../middlewares/last-modified-header');
|
2018-04-09 22:18:30 +08:00
|
|
|
const lastUpdatedTimeLayergroup = require('../middlewares/last-updated-time-layergroup');
|
|
|
|
const layerStats = require('../middlewares/layer-stats');
|
|
|
|
const layergroupIdHeader = require('../middlewares/layergroup-id-header');
|
|
|
|
const layergroupMetadata = require('../middlewares/layergroup-metadata');
|
|
|
|
const mapError = require('../middlewares/map-error');
|
2018-03-28 18:39:39 +08:00
|
|
|
const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider');
|
2018-04-07 00:20:33 +08:00
|
|
|
const rateLimit = require('../middlewares/rate-limit');
|
2018-02-27 23:52:27 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
2020-04-29 16:40:45 +08:00
|
|
|
const metrics = require('../middlewares/metrics');
|
2017-05-10 23:17:01 +08:00
|
|
|
|
2018-05-11 18:14:27 +08:00
|
|
|
module.exports = class AnonymousMapController {
|
|
|
|
/**
|
|
|
|
* @param {AuthBackend} authBackend
|
|
|
|
* @param {PgConnection} pgConnection
|
|
|
|
* @param {TemplateMaps} templateMaps
|
|
|
|
* @param {MapBackend} mapBackend
|
|
|
|
* @param metadataBackend
|
|
|
|
* @param {SurrogateKeysCache} surrogateKeysCache
|
|
|
|
* @param {UserLimitsBackend} userLimitsBackend
|
|
|
|
* @param {LayergroupAffectedTables} layergroupAffectedTables
|
|
|
|
* @param {MapConfigAdapter} mapConfigAdapter
|
|
|
|
* @param {StatsBackend} statsBackend
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
constructor (
|
2020-04-29 01:17:00 +08:00
|
|
|
config,
|
2018-05-11 18:14:27 +08:00
|
|
|
pgConnection,
|
|
|
|
templateMaps,
|
|
|
|
mapBackend,
|
|
|
|
metadataBackend,
|
|
|
|
surrogateKeysCache,
|
|
|
|
userLimitsBackend,
|
|
|
|
layergroupAffectedTables,
|
|
|
|
mapConfigAdapter,
|
|
|
|
statsBackend,
|
|
|
|
authBackend,
|
2020-04-29 01:17:00 +08:00
|
|
|
layergroupMetadata,
|
|
|
|
metricsBackend
|
2018-05-11 18:14:27 +08:00
|
|
|
) {
|
2020-04-29 01:17:00 +08:00
|
|
|
this.config = config;
|
2018-05-11 18:14:27 +08:00
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.templateMaps = templateMaps;
|
|
|
|
this.mapBackend = mapBackend;
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
|
|
|
this.layergroupAffectedTables = layergroupAffectedTables;
|
|
|
|
this.mapConfigAdapter = mapConfigAdapter;
|
|
|
|
this.statsBackend = statsBackend;
|
|
|
|
this.authBackend = authBackend;
|
|
|
|
this.layergroupMetadata = layergroupMetadata;
|
2020-04-29 01:17:00 +08:00
|
|
|
this.metricsBackend = metricsBackend;
|
2018-05-11 18:14:27 +08:00
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (mapRouter) {
|
2018-05-11 18:14:27 +08:00
|
|
|
mapRouter.options('/');
|
2018-05-11 18:42:17 +08:00
|
|
|
mapRouter.get('/', this.middlewares());
|
|
|
|
mapRouter.post('/', this.middlewares());
|
2018-05-11 18:14:27 +08:00
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-05-11 18:42:17 +08:00
|
|
|
middlewares () {
|
2018-05-11 18:14:27 +08:00
|
|
|
const isTemplateInstantiation = false;
|
|
|
|
const useTemplateHash = false;
|
|
|
|
const includeQuery = true;
|
|
|
|
const label = 'ANONYMOUS LAYERGROUP';
|
|
|
|
const addContext = true;
|
2020-04-29 01:17:00 +08:00
|
|
|
const metricsTags = {
|
|
|
|
event: 'map_view',
|
|
|
|
attributes: { map_type: 'anonymous' },
|
|
|
|
from: {
|
|
|
|
req: {
|
|
|
|
query: { client: 'client' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-11-03 02:03:20 +08:00
|
|
|
|
2018-05-11 18:14:27 +08:00
|
|
|
return [
|
2020-04-29 01:17:00 +08:00
|
|
|
metrics({
|
|
|
|
enabled: this.config.pubSubMetrics.enabled,
|
|
|
|
metricsBackend: this.metricsBackend,
|
|
|
|
tags: metricsTags
|
|
|
|
}),
|
2018-05-11 18:14:27 +08:00
|
|
|
credentials(),
|
|
|
|
authorize(this.authBackend),
|
|
|
|
dbConnSetup(this.pgConnection),
|
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS),
|
|
|
|
cleanUpQueryParams(['aggregation']),
|
|
|
|
initProfiler(isTemplateInstantiation),
|
|
|
|
checkJsonContentType(),
|
|
|
|
checkCreateLayergroup(),
|
|
|
|
prepareAdapterMapConfig(this.mapConfigAdapter),
|
2019-10-22 01:07:24 +08:00
|
|
|
createLayergroup(
|
2018-05-11 18:14:27 +08:00
|
|
|
this.mapBackend,
|
|
|
|
this.userLimitsBackend,
|
|
|
|
this.pgConnection,
|
|
|
|
this.layergroupAffectedTables
|
|
|
|
),
|
|
|
|
incrementMapViewCount(this.metadataBackend),
|
|
|
|
augmentLayergroupData(),
|
|
|
|
cacheControlHeader({ ttl: global.environment.varnish.layergroupTtl || 86400, revalidate: true }),
|
|
|
|
cacheChannelHeader(),
|
|
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
2018-07-10 19:22:38 +08:00
|
|
|
lastModifiedHeader(),
|
2018-05-11 18:14:27 +08:00
|
|
|
lastUpdatedTimeLayergroup(),
|
|
|
|
layerStats(this.pgConnection, this.statsBackend),
|
|
|
|
layergroupIdHeader(this.templateMaps, useTemplateHash),
|
|
|
|
layergroupMetadata(this.layergroupMetadata, includeQuery),
|
|
|
|
mapError({ label, addContext })
|
|
|
|
];
|
|
|
|
}
|
2017-11-03 02:24:33 +08:00
|
|
|
};
|
|
|
|
|
2018-03-08 19:16:24 +08:00
|
|
|
function checkCreateLayergroup () {
|
2017-11-06 01:55:23 +08:00
|
|
|
return function checkCreateLayergroupMiddleware (req, res, next) {
|
2017-11-03 22:06:15 +08:00
|
|
|
if (req.method === 'GET') {
|
2018-03-23 23:08:52 +08:00
|
|
|
const { config } = req.query;
|
2017-11-03 22:06:15 +08:00
|
|
|
|
|
|
|
if (!config) {
|
|
|
|
return next(new Error('layergroup GET needs a "config" parameter'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.body = JSON.parse(config);
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-11-03 16:37:01 +08:00
|
|
|
}
|
2017-11-02 00:57:35 +08:00
|
|
|
|
2017-11-08 21:27:35 +08:00
|
|
|
req.profiler.done('checkCreateLayergroup');
|
2017-11-03 22:06:15 +08:00
|
|
|
return next();
|
|
|
|
};
|
2018-03-08 19:16:24 +08:00
|
|
|
}
|
2017-11-02 00:57:35 +08:00
|
|
|
|
2018-03-08 19:16:24 +08:00
|
|
|
function prepareAdapterMapConfig (mapConfigAdapter) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return function prepareAdapterMapConfigMiddleware (req, res, next) {
|
2017-11-03 22:06:15 +08:00
|
|
|
const requestMapConfig = req.body;
|
2018-03-23 02:14:18 +08:00
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
const { user, api_key: apiKey } = res.locals;
|
2018-03-23 02:14:18 +08:00
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
const params = Object.assign({ dbuser, dbname, dbpassword, dbhost, dbport }, req.query);
|
2017-11-03 22:06:15 +08:00
|
|
|
|
|
|
|
const context = {
|
|
|
|
analysisConfiguration: {
|
|
|
|
user,
|
|
|
|
db: {
|
|
|
|
host: dbhost,
|
|
|
|
port: dbport,
|
|
|
|
dbname: dbname,
|
|
|
|
user: dbuser,
|
|
|
|
pass: dbpassword
|
|
|
|
},
|
|
|
|
batch: {
|
|
|
|
username: user,
|
2019-11-14 18:36:47 +08:00
|
|
|
apiKey
|
2017-11-03 22:06:15 +08:00
|
|
|
}
|
2017-11-02 02:02:07 +08:00
|
|
|
}
|
2017-11-03 22:06:15 +08:00
|
|
|
};
|
2017-11-02 02:02:07 +08:00
|
|
|
|
2018-10-23 23:45:42 +08:00
|
|
|
mapConfigAdapter.getMapConfig(user,
|
2019-10-22 01:07:24 +08:00
|
|
|
requestMapConfig,
|
|
|
|
params,
|
|
|
|
context,
|
|
|
|
(err, requestMapConfig, stats = { overviewsAddedToMapconfig: false }) => {
|
|
|
|
req.profiler.done('anonymous.getMapConfig');
|
2018-09-04 22:20:38 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
stats.mapType = 'anonymous';
|
|
|
|
req.profiler.add(stats);
|
2018-10-23 23:45:42 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-11-02 02:02:07 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
req.body = requestMapConfig;
|
|
|
|
res.locals.context = context;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
next();
|
|
|
|
});
|
2018-03-08 19:16:24 +08:00
|
|
|
};
|
|
|
|
}
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
function createLayergroup (mapBackend, userLimitsBackend, pgConnection, affectedTablesCache) {
|
2017-11-03 22:06:15 +08:00
|
|
|
return function createLayergroupMiddleware (req, res, next) {
|
|
|
|
const requestMapConfig = req.body;
|
2018-03-23 02:36:42 +08:00
|
|
|
|
|
|
|
const { context } = res.locals;
|
2019-11-14 18:36:47 +08:00
|
|
|
const { user, cache_buster: cacheBuster, api_key: apiKey } = res.locals;
|
2018-03-23 02:36:42 +08:00
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
|
|
|
|
const params = {
|
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
|
2018-03-23 02:36:42 +08:00
|
|
|
};
|
|
|
|
|
2017-11-03 22:06:15 +08:00
|
|
|
const datasource = context.datasource || Datasource.EmptyDatasource();
|
2018-03-21 21:11:54 +08:00
|
|
|
const mapConfig = new MapConfig(requestMapConfig, datasource);
|
2018-03-23 02:36:42 +08:00
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
const mapConfigProvider = new CreateLayergroupMapConfigProvider(
|
|
|
|
mapConfig,
|
|
|
|
user,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend,
|
2018-03-21 21:11:54 +08:00
|
|
|
pgConnection,
|
|
|
|
affectedTablesCache,
|
2018-03-23 02:36:42 +08:00
|
|
|
params
|
2018-03-21 21:11:54 +08:00
|
|
|
);
|
2016-05-23 22:20:42 +08:00
|
|
|
|
2018-03-23 17:57:35 +08:00
|
|
|
res.locals.mapConfig = mapConfig;
|
2020-04-29 20:32:08 +08:00
|
|
|
res.locals.mapConfigProvider = mapConfigProvider;
|
2018-03-23 17:57:35 +08:00
|
|
|
res.locals.analysesResults = context.analysesResults;
|
|
|
|
|
2018-03-23 02:37:08 +08:00
|
|
|
const mapParams = { dbuser, dbname, dbpassword, dbhost, dbport };
|
|
|
|
|
2018-07-16 22:52:26 +08:00
|
|
|
mapBackend.createLayergroup(mapConfig, mapParams, mapConfigProvider, (err, layergroup, stats = {}) => {
|
|
|
|
req.profiler.add(stats);
|
|
|
|
|
2017-11-03 22:06:15 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-11-02 02:57:20 +08:00
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-20 02:16:18 +08:00
|
|
|
res.body = layergroup;
|
2017-11-01 03:10:37 +08:00
|
|
|
|
2017-11-03 22:06:15 +08:00
|
|
|
next();
|
|
|
|
});
|
2018-03-08 19:16:24 +08:00
|
|
|
};
|
|
|
|
}
|