moved maintenace mode middleware

v1.18.x
barisusakli 10 years ago
parent 6d930d0d1a
commit 93e6916fe6

@ -0,0 +1,77 @@
'use strict';
var meta = require('../meta');
var user = require('../user');
var translator = require('../../public/src/modules/translator');
module.exports = function(middleware) {
middleware.maintenanceMode = function(req, res, next) {
console.log(req.path, meta.config.maintenanceMode)
if (parseInt(meta.config.maintenanceMode, 10) !== 1) {
return next();
}
var allowedRoutes = [
'/login',
'/stylesheet.css',
'/nodebb.min.js',
'/vendor/fontawesome/fonts/fontawesome-webfont.woff',
'/src/(modules|client)/[\\w/]+.js',
'/templates/[\\w/]+.tpl',
'/api/login',
'/api/?',
'/language/.+',
'/uploads/system/site-logo.png'
],
render = function() {
res.status(503);
if (!isApiRoute.test(req.url)) {
middleware.buildHeader(req, res, function() {
res.render('maintenance', {
site_title: meta.config.title || 'NodeBB',
message: meta.config.maintenanceModeMessage
});
});
} else {
translator.translate('[[pages:maintenance.text, ' + meta.config.title + ']]', meta.config.defaultLang || 'en_GB', function(translated) {
res.json({
error: translated
});
});
}
},
isAllowed = function(url) {
for(var x=0,numAllowed=allowedRoutes.length,route;x<numAllowed;x++) {
route = new RegExp(allowedRoutes[x]);
if (route.test(url)) {
return true;
}
}
return false;
},
isApiRoute = /^\/api/;
if (isAllowed(req.url)) {
return next();
}
if (!req.user) {
return render();
}
user.isAdministrator(req.user.uid, function(err, isAdmin) {
if (err) {
return next(err);
}
if (!isAdmin) {
render();
} else {
next();
}
});
};
};

@ -405,71 +405,7 @@ middleware.addExpiresHeaders = function(req, res, next) {
next(); next();
}; };
middleware.maintenanceMode = function(req, res, next) {
if (parseInt(meta.config.maintenanceMode, 10) !== 1) {
return next();
}
var allowedRoutes = [
'/login',
'/stylesheet.css',
'/nodebb.min.js',
'/vendor/fontawesome/fonts/fontawesome-webfont.woff',
'/src/(modules|client)/[\\w/]+.js',
'/templates/[\\w/]+.tpl',
'/api/login',
'/api/?',
'/language/.+',
'/uploads/system/site-logo.png'
],
render = function() {
res.status(503);
if (!isApiRoute.test(req.url)) {
middleware.buildHeader(req, res, function() {
res.render('maintenance', {
site_title: meta.config.title || 'NodeBB',
message: meta.config.maintenanceModeMessage
});
});
} else {
translator.translate('[[pages:maintenance.text, ' + meta.config.title + ']]', meta.config.defaultLang || 'en_GB', function(translated) {
res.json({
error: translated
});
});
}
},
isAllowed = function(url) {
for(var x=0,numAllowed=allowedRoutes.length,route;x<numAllowed;x++) {
route = new RegExp(allowedRoutes[x]);
if (route.test(url)) {
return true;
}
}
return false;
},
isApiRoute = /^\/api/;
if (isAllowed(req.url)) {
return next();
}
if (!req.user) {
return render();
}
user.isAdministrator(req.user.uid, function(err, isAdmin) {
if (err) {
return next(err);
}
if (!isAdmin) {
render();
} else {
next();
}
});
};
middleware.privateTagListing = function(req, res, next) { middleware.privateTagListing = function(req, res, next) {
if (!req.user && parseInt(meta.config.privateTagListing, 10) === 1) { if (!req.user && parseInt(meta.config.privateTagListing, 10) === 1) {
@ -539,5 +475,7 @@ module.exports = function(webserver) {
app = webserver; app = webserver;
middleware.admin = require('./admin')(webserver); middleware.admin = require('./admin')(webserver);
require('./maintenance')(middleware);
return middleware; return middleware;
}; };

Loading…
Cancel
Save