2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-06 01:42:20 +08:00
|
|
|
const { Router: router } = require('express');
|
|
|
|
|
2018-04-03 01:02:31 +08:00
|
|
|
const RedisPool = require('redis-mpool');
|
|
|
|
const cartodbRedis = require('cartodb-redis');
|
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
const { factory: windshaftFactory } = require('windshaft');
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2019-10-07 15:43:40 +08:00
|
|
|
const PgConnection = require('../backends/pg-connection');
|
2018-04-03 01:02:31 +08:00
|
|
|
const AnalysisBackend = require('../backends/analysis');
|
|
|
|
const AnalysisStatusBackend = require('../backends/analysis-status');
|
|
|
|
const DataviewBackend = require('../backends/dataview');
|
2019-10-07 16:50:14 +08:00
|
|
|
const TemplateMaps = require('../backends/template-maps');
|
2019-10-07 15:45:46 +08:00
|
|
|
const PgQueryRunner = require('../backends/pg-query-runner');
|
2018-04-03 01:02:31 +08:00
|
|
|
const StatsBackend = require('../backends/stats');
|
2018-04-10 00:08:56 +08:00
|
|
|
const AuthBackend = require('../backends/auth');
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
const UserLimitsBackend = require('../backends/user-limits');
|
2018-04-10 01:47:24 +08:00
|
|
|
const OverviewsMetadataBackend = require('../backends/overviews-metadata');
|
2018-04-10 00:56:01 +08:00
|
|
|
const FilterStatsApi = require('../backends/filter-stats');
|
2018-04-10 15:40:09 +08:00
|
|
|
const TablesExtentBackend = require('../backends/tables-extent');
|
2019-02-27 02:19:44 +08:00
|
|
|
const ClusterBackend = require('../backends/cluster');
|
2020-04-29 16:40:45 +08:00
|
|
|
const PubSubMetricsBackend = require('../backends/metrics');
|
2020-02-20 22:25:53 +08:00
|
|
|
|
2019-10-07 15:53:06 +08:00
|
|
|
const LayergroupAffectedTablesCache = require('../cache/layergroup-affected-tables');
|
2019-10-07 15:55:55 +08:00
|
|
|
const SurrogateKeysCache = require('../cache/surrogate-keys-cache');
|
2019-10-07 15:47:42 +08:00
|
|
|
const VarnishHttpCacheBackend = require('../cache/backend/varnish-http');
|
2018-04-03 01:02:31 +08:00
|
|
|
const FastlyCacheBackend = require('../cache/backend/fastly');
|
2019-10-07 15:54:54 +08:00
|
|
|
const NamedMapProviderCache = require('../cache/named-map-provider-cache');
|
2019-10-07 15:51:51 +08:00
|
|
|
const NamedMapsCacheEntry = require('../cache/model/named-maps-entry');
|
2020-04-04 23:46:08 +08:00
|
|
|
const NamedMapProviderCacheReporter = require('../stats/reporter/named-map-provider-cache');
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const SqlWrapMapConfigAdapter = require('../models/mapconfig/adapter/sql-wrap-mapconfig-adapter');
|
|
|
|
const MapConfigNamedLayersAdapter = require('../models/mapconfig/adapter/mapconfig-named-layers-adapter');
|
|
|
|
const MapConfigBufferSizeAdapter = require('../models/mapconfig/adapter/mapconfig-buffer-size-adapter');
|
|
|
|
const AnalysisMapConfigAdapter = require('../models/mapconfig/adapter/analysis-mapconfig-adapter');
|
|
|
|
const MapConfigOverviewsAdapter = require('../models/mapconfig/adapter/mapconfig-overviews-adapter');
|
|
|
|
const TurboCartoAdapter = require('../models/mapconfig/adapter/turbo-carto-adapter');
|
|
|
|
const DataviewsWidgetsAdapter = require('../models/mapconfig/adapter/dataviews-widgets-adapter');
|
|
|
|
const AggregationMapConfigAdapter = require('../models/mapconfig/adapter/aggregation-mapconfig-adapter');
|
|
|
|
const MapConfigAdapter = require('../models/mapconfig/adapter');
|
2018-05-31 18:41:34 +08:00
|
|
|
const VectorMapConfigAdapter = require('../models/mapconfig/adapter/vector-mapconfig-adapter');
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const ResourceLocator = require('../models/resource-locator');
|
|
|
|
const LayergroupMetadata = require('../utils/layergroup-metadata');
|
|
|
|
const RendererStatsReporter = require('../stats/reporter/renderer');
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
const initializeStatusCode = require('./middlewares/initialize-status-code');
|
2020-06-02 17:49:54 +08:00
|
|
|
const initLogger = require('./middlewares/logger');
|
2018-04-06 01:42:20 +08:00
|
|
|
const bodyParser = require('body-parser');
|
2018-04-07 00:20:33 +08:00
|
|
|
const servedByHostHeader = require('./middlewares/served-by-host-header');
|
2020-06-02 23:09:06 +08:00
|
|
|
const profiler = require('./middlewares/profiler');
|
2018-04-07 00:20:33 +08:00
|
|
|
const lzmaMiddleware = require('./middlewares/lzma');
|
|
|
|
const cors = require('./middlewares/cors');
|
|
|
|
const user = require('./middlewares/user');
|
|
|
|
const sendResponse = require('./middlewares/send-response');
|
|
|
|
const syntaxError = require('./middlewares/syntax-error');
|
|
|
|
const errorMiddleware = require('./middlewares/error-middleware');
|
2020-05-26 19:15:35 +08:00
|
|
|
const clientHeader = require('./middlewares/client-header');
|
2018-04-06 01:42:20 +08:00
|
|
|
|
2018-04-09 22:18:30 +08:00
|
|
|
const MapRouter = require('./map/map-router');
|
|
|
|
const TemplateRouter = require('./template/template-router');
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
const getOnTileErrorStrategy = require('../utils/on-tile-error-strategy');
|
|
|
|
|
2018-04-06 01:42:20 +08:00
|
|
|
module.exports = class ApiRouter {
|
2018-04-03 01:02:31 +08:00
|
|
|
constructor ({ serverOptions, environmentOptions }) {
|
2018-04-04 21:52:54 +08:00
|
|
|
this.serverOptions = serverOptions;
|
|
|
|
|
2018-05-08 00:24:41 +08:00
|
|
|
const redisOptions = Object.assign({
|
2018-04-03 01:02:31 +08:00
|
|
|
name: 'windshaft-server',
|
|
|
|
unwatchOnRelease: false,
|
|
|
|
noReadyCheck: true
|
2018-05-08 00:24:41 +08:00
|
|
|
}, environmentOptions.redis);
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const redisPool = new RedisPool(redisOptions);
|
|
|
|
|
2018-04-04 21:52:54 +08:00
|
|
|
redisPool.on('status', function (status) {
|
2018-04-03 01:02:31 +08:00
|
|
|
var keyPrefix = 'windshaft.redis-pool.' + status.name + '.db' + status.db + '.';
|
|
|
|
global.statsClient.gauge(keyPrefix + 'count', status.count);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'unused', status.unused);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'waiting', status.waiting);
|
|
|
|
});
|
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
const { rendererCache, tileBackend, attributesBackend, previewBackend, mapBackend, mapStore } = windshaftFactory({
|
|
|
|
rendererOptions: serverOptions,
|
|
|
|
redisPool,
|
2020-06-02 19:20:57 +08:00
|
|
|
onTileErrorStrategy: getOnTileErrorStrategy({ enabled: environmentOptions.enabledFeatures.onTileErrorStrategy }),
|
|
|
|
logger: this.serverOptions.logger
|
2018-04-03 01:02:31 +08:00
|
|
|
});
|
|
|
|
|
2020-04-05 00:51:22 +08:00
|
|
|
const rendererStatsReporter = new RendererStatsReporter(rendererCache, serverOptions.renderCache.statsInterval);
|
2018-04-03 01:02:31 +08:00
|
|
|
rendererStatsReporter.start();
|
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
const metadataBackend = cartodbRedis({ pool: redisPool });
|
|
|
|
const pgConnection = new PgConnection(metadataBackend);
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const surrogateKeysCacheBackends = createSurrogateKeysCacheBackends(serverOptions);
|
|
|
|
const surrogateKeysCache = new SurrogateKeysCache(surrogateKeysCacheBackends);
|
2020-06-02 17:49:54 +08:00
|
|
|
const templateMaps = createTemplateMaps({ redisPool, surrogateKeysCache, logger: this.serverOptions.logger });
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const analysisStatusBackend = new AnalysisStatusBackend();
|
|
|
|
const analysisBackend = new AnalysisBackend(metadataBackend, serverOptions.analysis);
|
|
|
|
const dataviewBackend = new DataviewBackend(analysisBackend);
|
|
|
|
const statsBackend = new StatsBackend();
|
2019-02-27 02:19:44 +08:00
|
|
|
const clusterBackend = new ClusterBackend();
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
const userLimitsBackend = new UserLimitsBackend(metadataBackend, {
|
2018-04-03 01:02:31 +08:00
|
|
|
limits: {
|
|
|
|
cacheOnTimeout: serverOptions.renderer.mapnik.limits.cacheOnTimeout || false,
|
|
|
|
render: serverOptions.renderer.mapnik.limits.render || 0,
|
|
|
|
rateLimitsEnabled: global.environment.enabledFeatures.rateLimitsEnabled
|
|
|
|
}
|
|
|
|
});
|
2018-04-10 00:08:56 +08:00
|
|
|
const authBackend = new AuthBackend(pgConnection, metadataBackend, mapStore, templateMaps);
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const layergroupAffectedTablesCache = new LayergroupAffectedTablesCache();
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
this.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pgQueryRunner = new PgQueryRunner(pgConnection);
|
2018-04-10 01:47:24 +08:00
|
|
|
const overviewsMetadataBackend = new OverviewsMetadataBackend(pgQueryRunner);
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2018-04-10 00:56:01 +08:00
|
|
|
const filterStatsBackend = new FilterStatsApi(pgQueryRunner);
|
2018-04-10 15:40:09 +08:00
|
|
|
const tablesExtentBackend = new TablesExtentBackend(pgQueryRunner);
|
2018-04-03 01:02:31 +08:00
|
|
|
|
|
|
|
const mapConfigAdapter = new MapConfigAdapter(
|
|
|
|
new MapConfigNamedLayersAdapter(templateMaps, pgConnection),
|
|
|
|
new MapConfigBufferSizeAdapter(),
|
|
|
|
new SqlWrapMapConfigAdapter(),
|
|
|
|
new DataviewsWidgetsAdapter(),
|
|
|
|
new AnalysisMapConfigAdapter(analysisBackend),
|
2018-05-31 18:41:34 +08:00
|
|
|
new VectorMapConfigAdapter(pgConnection),
|
2018-04-03 01:02:31 +08:00
|
|
|
new AggregationMapConfigAdapter(pgConnection),
|
2018-04-10 01:47:24 +08:00
|
|
|
new MapConfigOverviewsAdapter(overviewsMetadataBackend, filterStatsBackend),
|
2018-04-03 01:02:31 +08:00
|
|
|
new TurboCartoAdapter()
|
|
|
|
);
|
|
|
|
|
|
|
|
const resourceLocator = new ResourceLocator(global.environment);
|
|
|
|
const layergroupMetadata = new LayergroupMetadata(resourceLocator);
|
|
|
|
|
|
|
|
const namedMapProviderCache = new NamedMapProviderCache(
|
|
|
|
templateMaps,
|
|
|
|
pgConnection,
|
|
|
|
metadataBackend,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend,
|
2018-04-03 01:02:31 +08:00
|
|
|
mapConfigAdapter,
|
|
|
|
layergroupAffectedTablesCache
|
|
|
|
);
|
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
const namedMapProviderCacheReporter = new NamedMapProviderCacheReporter({
|
2019-09-14 02:01:03 +08:00
|
|
|
namedMapProviderCache,
|
2020-04-05 00:51:22 +08:00
|
|
|
intervalInMilliseconds: serverOptions.renderCache.statsInterval
|
2019-09-14 02:01:03 +08:00
|
|
|
});
|
2020-04-04 23:46:08 +08:00
|
|
|
namedMapProviderCacheReporter.start();
|
2019-09-14 02:01:03 +08:00
|
|
|
|
2020-04-29 01:17:00 +08:00
|
|
|
const metricsBackend = new PubSubMetricsBackend(serverOptions.pubSubMetrics);
|
|
|
|
|
2018-04-04 21:52:54 +08:00
|
|
|
const collaborators = {
|
2020-04-29 01:17:00 +08:00
|
|
|
config: serverOptions,
|
2018-04-03 01:02:31 +08:00
|
|
|
analysisStatusBackend,
|
|
|
|
attributesBackend,
|
|
|
|
dataviewBackend,
|
|
|
|
previewBackend,
|
|
|
|
tileBackend,
|
|
|
|
pgConnection,
|
|
|
|
mapStore,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend,
|
2018-04-03 01:02:31 +08:00
|
|
|
layergroupAffectedTablesCache,
|
2018-04-10 00:08:56 +08:00
|
|
|
authBackend,
|
2018-04-03 01:02:31 +08:00
|
|
|
surrogateKeysCache,
|
|
|
|
templateMaps,
|
|
|
|
mapBackend,
|
|
|
|
metadataBackend,
|
|
|
|
mapConfigAdapter,
|
|
|
|
statsBackend,
|
2018-04-04 21:52:54 +08:00
|
|
|
layergroupMetadata,
|
2018-04-03 01:02:31 +08:00
|
|
|
namedMapProviderCache,
|
2019-02-27 02:19:44 +08:00
|
|
|
tablesExtentBackend,
|
2020-04-29 01:17:00 +08:00
|
|
|
clusterBackend,
|
|
|
|
metricsBackend
|
2018-04-04 21:52:54 +08:00
|
|
|
};
|
2018-04-03 01:02:31 +08:00
|
|
|
|
2020-04-29 01:17:00 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2018-05-09 20:59:21 +08:00
|
|
|
this.mapRouter = new MapRouter({ collaborators });
|
|
|
|
this.templateRouter = new TemplateRouter({ collaborators });
|
2018-04-03 01:02:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (app, routes) {
|
2018-04-04 01:08:56 +08:00
|
|
|
// FIXME: we need a better way to reset cache while running tests
|
2018-04-03 01:02:31 +08:00
|
|
|
if (process.env.NODE_ENV === 'test') {
|
2018-04-04 21:52:54 +08:00
|
|
|
app.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache;
|
2018-04-03 01:02:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:07:58 +08:00
|
|
|
routes.forEach(route => {
|
2018-05-14 17:50:48 +08:00
|
|
|
const apiRouter = router({ mergeParams: true });
|
2019-10-04 18:22:23 +08:00
|
|
|
const { paths, middlewares = [] } = route;
|
2019-10-02 16:42:29 +08:00
|
|
|
|
2020-06-09 02:16:00 +08:00
|
|
|
apiRouter.use(initLogger({ logger: this.serverOptions.logger }));
|
2020-12-16 23:32:32 +08:00
|
|
|
apiRouter.use(user(this.metadataBackend));
|
2020-06-09 02:16:00 +08:00
|
|
|
apiRouter.use(profiler({
|
|
|
|
enabled: this.serverOptions.useProfiler,
|
|
|
|
statsClient: global.statsClient
|
|
|
|
}));
|
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
middlewares.forEach(middleware => apiRouter.use(middleware()));
|
2018-04-06 01:42:20 +08:00
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
apiRouter.use(initializeStatusCode());
|
2018-05-09 20:59:21 +08:00
|
|
|
apiRouter.use(bodyParser.json());
|
|
|
|
apiRouter.use(servedByHostHeader());
|
2020-05-26 19:15:35 +08:00
|
|
|
apiRouter.use(clientHeader());
|
2018-05-09 20:59:21 +08:00
|
|
|
apiRouter.use(lzmaMiddleware());
|
|
|
|
apiRouter.use(cors());
|
2018-04-06 01:42:20 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
this.templateRouter.route(apiRouter, route.template);
|
|
|
|
this.mapRouter.route(apiRouter, route.map);
|
2018-04-06 01:42:20 +08:00
|
|
|
|
2018-05-09 20:59:21 +08:00
|
|
|
apiRouter.use(sendResponse());
|
|
|
|
apiRouter.use(syntaxError());
|
|
|
|
apiRouter.use(errorMiddleware());
|
2018-04-06 01:42:20 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
paths.forEach(path => app.use(path, apiRouter));
|
2018-05-09 20:59:21 +08:00
|
|
|
});
|
2018-04-03 01:02:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-02 17:49:54 +08:00
|
|
|
function createTemplateMaps ({ redisPool, surrogateKeysCache, logger }) {
|
2018-04-03 01:02:31 +08:00
|
|
|
const templateMaps = new TemplateMaps(redisPool, {
|
|
|
|
max_user_templates: global.environment.maxUserTemplates
|
|
|
|
});
|
|
|
|
|
2020-06-02 01:18:15 +08:00
|
|
|
function invalidateNamedMap (user, templateName) {
|
|
|
|
const startTime = Date.now();
|
|
|
|
surrogateKeysCache.invalidate(new NamedMapsCacheEntry(user, templateName), (err) => {
|
2018-04-03 01:02:31 +08:00
|
|
|
if (err) {
|
2020-07-21 23:53:46 +08:00
|
|
|
return logger.error({ exception: err, 'cdb-user': user, template_id: templateName }, 'Named map invalidation failed');
|
2018-04-03 01:02:31 +08:00
|
|
|
}
|
2020-06-02 01:18:15 +08:00
|
|
|
|
2020-07-23 20:01:34 +08:00
|
|
|
const elapsed = Date.now() - startTime;
|
|
|
|
logger.info({ 'cdb-user': user, template_id: templateName, duration: elapsed / 1000, duration_ms: elapsed }, 'Named map invalidation success');
|
2018-04-03 01:02:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
['update', 'delete'].forEach(function (eventType) {
|
2018-04-03 01:02:31 +08:00
|
|
|
templateMaps.on(eventType, invalidateNamedMap);
|
|
|
|
});
|
|
|
|
|
|
|
|
return templateMaps;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createSurrogateKeysCacheBackends (serverOptions) {
|
2018-04-03 01:02:31 +08:00
|
|
|
var cacheBackends = [];
|
|
|
|
|
|
|
|
if (serverOptions.varnish_purge_enabled) {
|
|
|
|
cacheBackends.push(
|
|
|
|
new VarnishHttpCacheBackend(serverOptions.varnish_host, serverOptions.varnish_http_port)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverOptions.fastly &&
|
|
|
|
!!serverOptions.fastly.enabled && !!serverOptions.fastly.apiKey && !!serverOptions.fastly.serviceId) {
|
|
|
|
cacheBackends.push(
|
|
|
|
new FastlyCacheBackend(serverOptions.fastly.apiKey, serverOptions.fastly.serviceId)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cacheBackends;
|
|
|
|
}
|