2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-08 22:29:22 +08:00
|
|
|
const tag = require('../middlewares/tag');
|
2018-04-17 22:07:47 +08:00
|
|
|
const coordinates = require('../middlewares/coordinates');
|
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 namedMapProvider = require('../middlewares/named-map-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');
|
|
|
|
const vectorError = require('../middlewares/vector-error');
|
|
|
|
const rateLimit = require('../middlewares/rate-limit');
|
2018-03-29 01:11:19 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
|
|
|
|
2018-05-11 22:24:28 +08:00
|
|
|
module.exports = class TileTemplateController {
|
|
|
|
constructor (
|
|
|
|
namedMapProviderCache,
|
|
|
|
tileBackend,
|
|
|
|
surrogateKeysCache,
|
|
|
|
pgConnection,
|
|
|
|
authBackend,
|
|
|
|
userLimitsBackend
|
|
|
|
) {
|
|
|
|
this.namedMapProviderCache = namedMapProviderCache;
|
|
|
|
this.tileBackend = tileBackend;
|
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.authBackend = authBackend;
|
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
|
|
|
}
|
2018-03-29 01:11:19 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (templateRouter) {
|
2018-05-11 22:26:05 +08:00
|
|
|
templateRouter.get('/:template_id/:layer/:z/:x/:y.(:format)', this.middlewares());
|
|
|
|
}
|
|
|
|
|
|
|
|
middlewares () {
|
|
|
|
return [
|
2020-06-08 22:29:22 +08:00
|
|
|
tag({ tags: ['tile', 'named'] }),
|
2018-05-11 22:24:28 +08:00
|
|
|
coordinates(),
|
|
|
|
credentials(),
|
|
|
|
authorize(this.authBackend),
|
|
|
|
dbConnSetup(this.pgConnection),
|
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_TILES),
|
|
|
|
cleanUpQueryParams(),
|
|
|
|
namedMapProvider({
|
|
|
|
namedMapProviderCache: this.namedMapProviderCache,
|
|
|
|
label: 'NAMED_MAP_TILE'
|
|
|
|
}),
|
|
|
|
getTile({
|
|
|
|
tileBackend: this.tileBackend,
|
|
|
|
label: 'NAMED_MAP_TILE'
|
|
|
|
}),
|
|
|
|
setContentTypeHeader(),
|
|
|
|
cacheControlHeader(),
|
|
|
|
cacheChannelHeader(),
|
|
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
|
|
|
lastModifiedHeader(),
|
|
|
|
vectorError()
|
2018-05-11 22:26:05 +08:00
|
|
|
];
|
2018-05-11 22:24:28 +08:00
|
|
|
}
|
2018-03-29 01:11:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function getTile ({ tileBackend, label }) {
|
|
|
|
return function getTileMiddleware (req, res, next) {
|
|
|
|
const { mapConfigProvider } = res.locals;
|
|
|
|
const { layer, z, x, y, format } = req.params;
|
|
|
|
const params = { layer, z, x, y, format };
|
|
|
|
|
2020-06-04 01:51:56 +08:00
|
|
|
tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats = {}) => {
|
2018-03-29 01:11:19 +08:00
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = label;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
res.set(headers);
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-29 01:11:19 +08:00
|
|
|
res.body = tile;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setContentTypeHeader () {
|
2019-10-22 01:07:24 +08:00
|
|
|
return function setContentTypeHeaderMiddleware (req, res, next) {
|
2018-03-29 01:11:19 +08:00
|
|
|
res.set('Content-Type', res.get('content-type') || res.get('Content-Type') || 'image/png');
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|