Draft: be able to inject middlewares from configuration

This commit is contained in:
Daniel García Aubert 2019-09-30 19:18:24 +02:00
parent b881bec668
commit 9cfaf6eefc
6 changed files with 44 additions and 8 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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());

View File

@ -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);

View File

@ -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);