1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Warn if editor routes are accessed but runtime not started

Closes #816
This commit is contained in:
Nick O'Leary 2016-03-12 00:03:50 +00:00
parent 7281e4deb6
commit e941c22f6c
3 changed files with 65 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2014, 2015 IBM Corp. * Copyright 2014, 2016 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,6 +40,7 @@ var log;
var adminApp; var adminApp;
var nodeApp; var nodeApp;
var server; var server;
var runtime;
var errorHandler = function(err,req,res,next) { var errorHandler = function(err,req,res,next) {
if (err.message === "request entity too large") { if (err.message === "request entity too large") {
@ -51,8 +52,18 @@ var errorHandler = function(err,req,res,next) {
res.status(400).json({error:"unexpected_error", message:err.toString()}); res.status(400).json({error:"unexpected_error", message:err.toString()});
}; };
function init(_server,runtime) { var ensureRuntimeStarted = function(req,res,next) {
if (!runtime.isStarted()) {
log.error("Node-RED runtime not started");
res.status(503).send("Not started");
} else {
next();
}
}
function init(_server,_runtime) {
server = _server; server = _server;
runtime = _runtime;
var settings = runtime.settings; var settings = runtime.settings;
i18n = runtime.i18n; i18n = runtime.i18n;
log = runtime.log; log = runtime.log;
@ -75,7 +86,7 @@ function init(_server,runtime) {
if (!settings.disableEditor) { if (!settings.disableEditor) {
ui.init(runtime); ui.init(runtime);
var editorApp = express(); var editorApp = express();
editorApp.get("/",ui.ensureSlash,ui.editor); editorApp.get("/",ensureRuntimeStarted,ui.ensureSlash,ui.editor);
editorApp.get("/icons/:icon",ui.icon); editorApp.get("/icons/:icon",ui.icon);
theme.init(runtime); theme.init(runtime);
if (settings.editorTheme) { if (settings.editorTheme) {

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013, 2015 IBM Corp. * Copyright 2013, 2016 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,8 @@ var os = require("os");
var runtimeMetricInterval = null; var runtimeMetricInterval = null;
var started = false;
var stubbedExpressApp = { var stubbedExpressApp = {
get: function() {}, get: function() {},
post: function() {}, post: function() {},
@ -142,6 +144,7 @@ function start() {
log.info(log._("runtime.paths.settings",{path:settings.settingsFile})); log.info(log._("runtime.paths.settings",{path:settings.settingsFile}));
} }
redNodes.loadFlows().then(redNodes.startFlows); redNodes.loadFlows().then(redNodes.startFlows);
started = true;
}).otherwise(function(err) { }).otherwise(function(err) {
console.log(err); console.log(err);
}); });
@ -173,6 +176,7 @@ function stop() {
clearInterval(runtimeMetricInterval); clearInterval(runtimeMetricInterval);
runtimeMetricInterval = null; runtimeMetricInterval = null;
} }
started = false;
return redNodes.stopFlows(); return redNodes.stopFlows();
} }
@ -190,5 +194,8 @@ var runtime = module.exports = {
events: events, events: events,
nodes: redNodes, nodes: redNodes,
util: require("./util"), util: require("./util"),
get adminApi() { return adminApi } get adminApi() { return adminApi },
isStarted: function() {
return started;
}
} }

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2014 IBM Corp. * Copyright 2014, 2016 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -87,6 +87,45 @@ describe("api index", function() {
}); });
}); });
describe("editor warns if runtime not started", function() {
var mockList = [
'nodes','flows','library','info','theme','locales','credentials'
]
before(function() {
mockList.forEach(function(m) {
sinon.stub(require("../../../red/api/"+m),"init",function(){});
});
});
after(function() {
mockList.forEach(function(m) {
require("../../../red/api/"+m).init.restore();
})
});
it('serves the editor', function(done) {
var errorLog = sinon.spy();
api.init({},{
log:{audit:function(){},error:errorLog},
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false},
events:{on:function(){},removeListener:function(){}},
isStarted: function() { return false; } // <-----
});
app = api.adminApp;
request(app)
.get("/")
.expect(503)
.end(function(err,res) {
if (err) {
return done(err);
}
res.text.should.eql("Not started");
errorLog.calledOnce.should.be.true;
done();
});
});
});
describe("enables editor", function() { describe("enables editor", function() {
var mockList = [ var mockList = [
@ -107,7 +146,8 @@ describe("api index", function() {
api.init({},{ api.init({},{
log:{audit:function(){}}, log:{audit:function(){}},
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false}, settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false},
events:{on:function(){},removeListener:function(){}} events:{on:function(){},removeListener:function(){}},
isStarted: function() { return true; }
}); });
app = api.adminApp; app = api.adminApp;
}); });