Permalink
Browse files

Remove workers concept, and herd dependency

  • Loading branch information...
1 parent 83ff647 commit d31c85b9344cb08380f3d87e537422ab2a913352 @Aurelsicoko Aurelsicoko committed Dec 2, 2016
View
@@ -73,5 +73,6 @@
"node": ">= 7.0.0",
"npm": ">= 3.0.0"
},
- "license": "MIT"
+ "license": "MIT",
+ "dependencies": {}
}
@@ -2,10 +2,7 @@
"host": "localhost",
"port": 1337,
"frontendUrl": "",
- "reload": {
- "timeout": 1000,
- "workers": 0
- },
+ "reload": true,
"logger": true,
"parser": {
"encode": "utf-8",
@@ -1,9 +1,6 @@
{
"frontendUrl": "",
- "reload": {
- "timeout": 1000,
- "workers": 0
- },
+ "reload": false,
"logger": true,
"parser": {
"encode": "utf-8",
@@ -8,7 +8,6 @@
// Node.js core.
const REPL = require('repl');
-const cluster = require('cluster');
// Public node modules.
const _ = require('lodash');
@@ -29,9 +28,7 @@ const logger = require('strapi-utils').logger;
module.exports = function () {
// Only log if the process is a master.
- if (cluster.isMaster) {
- strapi.log.info('Starting the application in interactive mode...');
- }
+ strapi.log.info('Starting the application in interactive mode...');
strapi.start({}, err => {
@@ -44,19 +41,17 @@ module.exports = function () {
}
// Open the Node.js REPL.
- if ((cluster.isMaster && _.isEmpty(cluster.workers)) || cluster.worker.id === 1) {
- const repl = REPL.start(strapi.config.name + ' > ' || 'strapi > ');
- repl.on('exit', err => {
-
- // Log and exit the REPL in case there is an error
- // while we were trying to open the REPL.
- if (err) {
- logger.error(err);
- process.exit(1);
- }
-
- process.exit(0);
- });
- }
+ const repl = REPL.start(strapi.config.name + ' > ' || 'strapi > ');
+ repl.on('exit', err => {
+
+ // Log and exit the REPL in case there is an error
+ // while we were trying to open the REPL.
+ if (err) {
+ logger.error(err);
+ process.exit(1);
+ }
+
+ process.exit(0);
+ });
});
};
@@ -4,9 +4,6 @@
* Module dependencies
*/
-// Node.js core.
-const cluster = require('cluster');
-
// Public node modules.
const _ = require('lodash');
const Boom = require('boom');
@@ -43,62 +40,104 @@ module.exports = strapi => {
const Joi = strapi.middlewares.joiRouter.Joi;
const builder = joijson.builder(Joi);
- 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.joiRouter();
- strapi.router.prefix(strapi.config.prefix);
+ // Initialize the router.
+ if (!strapi.router) {
+ strapi.router = strapi.middlewares.joiRouter();
+ strapi.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 => {
+ if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
+ return;
}
- // 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.
+ const endpoint = `${value.method} ${value.path}`;
- _.forEach(strapi.config.routes, value => {
- if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
- return;
+ try {
+ const {route, policies, action, validate} = routerChecker(value, endpoint);
+
+ if (_.isUndefined(action) || !_.isFunction(action)) {
+ return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
}
- const endpoint = `${value.method} ${value.path}`;
+ strapi.router.route(_.omitBy({
+ method: value.method,
+ path: value.path,
+ handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
+ validate
+ }, _.isEmpty));
+ } catch (err) {
+ cb(err);
+ }
+ });
- try {
- const {route, policies, action, validate} = routerChecker(value, endpoint);
+ // Create router for admin.
+ // Prefix router with the admin's name.
+ const routerAdmin = strapi.middlewares.joiRouter();
- if (_.isUndefined(action) || !_.isFunction(action)) {
- return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
- }
+ _.forEach(strapi.admin.config.routes, value => {
+ if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
+ return;
+ }
- strapi.router.route(_.omitBy({
- method: value.method,
- path: value.path,
- handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
- validate
- }, _.isEmpty));
- } catch (err) {
- cb(err);
+ const endpoint = `${value.method} ${value.path}`;
+
+ try {
+ const {route, policies, action, validate} = routerChecker(value, endpoint);
+
+ if (_.isUndefined(action) || !_.isFunction(action)) {
+ return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
}
- });
- // Create router for admin.
- // Prefix router with the admin's name.
- const routerAdmin = strapi.middlewares.joiRouter();
+ routerAdmin.route(_.omitBy({
+ method: value.method,
+ path: value.path,
+ handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
+ validate
+ }, _.isEmpty));
+ } catch (err) {
+ cb(err);
+ }
+ });
+
+ routerAdmin.prefix(strapi.config.admin || `/${strapi.config.paths.admin}`);
- _.forEach(strapi.admin.config.routes, value => {
+ // TODO:
+ // - Mount on main router `strapi.router.use(routerAdmin.middleware());`
+
+ // Mount admin router on Strapi router
+ strapi.app.use(routerAdmin.middleware());
+
+ // Parse each plugin's routes.
+ _.forEach(strapi.config.plugins.routes, (value, plugin) => {
+ // Create router for each plugin.
+ // Prefix router with the plugin's name.
+ const router = strapi.middlewares.joiRouter();
+
+ // 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 => {
if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
return;
}
const endpoint = `${value.method} ${value.path}`;
try {
- const {route, policies, action, validate} = routerChecker(value, endpoint);
+ const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
if (_.isUndefined(action) || !_.isFunction(action)) {
return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
}
- routerAdmin.route(_.omitBy({
+ router.route(_.omitBy({
method: value.method,
path: value.path,
handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
@@ -109,25 +148,11 @@ module.exports = strapi => {
}
});
- routerAdmin.prefix(strapi.config.admin || `/${strapi.config.paths.admin}`);
-
- // TODO:
- // - Mount on main router `strapi.router.use(routerAdmin.middleware());`
-
- // Mount admin router on Strapi router
- strapi.app.use(routerAdmin.middleware());
-
- // Parse each plugin's routes.
- _.forEach(strapi.config.plugins.routes, (value, plugin) => {
- // Create router for each plugin.
- // Prefix router with the plugin's name.
- const router = strapi.middlewares.joiRouter();
+ 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 => {
+ // /!\ Could override main router's routes.
+ if (!_.isEmpty(excludedRoutes)) {
+ _.forEach(excludedRoutes, value => {
if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
return;
}
@@ -141,7 +166,7 @@ module.exports = strapi => {
return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
}
- router.route(_.omitBy({
+ strapi.router.route(_.omitBy({
method: value.method,
path: value.path,
handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
@@ -151,69 +176,38 @@ module.exports = strapi => {
cb(err);
}
});
+ }
- router.prefix('/' + plugin);
-
- // /!\ Could override main router's routes.
- if (!_.isEmpty(excludedRoutes)) {
- _.forEach(excludedRoutes, value => {
- if (_.isEmpty(_.get(value, 'method')) || _.isEmpty(_.get(value, 'path'))) {
- return;
- }
-
- const endpoint = `${value.method} ${value.path}`;
-
- try {
- const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
-
- if (_.isUndefined(action) || !_.isFunction(action)) {
- return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
- }
-
- strapi.router.route(_.omitBy({
- method: value.method,
- path: value.path,
- handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
- validate
- }, _.isEmpty));
- } catch (err) {
- cb(err);
- }
- });
- }
-
- // TODO:
- // - Mount on main router `strapi.router.use(router.middleware());`
-
- // Mount plugin router
- strapi.app.use(router.middleware());
- });
+ // TODO:
+ // - Mount on main router `strapi.router.use(router.middleware());`
- // Wrap error into Boom object
- strapi.app.use(async (ctx, next) => {
- try {
- await next();
+ // Mount plugin router
+ strapi.app.use(router.middleware());
+ });
- if (ctx.status >= 400 || _.get(ctx.body, 'isBoom')) {
- ctx.throw(ctx.status);
- }
- } catch (error) {
- const formattedError = _.get(ctx.body, 'isBoom') ? ctx.body : Boom.wrap(error, error.status, ctx.body);
+ // Wrap error into Boom object
+ strapi.app.use(async (ctx, next) => {
+ try {
+ await next();
- ctx.status = formattedError.output.statusCode || error.status || 500;
- ctx.body = formattedError.output.payload;
+ if (ctx.status >= 400 || _.get(ctx.body, 'isBoom')) {
+ ctx.throw(ctx.status);
}
- });
+ } catch (error) {
+ const formattedError = _.get(ctx.body, 'isBoom') ? ctx.body : Boom.wrap(error, error.status, ctx.body);
- // Let the router use our routes and allowed methods.
- strapi.app.use(strapi.router.middleware());
- strapi.app.use(strapi.router.router.allowedMethods({
- throw: false,
- notImplemented: () => Boom.notImplemented(),
- methodNotAllowed: () => Boom.methodNotAllowed()
- }));
-
- }
+ ctx.status = formattedError.output.statusCode || error.status || 500;
+ ctx.body = formattedError.output.payload;
+ }
+ });
+
+ // Let the router use our routes and allowed methods.
+ strapi.app.use(strapi.router.middleware());
+ strapi.app.use(strapi.router.router.allowedMethods({
+ throw: false,
+ notImplemented: () => Boom.notImplemented(),
+ methodNotAllowed: () => Boom.methodNotAllowed()
+ }));
cb();
@@ -4,9 +4,6 @@
* Module dependencies
*/
-// Node.js core.
-const cluster = require('cluster');
-
/**
* WebSockets hook
*/
@@ -28,15 +25,13 @@ module.exports = strapi => {
initialize: cb => {
if (strapi.config.websockets === true) {
- if (cluster.isMaster) {
- global.io = require('socket.io')(strapi.server);
-
- if (strapi.config.reload.workers > 0) {
- strapi.log.warn('Strapi doesn\'t handle WebSockets using multiple nodes yet.');
- strapi.log.warn('WebSockets only work on the master process.');
- strapi.log.warn('You can disable the `websockets` and implement Socket.IO on your own.');
- strapi.log.warn('Feel free to join us on Slack to talk about it: http://slack.strapi.io/');
- }
+ global.io = require('socket.io')(strapi.server);
+
+ if (strapi.config.reload.workers > 0) {
+ strapi.log.warn('Strapi doesn\'t handle WebSockets using multiple nodes yet.');
+ strapi.log.warn('WebSockets only work on the master process.');
+ strapi.log.warn('You can disable the `websockets` and implement Socket.IO on your own.');
+ strapi.log.warn('Feel free to join us on Slack to talk about it: http://slack.strapi.io/');
}
}
Oops, something went wrong.

0 comments on commit d31c85b

Please sign in to comment.