Use routes configuration to create and mount routes and controllers

This commit is contained in:
Daniel García Aubert 2018-05-09 14:59:21 +02:00
parent 7ed819e84a
commit 9feea66550
8 changed files with 171 additions and 58 deletions

View File

@ -32,28 +32,42 @@ var config = {
// //
// See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API
,routes: { ,routes: {
api: { v1: {
// "/tiles" is for compatibility with versions up to 1.6.x
paths: [ paths: [
'/api/v1', '/api/v1',
'/user/:user/api/v1', '/user/:user/api/v1',
'/tiles' // for compatibility with versions up to 1.6.x
], ],
// Base url for the Detached Maps API // Base url for the Detached Maps API
// "maps" is the the new API, // "/api/v1/map" is the new API,
// "tiles/layergroup" is for compatibility with versions up to 1.6.x
map: { map: {
paths: [ paths: [
'/map', '/map',
'/layergroup'
] ]
}, },
// Base url for the Templated Maps API // Base url for the Templated Maps API
// "/api/v1/map/named" is the new 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 // "/tiles/template" is for compatibility with versions up to 1.6.x
template: { template: {
paths: [ paths: [
'/map/named',
'/template' '/template'
] ]
} }

View File

@ -32,28 +32,42 @@ var config = {
// //
// See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API
,routes: { ,routes: {
api: { v1: {
// "/tiles" is for compatibility with versions up to 1.6.x
paths: [ paths: [
'/api/v1', '/api/v1',
'/user/:user/api/v1', '/user/:user/api/v1',
'/tiles' // for compatibility with versions up to 1.6.x
], ],
// Base url for the Detached Maps API // Base url for the Detached Maps API
// "maps" is the the new API, // "/api/v1/map" is the new API,
// "tiles/layergroup" is for compatibility with versions up to 1.6.x
map: { map: {
paths: [ paths: [
'/map', '/map',
'/layergroup'
] ]
}, },
// Base url for the Templated Maps API // Base url for the Templated Maps API
// "/api/v1/map/named" is the new 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 // "/tiles/template" is for compatibility with versions up to 1.6.x
template: { template: {
paths: [ paths: [
'/map/named',
'/template' '/template'
] ]
} }

View File

@ -32,28 +32,42 @@ var config = {
// //
// See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API // See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API
,routes: { ,routes: {
api: { v1: {
// "/tiles" is for compatibility with versions up to 1.6.x
paths: [ paths: [
'/api/v1', '/api/v1',
'/user/:user/api/v1', '/user/:user/api/v1',
'/tiles' // for compatibility with versions up to 1.6.x
], ],
// Base url for the Detached Maps API // Base url for the Detached Maps API
// "maps" is the the new API, // "/api/v1/map" is the new API,
// "tiles/layergroup" is for compatibility with versions up to 1.6.x
map: { map: {
paths: [ paths: [
'/map', '/map',
'/layergroup'
] ]
}, },
// Base url for the Templated Maps API // Base url for the Templated Maps API
// "/api/v1/map/named" is the new 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 // "/tiles/template" is for compatibility with versions up to 1.6.x
template: { template: {
paths: [ paths: [
'/map/named',
'/template' '/template'
] ]
} }

View File

@ -13,6 +13,8 @@ var config = {
// from hostname. Must have a single grabbing block. // from hostname. Must have a single grabbing block.
,user_from_host: '(.*)' ,user_from_host: '(.*)'
// DEPRECATED: use routes property instead
// ---------------------------------------
// Base URLs for the APIs // Base URLs for the APIs
// //
// See https://github.com/CartoDB/Windshaft-cartodb/wiki/Unified-Map-API // 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 // "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_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. // 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 // This URLs depend on how `base_url_detached` and `user_from_host` are configured: the application can be

View File

@ -177,8 +177,8 @@ module.exports = class ApiRouter {
tablesExtentBackend tablesExtentBackend
}; };
this.mapRouter = new MapRouter({ collaborators, serverOptions }); this.mapRouter = new MapRouter({ collaborators });
this.templateRouter = new TemplateRouter({ collaborators, serverOptions }); this.templateRouter = new TemplateRouter({ collaborators });
} }
register (app) { register (app) {
@ -187,29 +187,33 @@ module.exports = class ApiRouter {
app.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache; 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)); const apiRouter = router();
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());
this.templateRouter.register(apiRouter); apiRouter.use(logger(this.serverOptions));
this.mapRouter.register(apiRouter); 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()); this.templateRouter.register(apiRouter, routes.template.paths);
apiRouter.use(syntaxError()); this.mapRouter.register(apiRouter, routes.map.paths);
apiRouter.use(errorMiddleware());
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));
});
} }
}; };

View File

@ -10,9 +10,7 @@ const PreviewTemplateController = require('./preview-template-controller');
const AnalysesCatalogController = require('./analyses-catalog-controller'); const AnalysesCatalogController = require('./analyses-catalog-controller');
module.exports = class MapRouter { module.exports = class MapRouter {
constructor ({ collaborators, serverOptions }) { constructor ({ collaborators }) {
this.serverOptions = serverOptions;
const { const {
analysisStatusBackend, analysisStatusBackend,
attributesBackend, attributesBackend,
@ -114,7 +112,7 @@ module.exports = class MapRouter {
); );
} }
register (apiRouter) { register (apiRouter, mapPaths) {
const mapRouter = router(); const mapRouter = router();
this.analysisLayergroupController.register(mapRouter); this.analysisLayergroupController.register(mapRouter);
@ -126,8 +124,6 @@ module.exports = class MapRouter {
this.previewTemplateController.register(mapRouter); this.previewTemplateController.register(mapRouter);
this.analysesController.register(mapRouter); this.analysesController.register(mapRouter);
const paths = this.serverOptions.routes.api.map.paths; mapPaths.forEach(path => apiRouter.use(path, mapRouter));
apiRouter.use(`(?:${paths.join('|')})`, mapRouter);
} }
}; };

View File

@ -5,9 +5,7 @@ const AdminTemplateController = require('./admin-template-controller');
const TileTemplateController = require('./tile-template-controller'); const TileTemplateController = require('./tile-template-controller');
module.exports = class TemplateRouter { module.exports = class TemplateRouter {
constructor ({ collaborators, serverOptions }) { constructor ({ collaborators }) {
this.serverOptions = serverOptions;
const { const {
pgConnection, pgConnection,
templateMaps, templateMaps,
@ -54,15 +52,13 @@ module.exports = class TemplateRouter {
); );
} }
register (apiRouter) { register (apiRouter, templatePaths) {
const templateRouter = router(); const templateRouter = router();
this.namedMapController.register(templateRouter); this.namedMapController.register(templateRouter);
this.tileTemplateController.register(templateRouter); this.tileTemplateController.register(templateRouter);
this.adminTemplateController.register(templateRouter); this.adminTemplateController.register(templateRouter);
const paths = this.serverOptions.routes.api.template.paths; templatePaths.forEach(path => apiRouter.use(path, templateRouter));
apiRouter.use(`(?:${paths.join('|')})`, templateRouter);
} }
}; };

View File

@ -67,22 +67,49 @@ module.exports = {
// FIXME: Remove it. This is no longer needed, paths are defined in routers // 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_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 || { routes: global.environment.routes || {
api: { v1: {
paths: [ paths: [
'/api/v1', '/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: { map: {
paths: [ paths: [
'/map' '/map',
] ]
}, },
// Base url for the Templated Maps API
// "/api/v1/map/named" is the new API,
template: { template: {
paths: [ paths: [
'/map/named' '/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'
]
}
} }
}, },