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;
|
|
|
|
|
|
|
|
function TileTemplateController (
|
|
|
|
namedMapProviderCache,
|
|
|
|
tileBackend,
|
|
|
|
surrogateKeysCache,
|
|
|
|
pgConnection,
|
2018-04-10 00:08:56 +08:00
|
|
|
authBackend,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend
|
2018-03-29 01:11:19 +08:00
|
|
|
) {
|
|
|
|
this.namedMapProviderCache = namedMapProviderCache;
|
|
|
|
this.tileBackend = tileBackend;
|
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
this.pgConnection = pgConnection;
|
2018-04-10 00:08:56 +08:00
|
|
|
this.authBackend = authBackend;
|
2018-04-10 16:16:07 +08:00
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
2018-03-29 01:11:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TileTemplateController;
|
|
|
|
|
|
|
|
TileTemplateController.prototype.register = function (templateRouter) {
|
|
|
|
templateRouter.get(
|
|
|
|
`/:template_id/:layer/:z/:x/:y.(:format)`,
|
2018-04-17 21:55:38 +08:00
|
|
|
coordinates(),
|
2018-03-29 01:11:19 +08:00
|
|
|
credentials(),
|
2018-04-10 00:08:56 +08:00
|
|
|
authorize(this.authBackend),
|
2018-03-29 01:11:19 +08:00
|
|
|
dbConnSetup(this.pgConnection),
|
2018-04-10 16:16:07 +08:00
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_TILES),
|
2018-03-29 01:11:19 +08:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats) => {
|
|
|
|
req.profiler.add(stats);
|
|
|
|
req.profiler.done('render-' + format);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = label;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
res.set(headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.body = tile;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setContentTypeHeader () {
|
|
|
|
return function setContentTypeHeaderMiddleware(req, res, next) {
|
|
|
|
res.set('Content-Type', res.get('content-type') || res.get('Content-Type') || 'image/png');
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|