From 9feea665505eaeda50feea9246a1458286e06ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 9 May 2018 14:59:21 +0200 Subject: [PATCH] Use routes configuration to create and mount routes and controllers --- config/environments/development.js.example | 28 +++++++++--- config/environments/production.js.example | 28 +++++++++--- config/environments/staging.js.example | 28 +++++++++--- config/environments/test.js.example | 48 +++++++++++++++++++++ lib/cartodb/api/api-router.js | 44 ++++++++++--------- lib/cartodb/api/map/map-router.js | 10 ++--- lib/cartodb/api/template/template-router.js | 10 ++--- lib/cartodb/server_options.js | 33 ++++++++++++-- 8 files changed, 171 insertions(+), 58 deletions(-) diff --git a/config/environments/development.js.example b/config/environments/development.js.example index e11b2a28..f9c58abb 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -32,28 +32,42 @@ var config = { // // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API ,routes: { - api: { - // "/tiles" is for compatibility with versions up to 1.6.x + v1: { paths: [ '/api/v1', '/user/:user/api/v1', - '/tiles' // for compatibility with versions up to 1.6.x ], // Base url for the Detached Maps API - // "maps" is the the new API, - // "tiles/layergroup" is for compatibility with versions up to 1.6.x + // "/api/v1/map" is the new API, map: { paths: [ '/map', - '/layergroup' ] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, + template: { + paths: [ + '/map/named' + ] + } + }, + // For compatibility with versions up to 1.6.x + v0: { + paths: [ + '/tiles' + ], + // Base url for the Detached Maps API + // "/tiles/layergroup" is for compatibility with versions up to 1.6.x + map: { + paths: [ + '/layergroup' + ] + }, + // Base url for the Templated Maps API // "/tiles/template" is for compatibility with versions up to 1.6.x template: { paths: [ - '/map/named', '/template' ] } diff --git a/config/environments/production.js.example b/config/environments/production.js.example index 24eb18fd..89c5e5bd 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -32,28 +32,42 @@ var config = { // // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API ,routes: { - api: { - // "/tiles" is for compatibility with versions up to 1.6.x + v1: { paths: [ '/api/v1', '/user/:user/api/v1', - '/tiles' // for compatibility with versions up to 1.6.x ], // Base url for the Detached Maps API - // "maps" is the the new API, - // "tiles/layergroup" is for compatibility with versions up to 1.6.x + // "/api/v1/map" is the new API, map: { paths: [ '/map', - '/layergroup' ] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, + template: { + paths: [ + '/map/named' + ] + } + }, + // For compatibility with versions up to 1.6.x + v0: { + paths: [ + '/tiles' + ], + // Base url for the Detached Maps API + // "/tiles/layergroup" is for compatibility with versions up to 1.6.x + map: { + paths: [ + '/layergroup' + ] + }, + // Base url for the Templated Maps API // "/tiles/template" is for compatibility with versions up to 1.6.x template: { paths: [ - '/map/named', '/template' ] } diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 28c66185..76353f9b 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -32,28 +32,42 @@ var config = { // // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API ,routes: { - api: { - // "/tiles" is for compatibility with versions up to 1.6.x + v1: { paths: [ '/api/v1', '/user/:user/api/v1', - '/tiles' // for compatibility with versions up to 1.6.x ], // Base url for the Detached Maps API - // "maps" is the the new API, - // "tiles/layergroup" is for compatibility with versions up to 1.6.x + // "/api/v1/map" is the new API, map: { paths: [ '/map', - '/layergroup' ] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, + template: { + paths: [ + '/map/named' + ] + } + }, + // For compatibility with versions up to 1.6.x + v0: { + paths: [ + '/tiles' + ], + // Base url for the Detached Maps API + // "/tiles/layergroup" is for compatibility with versions up to 1.6.x + map: { + paths: [ + '/layergroup' + ] + }, + // Base url for the Templated Maps API // "/tiles/template" is for compatibility with versions up to 1.6.x template: { paths: [ - '/map/named', '/template' ] } diff --git a/config/environments/test.js.example b/config/environments/test.js.example index 4d2c2d27..1f4e34a6 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -13,6 +13,8 @@ var config = { // from hostname. Must have a single grabbing block. ,user_from_host: '(.*)' + // DEPRECATED: use routes property instead + // --------------------------------------- // Base URLs for the APIs // // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API @@ -26,6 +28,52 @@ var config = { // "tiles/layergroup" is for compatibility with versions up to 1.6.x ,base_url_detached: '(?:/api/v1/map|/user/:user/api/v1/map|/tiles/layergroup)' + // Base URLs for the APIs + // + // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API + ,routes: { + v1: { + paths: [ + '/api/v1', + '/user/:user/api/v1', + ], + // Base url for the Detached Maps API + // "/api/v1/map" is the new API, + map: { + paths: [ + '/map', + ] + }, + // Base url for the Templated Maps API + // "/api/v1/map/named" is the new API, + template: { + paths: [ + '/map/named' + ] + } + }, + // For compatibility with versions up to 1.6.x + v0: { + paths: [ + '/tiles' + ], + // Base url for the Detached Maps API + // "/tiles/layergroup" is for compatibility with versions up to 1.6.x + map: { + paths: [ + '/layergroup' + ] + }, + // Base url for the Templated Maps API + // "/tiles/template" is for compatibility with versions up to 1.6.x + template: { + paths: [ + '/template' + ] + } + } + } + // Resource URLs expose endpoints to request/retrieve metadata associated to Maps: dataviews, analysis node status. // // This URLs depend on how `base_url_detached` and `user_from_host` are configured: the application can be diff --git a/lib/cartodb/api/api-router.js b/lib/cartodb/api/api-router.js index ecffe562..5165007d 100644 --- a/lib/cartodb/api/api-router.js +++ b/lib/cartodb/api/api-router.js @@ -177,8 +177,8 @@ module.exports = class ApiRouter { tablesExtentBackend }; - this.mapRouter = new MapRouter({ collaborators, serverOptions }); - this.templateRouter = new TemplateRouter({ collaborators, serverOptions }); + this.mapRouter = new MapRouter({ collaborators }); + this.templateRouter = new TemplateRouter({ collaborators }); } register (app) { @@ -187,29 +187,33 @@ module.exports = class ApiRouter { app.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache; } - const apiRouter = router(); + Object.keys(this.serverOptions.routes).forEach(apiVersion => { + const routes = this.serverOptions.routes[apiVersion]; - apiRouter.use(logger(this.serverOptions)); - apiRouter.use(bodyParser.json()); - apiRouter.use(servedByHostHeader()); - apiRouter.use(stats({ - enabled: this.serverOptions.useProfiler, - statsClient: global.statsClient - })); - apiRouter.use(lzmaMiddleware()); - apiRouter.use(cors()); - apiRouter.use(user()); + const apiRouter = router(); - this.templateRouter.register(apiRouter); - this.mapRouter.register(apiRouter); + apiRouter.use(logger(this.serverOptions)); + apiRouter.use(bodyParser.json()); + apiRouter.use(servedByHostHeader()); + apiRouter.use(stats({ + enabled: this.serverOptions.useProfiler, + statsClient: global.statsClient + })); + apiRouter.use(lzmaMiddleware()); + apiRouter.use(cors()); + apiRouter.use(user()); - apiRouter.use(sendResponse()); - apiRouter.use(syntaxError()); - apiRouter.use(errorMiddleware()); + this.templateRouter.register(apiRouter, routes.template.paths); + this.mapRouter.register(apiRouter, routes.map.paths); - const paths = this.serverOptions.routes.api.paths; + apiRouter.use(sendResponse()); + apiRouter.use(syntaxError()); + apiRouter.use(errorMiddleware()); - app.use(`(?:${paths.join('|')})`, apiRouter); + const apiPaths = routes.paths; + + apiPaths.forEach(path => app.use(path, apiRouter)); + }); } }; diff --git a/lib/cartodb/api/map/map-router.js b/lib/cartodb/api/map/map-router.js index a3c2bbaa..03f19937 100644 --- a/lib/cartodb/api/map/map-router.js +++ b/lib/cartodb/api/map/map-router.js @@ -10,9 +10,7 @@ const PreviewTemplateController = require('./preview-template-controller'); const AnalysesCatalogController = require('./analyses-catalog-controller'); module.exports = class MapRouter { - constructor ({ collaborators, serverOptions }) { - this.serverOptions = serverOptions; - + constructor ({ collaborators }) { const { analysisStatusBackend, attributesBackend, @@ -114,7 +112,7 @@ module.exports = class MapRouter { ); } - register (apiRouter) { + register (apiRouter, mapPaths) { const mapRouter = router(); this.analysisLayergroupController.register(mapRouter); @@ -126,8 +124,6 @@ module.exports = class MapRouter { this.previewTemplateController.register(mapRouter); this.analysesController.register(mapRouter); - const paths = this.serverOptions.routes.api.map.paths; - - apiRouter.use(`(?:${paths.join('|')})`, mapRouter); + mapPaths.forEach(path => apiRouter.use(path, mapRouter)); } }; diff --git a/lib/cartodb/api/template/template-router.js b/lib/cartodb/api/template/template-router.js index cfb883e7..9ddcf9fc 100644 --- a/lib/cartodb/api/template/template-router.js +++ b/lib/cartodb/api/template/template-router.js @@ -5,9 +5,7 @@ const AdminTemplateController = require('./admin-template-controller'); const TileTemplateController = require('./tile-template-controller'); module.exports = class TemplateRouter { - constructor ({ collaborators, serverOptions }) { - this.serverOptions = serverOptions; - + constructor ({ collaborators }) { const { pgConnection, templateMaps, @@ -54,15 +52,13 @@ module.exports = class TemplateRouter { ); } - register (apiRouter) { + register (apiRouter, templatePaths) { const templateRouter = router(); this.namedMapController.register(templateRouter); this.tileTemplateController.register(templateRouter); this.adminTemplateController.register(templateRouter); - const paths = this.serverOptions.routes.api.template.paths; - - apiRouter.use(`(?:${paths.join('|')})`, templateRouter); + templatePaths.forEach(path => apiRouter.use(path, templateRouter)); } }; diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index d05e5e1d..c3d0fdb8 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -67,22 +67,49 @@ module.exports = { // FIXME: Remove it. This is no longer needed, paths are defined in routers base_url_templated: global.environment.base_url_templated || '(?:/maps/named|/tiles/template)', + // Base URLs for the APIs + // + // See http://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API routes: global.environment.routes || { - api: { + v1: { paths: [ '/api/v1', - '/user/:user/api/v1' + '/user/:user/api/v1', ], + // Base url for the Detached Maps API + // "/api/v1/map" is the new API, map: { paths: [ - '/map' + '/map', ] }, + // Base url for the Templated Maps API + // "/api/v1/map/named" is the new API, template: { paths: [ '/map/named' ] } + }, + // For compatibility with versions up to 1.6.x + v0: { + paths: [ + '/tiles' + ], + // Base url for the Detached Maps API + // "/tiles/layergroup" is for compatibility with versions up to 1.6.x + map: { + paths: [ + '/layergroup' + ] + }, + // Base url for the Templated Maps API + // "/tiles/template" is for compatibility with versions up to 1.6.x + template: { + paths: [ + '/template' + ] + } } },