remove hotswap (#6835)

v1.18.x
Barış Soner Uşaklı 6 years ago committed by GitHub
parent 92744a7200
commit 60c58870af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,34 +0,0 @@
'use strict';
var HotSwap = {};
var winston = require('winston');
var stack;
HotSwap.prepare = function (app) {
stack = app._router.stack;
};
HotSwap.find = function (id) {
if (stack) {
for (var x = 0, numEntries = stack.length; x < numEntries; x += 1) {
if (stack[x].handle.hotswapId === id) {
return x;
}
}
} else {
winston.error('[hotswap] HotSwap module has not been prepared!');
}
};
HotSwap.replace = function (id, router) {
var idx = HotSwap.find(id);
if (idx) {
delete stack[idx].handle; // Destroy the old router
stack[idx].handle = router; // Replace with the new one
winston.verbose('[hotswap] Router with id `' + id + '` replaced successfully');
} else {
winston.warn('[hotswap] Could not find router in stack with hotswapId `' + id + '`');
}
};
module.exports = HotSwap;

@ -5,11 +5,8 @@ var path = require('path');
var async = require('async');
var winston = require('winston');
var semver = require('semver');
var express = require('express');
var nconf = require('nconf');
var hotswap = require('./hotswap');
var app;
var middleware;
@ -53,7 +50,6 @@ Plugins.init = function (nbbApp, nbbMiddleware, callback) {
if (nbbApp) {
app = nbbApp;
middleware = nbbMiddleware;
hotswap.prepare(nbbApp);
}
if (global.env === 'development') {
@ -116,14 +112,7 @@ Plugins.reload = function (callback) {
], callback);
};
Plugins.reloadRoutes = function (callback) {
var router = express.Router();
router.hotswapId = 'plugins';
router.render = function () {
app.render.apply(app, arguments);
};
Plugins.reloadRoutes = function (router, callback) {
var controllers = require('./controllers');
Plugins.fireHook('static:app.load', { app: app, router: router, middleware: middleware, controllers: controllers }, function (err) {
if (err) {
@ -131,7 +120,6 @@ Plugins.reloadRoutes = function (callback) {
return callback(err);
}
hotswap.replace('plugins', router);
winston.verbose('[plugins] All plugins reloaded and rerouted');
callback();
});

@ -5,11 +5,9 @@ var passport = require('passport');
var passportLocal = require('passport-local').Strategy;
var nconf = require('nconf');
var winston = require('winston');
var express = require('express');
var controllers = require('../controllers');
var plugins = require('../plugins');
var hotswap = require('../hotswap');
var loginStrategies = [];
@ -42,10 +40,7 @@ Auth.getLoginStrategies = function () {
return loginStrategies;
};
Auth.reloadRoutes = function (callback) {
var router = express.Router();
router.hotswapId = 'auth';
Auth.reloadRoutes = function (router, callback) {
loginStrategies.length = 0;
if (plugins.hasListeners('action:auth.overrideLogin')) {
@ -87,12 +82,10 @@ Auth.reloadRoutes = function (callback) {
router.post('/register', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.register);
router.post('/register/complete', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.registerComplete);
// router.get('/register/abort', controllers.authentication.registerAbort);
router.post('/register/abort', controllers.authentication.registerAbort);
router.post('/login', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.login);
router.post('/logout', Auth.middleware.applyCSRF, controllers.authentication.logout);
hotswap.replace('auth', router);
next();
},
], callback);

@ -86,35 +86,22 @@ function groupRoutes(app, middleware, controllers) {
setupPageRoute(app, '/groups/:slug/members', middleware, middlewares, controllers.groups.members);
}
module.exports = function (app, middleware, hotswapIds, callback) {
module.exports = function (app, middleware, callback) {
var routers = [
express.Router(), // plugin router
express.Router(), // main app router
express.Router(), // auth router
];
var router = routers[1];
var pluginRouter = routers[0];
var authRouter = routers[2];
var relativePath = nconf.get('relative_path');
var ensureLoggedIn = require('connect-ensure-login');
var idx;
var x;
if (Array.isArray(hotswapIds) && hotswapIds.length) {
for (x = 0; x < hotswapIds.length; x += 1) {
idx = routers.push(express.Router()) - 1;
routers[idx].hotswapId = hotswapIds[x];
}
}
pluginRouter.render = function () {
app.render.apply(app, arguments);
};
// Set-up for hotswapping (when NodeBB reloads)
pluginRouter.hotswapId = 'plugins';
authRouter.hotswapId = 'auth';
var router = routers[1];
var authRouter = routers[2];
var relativePath = nconf.get('relative_path');
var ensureLoggedIn = require('connect-ensure-login');
app.all(relativePath + '(/+api|/+api/*?)', middleware.prepareAPI);
app.all(relativePath + '(/+api/admin|/+api/admin/*?)', middleware.isAdmin);
@ -143,9 +130,9 @@ module.exports = function (app, middleware, hotswapIds, callback) {
userRoutes(router, middleware, controllers);
groupRoutes(router, middleware, controllers);
for (x = 0; x < routers.length; x += 1) {
app.use(relativePath || '/', routers[x]);
}
routers.forEach((router) => {
app.use(relativePath || '/', router);
});
if (process.env.NODE_ENV === 'development') {
require('./debug')(app, middleware, controllers);
@ -180,8 +167,8 @@ module.exports = function (app, middleware, hotswapIds, callback) {
// Add plugin routes
async.series([
async.apply(plugins.reloadRoutes),
async.apply(authRoutes.reloadRoutes),
async.apply(plugins.reloadRoutes, pluginRouter),
async.apply(authRoutes.reloadRoutes, authRouter),
async.apply(user.addInterstitials),
function (next) {
winston.info('Routes added');

@ -120,10 +120,7 @@ function initializeNodeBB(callback) {
}, next);
},
function (next) {
plugins.fireHook('filter:hotswap.prepare', [], next);
},
function (hotswapIds, next) {
routes(app, middleware, hotswapIds, next);
routes(app, middleware, next);
},
meta.sounds.addUploads,
meta.blacklist.load,

Loading…
Cancel
Save