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

Add headless mode

closes #2

httpAdminRoot / httpNodeRoot can be set to false to disable their respective bits. If both are set to false, (or httpRoot is set to false), and httpStatic is not defined, then it will not start the http server.
This commit is contained in:
Nick O'Leary 2014-03-08 22:35:35 +00:00
parent 5e58cc9fc1
commit 15002f6872
2 changed files with 94 additions and 60 deletions

View File

@ -43,61 +43,66 @@ function rawBodyParser(req, res, next) {
function HTTPIn(n) { function HTTPIn(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.url = n.url; if (RED.settings.httpNodeRoot !== false) {
this.method = n.method;
var node = this; this.url = n.url;
this.method = n.method;
this.errorHandler = function(err,req,res,next) { var node = this;
node.warn(err);
res.send(500);
};
this.callback = function(req,res) { this.errorHandler = function(err,req,res,next) {
if (node.method == "post") { node.warn(err);
node.send({req:req,res:res,payload:req.body}); res.send(500);
} else if (node.method == "get") { };
node.send({req:req,res:res,payload:req.query});
} else {
node.send({req:req,res:res});
}
}
var corsHandler = function(req,res,next) { next(); } this.callback = function(req,res) {
if (node.method == "post") {
if (RED.settings.httpNodeCors) { node.send({req:req,res:res,payload:req.body});
corsHandler = cors(RED.settings.httpNodeCors); } else if (node.method == "get") {
RED.httpNode.options(this.url,corsHandler); node.send({req:req,res:res,payload:req.query});
} } else {
node.send({req:req,res:res});
if (this.method == "get") {
RED.httpNode.get(this.url,corsHandler,this.callback,this.errorHandler);
} else if (this.method == "post") {
RED.httpNode.post(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "put") {
RED.httpNode.put(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "delete") {
RED.httpNode.delete(this.url,corsHandler,this.callback,errorHandler);
}
this.on("close",function() {
var routes = RED.httpNode.routes[this.method];
for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) {
routes.splice(i,1);
//break;
} }
} }
var corsHandler = function(req,res,next) { next(); }
if (RED.settings.httpNodeCors) { if (RED.settings.httpNodeCors) {
var routes = RED.httpNode.routes['options']; corsHandler = cors(RED.settings.httpNodeCors);
RED.httpNode.options(this.url,corsHandler);
}
if (this.method == "get") {
RED.httpNode.get(this.url,corsHandler,this.callback,this.errorHandler);
} else if (this.method == "post") {
RED.httpNode.post(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "put") {
RED.httpNode.put(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "delete") {
RED.httpNode.delete(this.url,corsHandler,this.callback,errorHandler);
}
this.on("close",function() {
var routes = RED.httpNode.routes[this.method];
for (var i = 0; i<routes.length; i++) { for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) { if (routes[i].path == this.url) {
routes.splice(i,1); routes.splice(i,1);
//break; //break;
} }
} }
} if (RED.settings.httpNodeCors) {
}); var routes = RED.httpNode.routes['options'];
for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) {
routes.splice(i,1);
//break;
}
}
}
});
} else {
this.warn("Cannot create http-in node when httpNodeRoot set to false");
}
} }
RED.nodes.registerType("http in",HTTPIn); RED.nodes.registerType("http in",HTTPIn);

59
red.js
View File

@ -92,13 +92,24 @@ function formatRoot(root) {
return root; return root;
} }
settings.httpRoot = settings.httpRoot||"/"; if (settings.httpRoot === false) {
settings.httpAdminRoot = false;
settings.httpNodeRoot = false;
} else {
settings.httpRoot = settings.httpRoot||"/";
}
settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/"); if (settings.httpAdminRoot !== false) {
settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth; settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
}
settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/"); console.log("1:"+settings.httpNodeRoot);
settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth; if (settings.httpNodeRoot !== false) {
settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
console.log("2:"+settings.httpNodeRoot);
settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
}
settings.uiPort = settings.uiPort||1880; settings.uiPort = settings.uiPort||1880;
settings.uiHost = settings.uiHost||"0.0.0.0"; settings.uiHost = settings.uiHost||"0.0.0.0";
@ -107,22 +118,28 @@ settings.flowFile = flowFile || settings.flowFile;
RED.init(server,settings); RED.init(server,settings);
if (settings.httpAdminAuth) { if (settings.httpAdminRoot !== false && settings.httpAdminAuth) {
app.use(settings.httpAdminRoot, app.use(settings.httpAdminRoot,
express.basicAuth(function(user, pass) { express.basicAuth(function(user, pass) {
return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass; return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass;
}) })
); );
} }
if (settings.httpNodeAuth) { if (settings.httpNodeRoot !== false && settings.httpNodeAuth) {
app.use(settings.httpNodeRoot, app.use(settings.httpNodeRoot,
express.basicAuth(function(user, pass) { express.basicAuth(function(user, pass) {
return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass; return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass;
}) })
); );
} }
app.use(settings.httpAdminRoot,RED.httpAdmin); if (settings.httpAdminRoot !== false) {
app.use(settings.httpNodeRoot,RED.httpNode); console.log("Attaching admin root");
app.use(settings.httpAdminRoot,RED.httpAdmin);
}
if (settings.httpNodeRoot !== false) {
console.log(settings.httpNodeRoot);
app.use(settings.httpNodeRoot,RED.httpNode);
}
if (settings.httpStatic) { if (settings.httpStatic) {
settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth; settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
@ -137,13 +154,25 @@ if (settings.httpStatic) {
} }
RED.start().then(function() { RED.start().then(function() {
var listenPath = 'http'+(settings.https?'s':'')+'://'+ if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) {
(settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+ var listenPath = 'http'+(settings.https?'s':'')+'://'+
':'+settings.uiPort+settings.httpAdminRoot; (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+
':'+settings.uiPort;
if (settings.httpAdminRoot !== false) {
listenPath += settings.httpAdminRoot;
} else if (settings.httpStatic) {
listenPath += "/";
}
server.listen(settings.uiPort,settings.uiHost,function() { server.listen(settings.uiPort,settings.uiHost,function() {
util.log('[red] Server now running at '+listenPath); if (settings.httpAdminRoot === false) {
}); util.log('[red] Admin UI disabled');
}
util.log('[red] Server now running at '+listenPath);
});
} else {
util.log('[red] Running in headless mode');
}
}); });