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
+21 -7
View File
@@ -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'
]
}
+21 -7
View File
@@ -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'
]
}
+21 -7
View File
@@ -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'
]
}
+48
View File
@@ -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
+24 -20
View File
@@ -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));
});
}
};
+3 -7
View File
@@ -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));
}
};
+3 -7
View File
@@ -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));
}
};
+30 -3
View File
@@ -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'
]
}
}
},