[API v3] Maintenance Mode (#7367)
* WIP(maintenance): maintenance * WIP(maintenance): working locale features * fix(maintenance): don't translate info page target * WIP(maintenance): start adding info page * fix(maintenance): linting * feat: Add container to maintenance info page * fix(maintenance): add config.json edits Also DRY variables for main vs info pages * fix(maintenance): linting * refactor(maintenance): further slim down variables * refactor: Remove unnecessary variables * fix: Correct string interpolation in maintenace view * feat: Dynamically add time to maintenance pages * maintenance mode: do not connect to mongodb * fix(maintenance): clean up timezones etc. * fix(maintenance): remove unneeded sprite
This commit is contained in:
committed by
Matteo Pagliazzi
parent
f4ca97ffc3
commit
237bc062ea
@@ -5,19 +5,24 @@ import mongoose from 'mongoose';
|
||||
import Bluebird from 'bluebird';
|
||||
|
||||
const IS_PROD = nconf.get('IS_PROD');
|
||||
const MAINTENANCE_MODE = nconf.get('MAINTENANCE_MODE');
|
||||
|
||||
// Use Q promises instead of mpromise in mongoose
|
||||
mongoose.Promise = Bluebird;
|
||||
|
||||
let mongooseOptions = !IS_PROD ? {} : {
|
||||
replset: { socketOptions: { keepAlive: 120, connectTimeoutMS: 30000 } },
|
||||
server: { socketOptions: { keepAlive: 120, connectTimeoutMS: 30000 } },
|
||||
};
|
||||
// Do not connect to MongoDB when in maintenance mode
|
||||
if (MAINTENANCE_MODE !== 'true') {
|
||||
let mongooseOptions = !IS_PROD ? {} : {
|
||||
replset: { socketOptions: { keepAlive: 120, connectTimeoutMS: 30000 } },
|
||||
server: { socketOptions: { keepAlive: 120, connectTimeoutMS: 30000 } },
|
||||
};
|
||||
|
||||
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
|
||||
let db = mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
|
||||
if (err) throw err;
|
||||
logger.info('Connected with Mongoose.');
|
||||
});
|
||||
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
|
||||
|
||||
autoinc.init(db);
|
||||
let db = mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
|
||||
if (err) throw err;
|
||||
logger.info('Connected with Mongoose.');
|
||||
});
|
||||
|
||||
autoinc.init(db);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import favicon from 'serve-favicon';
|
||||
import methodOverride from 'method-override';
|
||||
import passport from 'passport';
|
||||
import path from 'path';
|
||||
import maintenanceMode from './maintenanceMode';
|
||||
import {
|
||||
forceSSL,
|
||||
forceHabitica,
|
||||
@@ -48,6 +49,8 @@ module.exports = function attachMiddlewares (app, server) {
|
||||
app.use(compression());
|
||||
app.use(favicon(`${PUBLIC_DIR}/favicon.ico`));
|
||||
|
||||
app.use(maintenanceMode);
|
||||
|
||||
app.use(cors);
|
||||
app.use(forceSSL);
|
||||
app.use(forceHabitica);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { getUserLanguage } from './language';
|
||||
import nconf from 'nconf';
|
||||
|
||||
const MAINTENANCE_MODE = nconf.get('MAINTENANCE_MODE');
|
||||
|
||||
module.exports = function maintenanceMode (req, res, next) {
|
||||
if (MAINTENANCE_MODE !== 'true') return next();
|
||||
|
||||
getUserLanguage(req, res, (err) => {
|
||||
if (err) return next(err);
|
||||
|
||||
let pageVariables = {
|
||||
maintenanceStart: nconf.get('MAINTENANCE_START'),
|
||||
maintenanceEnd: nconf.get('MAINTENANCE_END'),
|
||||
translation: res.t,
|
||||
};
|
||||
|
||||
if (req.headers && req.headers.accept && req.headers.accept.indexOf('text/html') !== -1) {
|
||||
if (req.path === '/views/static/maintenance-info') {
|
||||
return res.status(503).render('../../../views/static/maintenance-info', pageVariables);
|
||||
} else {
|
||||
return res.status(503).render('../../../views/static/maintenance', pageVariables);
|
||||
}
|
||||
} else {
|
||||
return res.status(503).send({
|
||||
error: 'Maintenance',
|
||||
message: 'Server offline for maintenance.',
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user