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;
|
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 (
|
|
|
|
pgConnection,
|
|
|
|
templateMaps,
|
|
|
|
mapBackend,
|
|
|
|
metadataBackend,
|
|
|
|
surrogateKeysCache,
|
|
|
|
userLimitsBackend,
|
|
|
|
layergroupAffectedTables,
|
|
|
|
mapConfigAdapter,
|
|
|
|
statsBackend,
|
|
|
|
authBackend,
|
|
|
|
layergroupMetadata
|
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-05-11 18:14:27 +08:00
|
|
|
register (mapRouter) {
|
|
|
|
mapRouter.options('/');
|
|
|
|
mapRouter.get('/', this.composeCreateMapMiddleware());
|
|
|
|
mapRouter.post('/', this.composeCreateMapMiddleware());
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-05-11 18:14:27 +08:00
|
|
|
composeCreateMapMiddleware () {
|
|
|
|
const isTemplateInstantiation = false;
|
|
|
|
const useTemplateHash = false;
|
|
|
|
const includeQuery = true;
|
|
|
|
const label = 'ANONYMOUS LAYERGROUP';
|
|
|
|
const addContext = true;
|
2017-11-03 02:03:20 +08:00
|
|
|
|
2018-05-11 18:14:27 +08:00
|
|
|
return [
|
|
|
|
credentials(),
|
|
|
|
authorize(this.authBackend),
|
|
|
|
dbConnSetup(this.pgConnection),
|
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS),
|
|
|
|
cleanUpQueryParams(['aggregation']),
|
|
|
|
initProfiler(isTemplateInstantiation),
|
|
|
|
checkJsonContentType(),
|
|
|
|
checkCreateLayergroup(),
|
|
|
|
prepareAdapterMapConfig(this.mapConfigAdapter),
|
|
|
|
createLayergroup (
|
|
|
|
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 }),
|
|
|
|
lastModifiedHeader({ now: true }),
|
|
|
|
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) {
|
2017-11-03 22:06:15 +08:00
|
|
|
return function prepareAdapterMapConfigMiddleware(req, res, next) {
|
|
|
|
const requestMapConfig = req.body;
|
2018-03-23 02:14:18 +08:00
|
|
|
|
|
|
|
const { user, api_key } = res.locals;
|
|
|
|
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,
|
|
|
|
apiKey: api_key
|
|
|
|
}
|
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-03-23 02:14:18 +08:00
|
|
|
mapConfigAdapter.getMapConfig(user, requestMapConfig, params, context, (err, requestMapConfig) => {
|
2017-11-08 21:08:27 +08:00
|
|
|
req.profiler.done('anonymous.getMapConfig');
|
2017-11-03 22:06:15 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-11-02 02:02:07 +08:00
|
|
|
|
2017-11-03 22:06:15 +08:00
|
|
|
req.body = requestMapConfig;
|
|
|
|
res.locals.context = context;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-03 22:06:15 +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;
|
|
|
|
const { user, cache_buster, api_key } = res.locals;
|
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
cache_buster, api_key,
|
|
|
|
dbuser, dbname, dbpassword, dbhost, dbport
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
res.locals.analysesResults = context.analysesResults;
|
|
|
|
|
2018-03-23 02:37:08 +08:00
|
|
|
const mapParams = { dbuser, dbname, dbpassword, dbhost, dbport };
|
|
|
|
|
|
|
|
mapBackend.createLayergroup(mapConfig, mapParams, mapConfigProvider, (err, layergroup) => {
|
2017-11-08 17:52:25 +08:00
|
|
|
req.profiler.done('createLayergroup');
|
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;
|
2018-03-21 21:11:54 +08:00
|
|
|
res.locals.mapConfigProvider = mapConfigProvider;
|
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
|
|
|
};
|
|
|
|
}
|