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:
parent
5e58cc9fc1
commit
15002f6872
@ -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) {
|
|
||||||
node.warn(err);
|
|
||||||
res.send(500);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.callback = function(req,res) {
|
|
||||||
if (node.method == "post") {
|
|
||||||
node.send({req:req,res:res,payload:req.body});
|
|
||||||
} 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(); }
|
|
||||||
|
|
||||||
if (RED.settings.httpNodeCors) {
|
|
||||||
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 node = this;
|
||||||
var routes = RED.httpNode.routes[this.method];
|
|
||||||
for (var i = 0; i<routes.length; i++) {
|
this.errorHandler = function(err,req,res,next) {
|
||||||
if (routes[i].path == this.url) {
|
node.warn(err);
|
||||||
routes.splice(i,1);
|
res.send(500);
|
||||||
//break;
|
};
|
||||||
|
|
||||||
|
this.callback = function(req,res) {
|
||||||
|
if (node.method == "post") {
|
||||||
|
node.send({req:req,res:res,payload:req.body});
|
||||||
|
} 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(); }
|
||||||
|
|
||||||
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);
|
||||||
|
61
red.js
61
red.js
@ -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;
|
||||||
server.listen(settings.uiPort,settings.uiHost,function() {
|
if (settings.httpAdminRoot !== false) {
|
||||||
util.log('[red] Server now running at '+listenPath);
|
listenPath += settings.httpAdminRoot;
|
||||||
});
|
} else if (settings.httpStatic) {
|
||||||
|
listenPath += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
server.listen(settings.uiPort,settings.uiHost,function() {
|
||||||
|
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');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user