2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
const layergroupToken = require('../middlewares/layergroup-token');
|
|
|
|
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');
|
|
|
|
const rateLimit = require('../middlewares/rate-limit');
|
2018-03-24 04:20:37 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
2018-04-09 22:18:30 +08:00
|
|
|
const createMapStoreMapConfigProvider = require('../middlewares/map-store-map-config-provider');
|
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-03-24 04:20:37 +08:00
|
|
|
|
|
|
|
const ALLOWED_DATAVIEW_QUERY_PARAMS = [
|
|
|
|
'filters', // json
|
|
|
|
'own_filter', // 0, 1
|
|
|
|
'no_filters', // 0, 1
|
|
|
|
'bbox', // w,s,e,n
|
2019-12-03 01:36:41 +08:00
|
|
|
'circle', // json
|
2019-12-03 17:58:55 +08:00
|
|
|
'polygon', // json
|
2018-03-24 04:20:37 +08:00
|
|
|
'start', // number
|
|
|
|
'end', // number
|
|
|
|
'column_type', // string
|
|
|
|
'bins', // number
|
2019-10-22 01:07:24 +08:00
|
|
|
'aggregation', // string
|
2018-03-24 04:20:37 +08:00
|
|
|
'offset', // number
|
|
|
|
'q', // widgets search
|
2019-10-22 01:07:24 +08:00
|
|
|
'categories' // number
|
2018-03-24 04:20:37 +08:00
|
|
|
];
|
|
|
|
|
2018-03-29 01:37:31 +08:00
|
|
|
module.exports = class DataviewLayergroupController {
|
2018-03-24 04:20:37 +08:00
|
|
|
constructor (
|
|
|
|
dataviewBackend,
|
|
|
|
pgConnection,
|
|
|
|
mapStore,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
layergroupAffectedTablesCache,
|
2018-04-10 00:08:56 +08:00
|
|
|
authBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
surrogateKeysCache
|
|
|
|
) {
|
|
|
|
this.dataviewBackend = dataviewBackend;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.mapStore = mapStore;
|
2018-04-10 16:16:07 +08:00
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
2018-03-24 04:20:37 +08:00
|
|
|
this.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
|
2018-04-10 00:08:56 +08:00
|
|
|
this.authBackend = authBackend;
|
2018-03-24 04:20:37 +08:00
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (mapRouter) {
|
2018-03-24 04:20:37 +08:00
|
|
|
// Undocumented/non-supported API endpoint methods.
|
|
|
|
// Use at your own peril.
|
|
|
|
|
2018-05-11 19:29:50 +08:00
|
|
|
mapRouter.get('/:token/dataview/:dataviewName', this.middlewares({
|
|
|
|
action: 'get',
|
|
|
|
rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW
|
|
|
|
}));
|
|
|
|
|
|
|
|
mapRouter.get('/:token/:layer/widget/:dataviewName', this.middlewares({
|
|
|
|
action: 'get',
|
|
|
|
rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW
|
|
|
|
}));
|
|
|
|
|
|
|
|
mapRouter.get('/:token/dataview/:dataviewName/search', this.middlewares({
|
|
|
|
action: 'search',
|
|
|
|
rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH
|
|
|
|
}));
|
|
|
|
|
|
|
|
mapRouter.get('/:token/:layer/widget/:dataviewName/search', this.middlewares({
|
|
|
|
action: 'search',
|
|
|
|
rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH
|
|
|
|
}));
|
2018-05-11 19:20:05 +08:00
|
|
|
}
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2018-05-11 19:20:05 +08:00
|
|
|
middlewares ({ action, rateLimitGroup }) {
|
|
|
|
return [
|
2018-03-24 04:20:37 +08:00
|
|
|
layergroupToken(),
|
|
|
|
credentials(),
|
2018-04-10 00:08:56 +08:00
|
|
|
authorize(this.authBackend),
|
2018-03-24 04:20:37 +08:00
|
|
|
dbConnSetup(this.pgConnection),
|
2018-05-11 19:20:05 +08:00
|
|
|
rateLimit(this.userLimitsBackend, rateLimitGroup),
|
2018-03-24 04:20:37 +08:00
|
|
|
cleanUpQueryParams(ALLOWED_DATAVIEW_QUERY_PARAMS),
|
|
|
|
createMapStoreMapConfigProvider(
|
|
|
|
this.mapStore,
|
2018-04-10 16:16:07 +08:00
|
|
|
this.userLimitsBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
this.pgConnection,
|
|
|
|
this.layergroupAffectedTablesCache
|
|
|
|
),
|
2018-05-11 19:20:05 +08:00
|
|
|
action === 'search' ? dataviewSearch(this.dataviewBackend) : getDataview(this.dataviewBackend),
|
2018-03-24 04:20:37 +08:00
|
|
|
cacheControlHeader(),
|
|
|
|
cacheChannelHeader(),
|
|
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
2018-04-05 01:15:51 +08:00
|
|
|
lastModifiedHeader()
|
2018-05-11 19:20:05 +08:00
|
|
|
];
|
2018-03-24 04:20:37 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function getDataview (dataviewBackend) {
|
|
|
|
return function getDataviewMiddleware (req, res, next) {
|
|
|
|
const { user, mapConfigProvider } = res.locals;
|
|
|
|
const { dataviewName } = req.params;
|
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
|
|
|
|
const params = Object.assign({ dataviewName, dbuser, dbname, dbpassword, dbhost, dbport }, req.query);
|
|
|
|
|
|
|
|
dataviewBackend.getDataview(mapConfigProvider, user, params, (err, dataview, stats = {}) => {
|
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = 'GET DATAVIEW';
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-24 04:20:37 +08:00
|
|
|
res.body = dataview;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataviewSearch (dataviewBackend) {
|
|
|
|
return function dataviewSearchMiddleware (req, res, next) {
|
|
|
|
const { user, mapConfigProvider } = res.locals;
|
|
|
|
const { dataviewName } = req.params;
|
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
|
|
|
|
const params = Object.assign({ dbuser, dbname, dbpassword, dbhost, dbport }, req.query);
|
|
|
|
|
|
|
|
dataviewBackend.search(mapConfigProvider, user, dataviewName, params, (err, searchResult, stats = {}) => {
|
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = 'GET DATAVIEW SEARCH';
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-24 04:20:37 +08:00
|
|
|
res.body = searchResult;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|