Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Less intimidating error page on start, which also checks for api_secret
  • Loading branch information
sulkaharo committed Sep 25, 2020
commit d3c1613132d73f047b3ba8e4f304aa18602c58d9
49 changes: 18 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,25 @@ function create (env, ctx) {
));
});

// Allow static resources to be cached for week
var maxAge = 7 * 24 * 60 * 60 * 1000;

if (process.env.NODE_ENV === 'development') {
maxAge = 1;
console.log('Development environment detected, setting static file cache age to 1 second');
}

var staticFiles = express.static(env.static_files, {
maxAge
});

// serve the static content
app.use(staticFiles);

if (ctx.bootErrors && ctx.bootErrors.length > 0) {
app.get('*', require('./lib/server/booterror')(ctx));
const bootErrorView = require('./lib/server/booterror')(env, ctx);
bootErrorView.setLocals(app.locals);
app.get('*', bootErrorView);
return app;
}

Expand Down Expand Up @@ -256,36 +273,6 @@ function create (env, ctx) {
res.sendFile(__dirname + '/swagger.yaml');
});

/* // FOR DEBUGGING MEMORY LEEAKS
if (env.settings.isEnabled('dumps')) {
var heapdump = require('heapdump');
app.get('/api/v2/dumps/start', function(req, res) {
var path = new Date().toISOString() + '.heapsnapshot';
path = path.replace(/:/g, '-');
console.info('writing dump to', path);
heapdump.writeSnapshot(path);
res.send('wrote dump to ' + path);
});
}
*/

// app.get('/package.json', software);

// Allow static resources to be cached for week
var maxAge = 7 * 24 * 60 * 60 * 1000;

if (process.env.NODE_ENV === 'development') {
maxAge = 1;
console.log('Development environment detected, setting static file cache age to 1 second');
}

var staticFiles = express.static(env.static_files, {
maxAge
});

// serve the static content
app.use(staticFiles);

// API docs

const swaggerUi = require('swagger-ui-express');
Expand Down
30 changes: 23 additions & 7 deletions lib/server/booterror.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
'use strict';

const express = require('express');
const path = require('path');
var _ = require('lodash');

var head = '<!DOCTYPE html><html><head><title>Nightscout - Boot Error</title></head><body><h1>Nightscout - Boot Error</h1><dl>';
var tail = '</dl></body></html>';
function bootError(env, ctx) {

function bootError(ctx) {
const app = new express();
let locals = {};

app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.set("views", path.join(__dirname, "../../views/"));

app.get('*', (req, res, next) => {

if (req.url.includes('images')) return next();

return function pageHandler (req, res) {
var errors = _.map(ctx.bootErrors, function (obj) {

let message;

if (typeof obj.err === 'string' || obj.err instanceof String) {
console.log('Its a string');
message = obj.err;
} else {
message = JSON.stringify(_.pick(obj.err, Object.getOwnPropertyNames(obj.err)));
}
return '<dt>' + obj.desc + '</dt><dd>' + message.replace(/\\n/g, '<br/>') + '</dd>';
}).join(' ');

res.set('Content-Type', 'text/html');
res.send(head + errors + tail);
res.render('error.html', {
errors,
locals
});

});

app.setLocals = function (_locals) {
locals = _locals;
}

return app;
}

module.exports = bootError;
20 changes: 20 additions & 0 deletions lib/server/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ function boot (env, language) {
}
}

function checkSettings (ctx, next) {

ctx.bootErrors = ctx.bootErrors || [];

console.log('Checking settings');

if (!env.storageURI) {
ctx.bootErrors.push({'desc': 'Mandatory setting missing',
err: 'MONGODB_URI setting is missing, cannot connect to database'});
}

if (!env.api_secret) {
ctx.bootErrors.push({'desc': 'Mandatory setting missing',
err: 'API_SECRET setting is missing, cannot enable REST API'});
}

next();
}

function setupStorage (ctx, next) {

if (hasBootErrors(ctx)) {
Expand Down Expand Up @@ -288,6 +307,7 @@ function boot (env, language) {
.acquire(checkNodeVersion)
.acquire(checkEnv)
.acquire(augmentSettings)
.acquire(checkSettings)
.acquire(setupStorage)
.acquire(setupAuthorization)
.acquire(setupInternals)
Expand Down
4 changes: 2 additions & 2 deletions lib/storage/mongo-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function init (env, cb, forceNewConnection) {
}
} else {
if (!env.storageURI) {
throw new Error('MongoDB connection string is missing. Please set MONGO_CONNECTION environment variable');
throw new Error('MongoDB connection string is missing. Please set MONGODB_URI environment variable');
}

console.log('Setting up new connection to MongoDB');
Expand Down Expand Up @@ -63,7 +63,7 @@ function init (env, cb, forceNewConnection) {
setTimeout(connect_with_retry, timeout, i + 1);
if (i == 1) cb(new Error('MongoDB authentication failed! Double check the MONGO_CONNECTION setting in Heroku.'), null);
} else {
cb(new Error('MONGO_CONNECTION ' + env.storageURI + ' seems invalid: ' + err.message));
cb(new Error('MONGODB_URI ' + env.storageURI + ' seems invalid: ' + err.message));
}
});

Expand Down