Create a sub-router for each plugin, but allows plugins to override main's routes

This commit is contained in:
Aurélien Georget 2016-09-01 18:32:04 +02:00
parent 4a552298e0
commit c4c58f3c9a
2 changed files with 75 additions and 31 deletions

View File

@ -1,4 +1,4 @@
'use strict'; 'use strict';
/** /**
* Module dependencies * Module dependencies
@ -53,38 +53,20 @@ module.exports = strapi => {
}; };
} }
if (((cluster.isWorker && strapi.config.reload.workers > 0) || (cluster.isMaster && strapi.config.reload.workers < 1)) || (!strapi.config.reload && cluster.isMaster)) { function routerChecker(value, endpoint, plugin) {
let route;
let controller;
let action; let action;
let policies = []; const route = regex.detectRoute(endpoint);
// Initialize the router.
if (!strapi.router) {
strapi.router = strapi.middlewares.router({
prefix: strapi.config.prefix
});
}
// Add response policy to the global variable
_.set(strapi.policies, 'responsesPolicy', responsesPolicy);
// Parse each route from the user config, load policies if any
// and match the controller and action to the desired endpoint.
_.forEach(strapi.config.routes, (value, endpoint) => {
try {
route = regex.detectRoute(endpoint);
// Check if the controller is a function. // Check if the controller is a function.
if (typeof value.controller === 'function') { if (typeof value.controller === 'function') {
action = value.controller; action = value.controller;
} else { } else {
controller = strapi.controllers[value.controller.toLowerCase()]; const controller = strapi.controllers[value.controller.toLowerCase()] || strapi.plugins[plugin].controllers[value.controller.toLowerCase()];
action = controller[value.action]; action = controller[value.action];
} }
// Init policies array. // Init policies array.
policies = []; const policies = [];
// Add the `globalPolicy`. // Add the `globalPolicy`.
policies.push(globalPolicy(endpoint, value, route)); policies.push(globalPolicy(endpoint, value, route));
@ -102,12 +84,71 @@ module.exports = strapi => {
} }
}); });
} }
return {
route: route,
policies: policies,
action: action
};
}
if (((cluster.isWorker && strapi.config.reload.workers > 0) || (cluster.isMaster && strapi.config.reload.workers < 1)) || (!strapi.config.reload && cluster.isMaster)) {
// Initialize the router.
if (!strapi.router) {
strapi.router = strapi.middlewares.router({
prefix: strapi.config.prefix
});
}
// Add response policy to the global variable.
_.set(strapi.policies, 'responsesPolicy', responsesPolicy);
// Parse each route from the user config, load policies if any
// and match the controller and action to the desired endpoint.
_.forEach(_.omit(strapi.config.routes, 'plugins'), (value, endpoint) => {
try {
const { route, policies, action } = routerChecker(value, endpoint);
strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action); strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
} catch (err) { } catch (err) {
strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.'); strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
} }
}); });
// Parse each plugin's routes.
_.forEach(strapi.config.routes.plugins, (value, plugin) => {
try {
// Create router for each plugin.
// Prefix router with the plugin's name.
const router = strapi.middlewares.router({
prefix: plugin
});
// Exclude routes with prefix.
const excludedRoutes = _.omitBy(value, o => !o.hasOwnProperty('prefix'));
// Add others routes to the plugin's router.
_.forEach(_.omit(value, _.keys(excludedRoutes)), (value, endpoint) => {
const { route, policies, action } = routerChecker(value, endpoint, plugin);
router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
});
// /!\ Could override main router's routes.
if (!_.isEmpty(excludedRoutes)) {
_.forEach(excludedRoutes, (value, endpoint) => {
const { route, policies, action } = routerChecker(value, endpoint, plugin);
strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
});
}
strapi.router.use(router.routes(), router.allowedMethods());
} catch (err) {
strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
}
});
// Let the router use our routes and allowed methods. // Let the router use our routes and allowed methods.
strapi.app.use(strapi.router.routes()); strapi.app.use(strapi.router.routes());
strapi.app.use(strapi.router.allowedMethods()); strapi.app.use(strapi.router.allowedMethods());

View File

@ -133,7 +133,10 @@ module.exports = strapi => {
}; };
// Delete the definition if it's empty. // Delete the definition if it's empty.
strapi.plugins[plugin.name] = _.omitBy(strapi.plugins[plugin.name], _.isEmpty); strapi.plugins[plugin.name] = _.omitBy(_.get(strapi.plugins, plugin.name), _.isEmpty);
// Merge API routes with the main ones.
_.set(strapi.config.routes, 'plugins.' + plugin.name, _.get(strapi.plugins, plugin.name + '.config.routes'));
// If the module doesn't have a definition at all // If the module doesn't have a definition at all
// just remove it completely from the dictionary. // just remove it completely from the dictionary.