From dd06de2632661e19d64c9fbc2be0ba1a8059f54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 4 Oct 2019 12:07:58 +0200 Subject: [PATCH] Use new routes configuration --- config/environments/development.js.example | 12 ++++----- config/environments/production.js.example | 12 ++++----- config/environments/staging.js.example | 12 ++++----- config/environments/test.js.example | 12 ++++----- lib/cartodb/api/api-router.js | 8 +++--- lib/cartodb/api/map/map-router.js | 28 ++++++++++++--------- lib/cartodb/api/template/template-router.js | 16 +++++++----- lib/cartodb/server.js | 2 +- 8 files changed, 55 insertions(+), 47 deletions(-) diff --git a/config/environments/development.js.example b/config/environments/development.js.example index a9cb7a78..91ff546c 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -20,7 +20,7 @@ var config = { // Note: each entry corresponds with an express' router. // You must define at least one path. However, middlewares are optional. ,routes: { - api: { + api: [{ paths: [ '/api/v1', '/user/:user/api/v1', @@ -36,21 +36,21 @@ var config = { ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, - map: { + map: [{ paths: [ '/map', ], middlewares: [] // Optional - }, + }], // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, - template: { + template: [{ paths: [ '/map/named' ], middlewares: [] // Optional - } - } + }] + }] } // Resource URLs expose endpoints to request/retrieve metadata associated to Maps: dataviews, analysis node status. diff --git a/config/environments/production.js.example b/config/environments/production.js.example index f6babe62..3e75a897 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -20,7 +20,7 @@ var config = { // Note: each entry corresponds with an express' router. // You must define at least one path. However, middlewares are optional. ,routes: { - api: { + api: [{ paths: [ '/api/v1', '/user/:user/api/v1', @@ -36,21 +36,21 @@ var config = { ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, - map: { + map: [{ paths: [ '/map', ], middlewares: [] // Optional - }, + }], // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, - template: { + template: [{ paths: [ '/map/named' ], middlewares: [] // Optional - } - } + }] + }] } // Resource URLs expose endpoints to request/retrieve metadata associated to Maps: dataviews, analysis node status. diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index a2675eb3..49e08ac7 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -20,7 +20,7 @@ var config = { // Note: each entry corresponds with an express' router. // You must define at least one path. However, middlewares are optional. ,routes: { - api: { + api: [{ paths: [ '/api/v1', '/user/:user/api/v1', @@ -36,21 +36,21 @@ var config = { ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, - map: { + map: [{ paths: [ '/map', ], middlewares: [] // Optional - }, + }], // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, - template: { + template: [{ paths: [ '/map/named' ], middlewares: [] // Optional - } - } + }] + }] } // Resource URLs expose endpoints to request/retrieve metadata associated to Maps: dataviews, analysis node status. diff --git a/config/environments/test.js.example b/config/environments/test.js.example index 9f69f2f0..04797ae9 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -20,7 +20,7 @@ var config = { // Note: each entry corresponds with an express' router. // You must define at least one path. However, middlewares are optional. ,routes: { - api: { + api: [{ paths: [ '/api/v1', '/user/:user/api/v1', @@ -36,21 +36,21 @@ var config = { ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, - map: { + map: [{ paths: [ '/map', ], middlewares: [] // Optional - }, + }], // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, - template: { + template: [{ paths: [ '/map/named' ], middlewares: [] // Optional - } - } + }] + }] } // Resource URLs expose endpoints to request/retrieve metadata associated to Maps: dataviews, analysis node status. diff --git a/lib/cartodb/api/api-router.js b/lib/cartodb/api/api-router.js index 60d0648b..0081c0eb 100644 --- a/lib/cartodb/api/api-router.js +++ b/lib/cartodb/api/api-router.js @@ -196,13 +196,13 @@ module.exports = class ApiRouter { this.templateRouter = new TemplateRouter({ collaborators }); } - register (app) { + register (app, routes) { // FIXME: we need a better way to reset cache while running tests if (process.env.NODE_ENV === 'test') { app.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache; } - Object.values(this.serverOptions.routes).forEach(route => { + routes.forEach(route => { const apiRouter = router({ mergeParams: true }); const apiPaths = route.paths; @@ -222,8 +222,8 @@ module.exports = class ApiRouter { apiRouter.use(cors()); apiRouter.use(user()); - this.templateRouter.register(apiRouter, route.template.paths, route.template.middlewares); - this.mapRouter.register(apiRouter, route.map.paths, route.map.middlewares); + this.templateRouter.register(apiRouter, route.template); + this.mapRouter.register(apiRouter, route.map); apiRouter.use(sendResponse()); apiRouter.use(syntaxError()); diff --git a/lib/cartodb/api/map/map-router.js b/lib/cartodb/api/map/map-router.js index a26637cb..2e17a59f 100644 --- a/lib/cartodb/api/map/map-router.js +++ b/lib/cartodb/api/map/map-router.js @@ -126,21 +126,25 @@ module.exports = class MapRouter { ); } - register (apiRouter, mapPaths, mapMiddlewares = []) { + register (apiRouter, routes) { const mapRouter = router({ mergeParams: true }); - mapMiddlewares.forEach(middleware => mapRouter.use(middleware())); + routes.forEach(route => { + const { paths, middlewares = [] } = route; - this.analysisLayergroupController.register(mapRouter); - this.attributesLayergroupController.register(mapRouter); - this.dataviewLayergroupController.register(mapRouter); - this.previewLayergroupController.register(mapRouter); - this.tileLayergroupController.register(mapRouter); - this.anonymousMapController.register(mapRouter); - this.previewTemplateController.register(mapRouter); - this.analysesController.register(mapRouter); - this.clusteredFeaturesLayergroupController.register(mapRouter); + middlewares.forEach(middleware => mapRouter.use(middleware())); - mapPaths.forEach(path => apiRouter.use(path, mapRouter)); + this.analysisLayergroupController.register(mapRouter); + this.attributesLayergroupController.register(mapRouter); + this.dataviewLayergroupController.register(mapRouter); + this.previewLayergroupController.register(mapRouter); + this.tileLayergroupController.register(mapRouter); + this.anonymousMapController.register(mapRouter); + this.previewTemplateController.register(mapRouter); + this.analysesController.register(mapRouter); + this.clusteredFeaturesLayergroupController.register(mapRouter); + + paths.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 b4a43053..566dfec4 100644 --- a/lib/cartodb/api/template/template-router.js +++ b/lib/cartodb/api/template/template-router.js @@ -54,15 +54,19 @@ module.exports = class TemplateRouter { ); } - register (apiRouter, templatePaths, templateMiddlewares = []) { + register (apiRouter, routes) { const templateRouter = router({ mergeParams: true }); - templateMiddlewares.forEach(middleware => templateRouter.use(middleware())); + routes.forEach(route => { + const { paths, middlewares = [] } = route; - this.namedMapController.register(templateRouter); - this.tileTemplateController.register(templateRouter); - this.adminTemplateController.register(templateRouter); + middlewares.forEach(middleware => templateRouter.use(middleware())); - templatePaths.forEach(path => apiRouter.use(path, templateRouter)); + this.namedMapController.register(templateRouter); + this.tileTemplateController.register(templateRouter); + this.adminTemplateController.register(templateRouter); + + paths.forEach(path => apiRouter.use(path, templateRouter)); + }); } }; diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 0ce6380e..f02ee384 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -33,7 +33,7 @@ module.exports = function createServer (serverOptions) { app.set('json replacer', jsonReplacer()); const apiRouter = new ApiRouter({ serverOptions, environmentOptions: global.environment }); - apiRouter.register(app); + apiRouter.register(app, serverOptions.routes.api); const versions = getAndValidateVersions(serverOptions);