Improve routing via regular expressions

This commit is contained in:
Daniel García Aubert 2018-04-04 20:00:59 +02:00
parent ef22c46199
commit 11099c88dc

View File

@ -41,10 +41,11 @@ module.exports = class TileLayergroupController {
}
register (mapRouter) {
const number = (param) => `${param}(-?\\d+)`;
const number = () => `(-?\\d+)`;
const not = (val) => `(?!${val})([^\/]+?)`;
mapRouter.get(
`/:token/:${number('z')}/:${number('x')}/:${number('y')}@:${number('scale_factor')}?x.:format`,
`/:token/:z${number()}/:x${number()}/:y${number()}@:scale_factor${number()}?x.:format`,
layergroupToken(),
credentials(),
authorize(this.authApi),
@ -69,7 +70,7 @@ module.exports = class TileLayergroupController {
);
mapRouter.get(
`/:token/:${number('z')}/:${number('x')}/:${number('y')}.:format`,
`/:token/:z${number()}/:x${number()}/:y${number()}.:format`,
layergroupToken(),
credentials(),
authorize(this.authApi),
@ -94,8 +95,7 @@ module.exports = class TileLayergroupController {
);
mapRouter.get(
`/:token/:layer/:${number('z')}/:${number('x')}/:${number('y')}.(:format)`,
distinguishLayergroupFromStaticRoute(),
`/:token${not('static')}/:layer/:z${number()}/:x${number()}/:y${number()}.(:format)`,
layergroupToken(),
credentials(),
authorize(this.authApi),
@ -121,16 +121,6 @@ module.exports = class TileLayergroupController {
}
};
function distinguishLayergroupFromStaticRoute () {
return function distinguishLayergroupFromStaticRouteMiddleware(req, res, next) {
if (req.params.token === 'static') {
return next('route');
}
next();
};
}
function parseFormat (format = '') {
const prettyFormat = format.replace('.', '_');
return SUPPORTED_FORMATS[prettyFormat] ? prettyFormat : 'invalid';