From 5b9f6086673847d2e0ea7520ef57982bdbadf6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 27 Mar 2018 18:46:54 +0200 Subject: [PATCH] Use express routers --- lib/cartodb/controllers/analyses.js | 8 ++--- .../controllers/layergroup/analysis.js | 8 ++--- .../controllers/layergroup/attributes.js | 8 ++--- .../controllers/layergroup/dataview.js | 20 ++++++------- lib/cartodb/controllers/layergroup/index.js | 2 -- lib/cartodb/controllers/layergroup/static.js | 12 ++++---- lib/cartodb/controllers/layergroup/tile.js | 16 +++++----- lib/cartodb/controllers/map.js | 26 +++++------------ lib/cartodb/controllers/named_maps.js | 11 ++++--- lib/cartodb/controllers/named_maps_admin.js | 29 ++++++++----------- lib/cartodb/controllers/server_info.js | 8 ++--- lib/cartodb/server.js | 24 ++++++++++----- 12 files changed, 75 insertions(+), 97 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index d08d2779..9c0e915c 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -17,11 +17,9 @@ function AnalysesController(pgConnection, authApi, userLimitsApi) { module.exports = AnalysesController; -AnalysesController.prototype.register = function (app) { - const { base_url_mapconfig: mapconfigBasePath } = app; - - app.get( - `${mapconfigBasePath}/analyses/catalog`, +AnalysesController.prototype.register = function (mapRouter) { + mapRouter.get( + `/analyses/catalog`, credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), diff --git a/lib/cartodb/controllers/layergroup/analysis.js b/lib/cartodb/controllers/layergroup/analysis.js index b5faedb1..9d58a53f 100644 --- a/lib/cartodb/controllers/layergroup/analysis.js +++ b/lib/cartodb/controllers/layergroup/analysis.js @@ -27,11 +27,9 @@ module.exports = class AnalysisController { this.surrogateKeysCache = surrogateKeysCache; } - register (app) { - const { base_url_mapconfig: mapConfigBasePath } = app; - - app.get( - `${mapConfigBasePath}/:token/analysis/node/:nodeId`, + register (mapRouter) { + mapRouter.get( + `/:token/analysis/node/:nodeId`, layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup/attributes.js b/lib/cartodb/controllers/layergroup/attributes.js index babe2621..4965fe44 100644 --- a/lib/cartodb/controllers/layergroup/attributes.js +++ b/lib/cartodb/controllers/layergroup/attributes.js @@ -31,11 +31,9 @@ module.exports = class AttributesController { this.surrogateKeysCache = surrogateKeysCache; } - register (app) { - const { base_url_mapconfig: mapConfigBasePath } = app; - - app.get( - `${mapConfigBasePath}/:token/:layer/attributes/:fid`, + register (mapRouter) { + mapRouter.get( + `/:token/:layer/attributes/:fid`, layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup/dataview.js b/lib/cartodb/controllers/layergroup/dataview.js index 3864ce37..b768ee89 100644 --- a/lib/cartodb/controllers/layergroup/dataview.js +++ b/lib/cartodb/controllers/layergroup/dataview.js @@ -46,14 +46,12 @@ module.exports = class DataviewController { this.surrogateKeysCache = surrogateKeysCache; } - register (app) { - const { base_url_mapconfig: mapConfigBasePath } = app; - + register (mapRouter) { // Undocumented/non-supported API endpoint methods. // Use at your own peril. - app.get( - `${mapConfigBasePath}/:token/dataview/:dataviewName`, + mapRouter.get( + `/:token/dataview/:dataviewName`, layergroupToken(), credentials(), authorize(this.authApi), @@ -74,8 +72,8 @@ module.exports = class DataviewController { sendResponse() ); - app.get( - `${mapConfigBasePath}/:token/:layer/widget/:dataviewName`, + mapRouter.get( + `/:token/:layer/widget/:dataviewName`, layergroupToken(), credentials(), authorize(this.authApi), @@ -96,8 +94,8 @@ module.exports = class DataviewController { sendResponse() ); - app.get( - `${mapConfigBasePath}/:token/dataview/:dataviewName/search`, + mapRouter.get( + `/:token/dataview/:dataviewName/search`, layergroupToken(), credentials(), authorize(this.authApi), @@ -118,8 +116,8 @@ module.exports = class DataviewController { sendResponse() ); - app.get( - `${mapConfigBasePath}/:token/:layer/widget/:dataviewName/search`, + mapRouter.get( + `/:token/:layer/widget/:dataviewName/search`, layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup/index.js b/lib/cartodb/controllers/layergroup/index.js index e4c1770c..fbbe1fd8 100644 --- a/lib/cartodb/controllers/layergroup/index.js +++ b/lib/cartodb/controllers/layergroup/index.js @@ -109,6 +109,4 @@ LayergroupController.prototype.register = function(app) { ); analysisController.register(app); - - }; diff --git a/lib/cartodb/controllers/layergroup/static.js b/lib/cartodb/controllers/layergroup/static.js index 56f9169d..70957b53 100644 --- a/lib/cartodb/controllers/layergroup/static.js +++ b/lib/cartodb/controllers/layergroup/static.js @@ -31,13 +31,11 @@ module.exports = class StaticController { this.surrogateKeysCache = surrogateKeysCache; } - register (app) { - const { base_url_mapconfig: mapConfigBasePath } = app; - + register (mapRouter) { const forcedFormat = 'png'; - app.get( - `${mapConfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, + mapRouter.get( + `/static/center/:token/:z/:lat/:lng/:width/:height.:format`, layergroupToken(), credentials(), authorize(this.authApi), @@ -59,8 +57,8 @@ module.exports = class StaticController { sendResponse() ); - app.get( - `${mapConfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, + mapRouter.get( + `/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup/tile.js b/lib/cartodb/controllers/layergroup/tile.js index d0df6fef..091a8f35 100644 --- a/lib/cartodb/controllers/layergroup/tile.js +++ b/lib/cartodb/controllers/layergroup/tile.js @@ -41,11 +41,9 @@ module.exports = class TileController { this.surrogateKeysCache = surrogateKeysCache; } - register (app) { - const { base_url_mapconfig: mapConfigBasePath } = app; - - app.get( - `${mapConfigBasePath}/:token/:z/:x/:y@:scale_factor?x.:format`, + register (mapRouter) { + mapRouter.get( + `/:token/:z/:x/:y@:scale_factor?x.:format`, layergroupToken(), credentials(), authorize(this.authApi), @@ -70,8 +68,8 @@ module.exports = class TileController { sendResponse() ); - app.get( - `${mapConfigBasePath}/:token/:z/:x/:y.:format`, + mapRouter.get( + `/:token/:z/:x/:y.:format`, layergroupToken(), credentials(), authorize(this.authApi), @@ -96,8 +94,8 @@ module.exports = class TileController { sendResponse() ); - app.get( - `${mapConfigBasePath}/:token/:layer/:z/:x/:y.(:format)`, + mapRouter.get( + `/:token/:layer/:z/:x/:y.(:format)`, distinguishLayergroupFromStaticRoute(), layergroupToken(), credentials(), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index bbd9483b..f42d1b5d 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -62,32 +62,22 @@ function MapController ( module.exports = MapController; -MapController.prototype.register = function(app) { - const { base_url_mapconfig: mapConfigBasePath, base_url_templated: templateBasePath } = app; - - app.get( - `${mapConfigBasePath}`, - this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS) - ); - - app.post( - `${mapConfigBasePath}`, - this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS) - ); +MapController.prototype.register = function(mapRouter, templateRouter) { + mapRouter.get(`/`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS)); + mapRouter.post(`/`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS)); + mapRouter.options(`/`, cors('Content-Type')); const useTemplate = true; - app.get( - `${templateBasePath}/:template_id/jsonp`, + templateRouter.get( + `/:template_id/jsonp`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate) ); - app.post( - `${templateBasePath}/:template_id`, + templateRouter.post( + `/:template_id`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate) ); - - app.options(`${mapConfigBasePath}`, cors('Content-Type')); }; MapController.prototype.composeCreateMapMiddleware = function (endpointGroup, useTemplate = false) { diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 044f98fa..c5347190 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -47,11 +47,10 @@ function NamedMapsController ( module.exports = NamedMapsController; -NamedMapsController.prototype.register = function(app) { - const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app; +NamedMapsController.prototype.register = function(mapRouter, templateRouter) { - app.get( - `${templateBasePath}/:template_id/:layer/:z/:x/:y.(:format)`, + templateRouter.get( + `/:template_id/:layer/:z/:x/:y.(:format)`, credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), @@ -74,8 +73,8 @@ NamedMapsController.prototype.register = function(app) { vectorError() ); - app.get( - `${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`, + mapRouter.get( + `/static/named/:template_id/:width/:height.:format`, credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 5e3c40dc..95ac0d51 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -19,11 +19,9 @@ function NamedMapsAdminController(authApi, templateMaps, userLimitsApi) { module.exports = NamedMapsAdminController; -NamedMapsAdminController.prototype.register = function (app) { - const { base_url_templated: templateBasePath } = app; - - app.post( - `${templateBasePath}/`, +NamedMapsAdminController.prototype.register = function (templateRouter) { + templateRouter.post( + `/`, credentials(), checkContentType({ action: 'POST', label: 'POST TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }), @@ -32,8 +30,8 @@ NamedMapsAdminController.prototype.register = function (app) { sendResponse() ); - app.put( - `${templateBasePath}/:template_id`, + templateRouter.put( + `/:template_id`, credentials(), checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }), @@ -42,8 +40,8 @@ NamedMapsAdminController.prototype.register = function (app) { sendResponse() ); - app.get( - `${templateBasePath}/:template_id`, + templateRouter.get( + `/:template_id`, credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET), @@ -51,8 +49,8 @@ NamedMapsAdminController.prototype.register = function (app) { sendResponse() ); - app.delete( - `${templateBasePath}/:template_id`, + templateRouter.delete( + `/:template_id`, credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE), @@ -60,8 +58,8 @@ NamedMapsAdminController.prototype.register = function (app) { sendResponse() ); - app.get( - `${templateBasePath}/`, + templateRouter.get( + `/`, credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST), @@ -69,10 +67,7 @@ NamedMapsAdminController.prototype.register = function (app) { sendResponse() ); - app.options( - `${templateBasePath}/:template_id`, - cors('Content-Type') - ); + templateRouter.options(`/:template_id`, cors('Content-Type')); }; function checkContentType ({ action, label }) { diff --git a/lib/cartodb/controllers/server_info.js b/lib/cartodb/controllers/server_info.js index c70de9fa..12f6432b 100644 --- a/lib/cartodb/controllers/server_info.js +++ b/lib/cartodb/controllers/server_info.js @@ -12,10 +12,10 @@ function ServerInfoController(versions) { module.exports = ServerInfoController; -ServerInfoController.prototype.register = function(app) { - app.get('/health', this.health.bind(this)); - app.get('/', this.welcome.bind(this)); - app.get('/version', this.version.bind(this)); +ServerInfoController.prototype.register = function(monitorRouter) { + monitorRouter.get('/health', this.health.bind(this)); + monitorRouter.get('/', this.welcome.bind(this)); + monitorRouter.get('/version', this.version.bind(this)); }; ServerInfoController.prototype.welcome = function(req, res) { diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 9231fb69..f9161b21 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -116,8 +116,6 @@ module.exports = function(serverOptions) { // initialize express server var app = bootstrap(serverOptions); - // Extend windshaft with all the elements of the options object - _.extend(app, serverOptions); var mapStore = new windshaft.storage.MapStore({ pool: redisPool, @@ -222,6 +220,12 @@ module.exports = function(serverOptions) { * Routing ******************************************************************************************************************/ + const mapRouter = express.Router(); + const templateRouter = express.Router(); + const monitorRouter = express.Router(); + + const { base_url_mapconfig: mapConfigBasePath, base_url_templated: templateBasePath } = serverOptions; + new controller.Layergroup( pgConnection, mapStore, @@ -233,7 +237,7 @@ module.exports = function(serverOptions) { layergroupAffectedTablesCache, analysisBackend, authApi - ).register(app); + ).register(mapRouter); new controller.Map( pgConnection, @@ -246,7 +250,7 @@ module.exports = function(serverOptions) { mapConfigAdapter, statsBackend, authApi - ).register(app); + ).register(mapRouter, templateRouter); new controller.NamedMaps( namedMapProviderCache, @@ -258,13 +262,17 @@ module.exports = function(serverOptions) { pgConnection, authApi, userLimitsApi - ).register(app); + ).register(mapRouter, templateRouter); - new controller.NamedMapsAdmin(authApi, templateMaps, userLimitsApi).register(app); + new controller.NamedMapsAdmin(authApi, templateMaps, userLimitsApi).register(templateRouter); - new controller.Analyses(pgConnection, authApi, userLimitsApi).register(app); + new controller.Analyses(pgConnection, authApi, userLimitsApi).register(mapRouter); - new controller.ServerInfo(versions).register(app); + new controller.ServerInfo(versions).register(monitorRouter); + + app.use(mapConfigBasePath, mapRouter); + app.use(templateBasePath, templateRouter); + app.use('/', monitorRouter); /******************************************************************************************************************* * END Routing