From 9cfaf6eefc635ae78748fb8fdcf310de0bf0664b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 30 Sep 2019 19:18:24 +0200 Subject: [PATCH] Draft: be able to inject middlewares from configuration --- config/environments/development.js.example | 12 +++++++++++- config/environments/production.js.example | 12 +++++++++++- config/environments/staging.js.example | 12 +++++++++++- lib/cartodb/api/api-router.js | 8 +++++--- lib/cartodb/api/map/map-router.js | 4 +++- lib/cartodb/api/template/template-router.js | 4 +++- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/config/environments/development.js.example b/config/environments/development.js.example index 68737330..adc7aa1d 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -22,12 +22,22 @@ var config = { '/api/v1', '/user/:user/api/v1', ], + // Attach middlewares at the begining of the req/res cycle + // to perform custom operations. + middlewares: [ + function noop () { + return function noopMiddleware (req, res, next) { + next(); + } + } + ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, map: { paths: [ '/map', - ] + ], + middlewares: [] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, diff --git a/config/environments/production.js.example b/config/environments/production.js.example index 5e8ab271..9173c53b 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -22,12 +22,22 @@ var config = { '/api/v1', '/user/:user/api/v1', ], + // Attach middlewares at the begining of the req/res cycle + // to perform custom operations. + middlewares: [ + function noop () { + return function noopMiddleware (req, res, next) { + next(); + } + } + ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, map: { paths: [ '/map', - ] + ], + middlewares: [] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 4741fd74..123ce930 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -22,12 +22,22 @@ var config = { '/api/v1', '/user/:user/api/v1', ], + // Attach middlewares at the begining of the req/res cycle + // to perform custom operations. + middlewares: [ + function noop () { + return function noopMiddleware (req, res, next) { + next(); + } + } + ], // Base url for the Detached Maps API // "/api/v1/map" is the new API, map: { paths: [ '/map', - ] + ], + middlewares: [] }, // Base url for the Templated Maps API // "/api/v1/map/named" is the new API, diff --git a/lib/cartodb/api/api-router.js b/lib/cartodb/api/api-router.js index fb62aa46..ac317839 100644 --- a/lib/cartodb/api/api-router.js +++ b/lib/cartodb/api/api-router.js @@ -204,8 +204,10 @@ module.exports = class ApiRouter { Object.keys(this.serverOptions.routes).forEach(apiVersion => { const routes = this.serverOptions.routes[apiVersion]; - const apiRouter = router({ mergeParams: true }); + const apiMiddlewares = routes.middlewares || []; + + apiMiddlewares.forEach(middleware => apiRouter.use(middleware())); apiRouter.use(logger(this.serverOptions)); apiRouter.use(initializeStatusCode()); @@ -219,8 +221,8 @@ module.exports = class ApiRouter { apiRouter.use(cors()); apiRouter.use(user()); - this.templateRouter.register(apiRouter, routes.template.paths); - this.mapRouter.register(apiRouter, routes.map.paths); + this.templateRouter.register(apiRouter, routes.template.paths, routes.template.middlewares); + this.mapRouter.register(apiRouter, routes.map.paths, routes.map.middlewares); 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 8fb7b8b2..a26637cb 100644 --- a/lib/cartodb/api/map/map-router.js +++ b/lib/cartodb/api/map/map-router.js @@ -126,9 +126,11 @@ module.exports = class MapRouter { ); } - register (apiRouter, mapPaths) { + register (apiRouter, mapPaths, mapMiddlewares = []) { const mapRouter = router({ mergeParams: true }); + mapMiddlewares.forEach(middleware => mapRouter.use(middleware())); + this.analysisLayergroupController.register(mapRouter); this.attributesLayergroupController.register(mapRouter); this.dataviewLayergroupController.register(mapRouter); diff --git a/lib/cartodb/api/template/template-router.js b/lib/cartodb/api/template/template-router.js index 0299679d..b4a43053 100644 --- a/lib/cartodb/api/template/template-router.js +++ b/lib/cartodb/api/template/template-router.js @@ -54,9 +54,11 @@ module.exports = class TemplateRouter { ); } - register (apiRouter, templatePaths) { + register (apiRouter, templatePaths, templateMiddlewares = []) { const templateRouter = router({ mergeParams: true }); + templateMiddlewares.forEach(middleware => templateRouter.use(middleware())); + this.namedMapController.register(templateRouter); this.tileTemplateController.register(templateRouter); this.adminTemplateController.register(templateRouter);