mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00: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:
		| @@ -43,61 +43,66 @@ function rawBodyParser(req, res, next) { | ||||
|  | ||||
| function HTTPIn(n) { | ||||
|     RED.nodes.createNode(this,n); | ||||
|     this.url = n.url; | ||||
|     this.method = n.method; | ||||
|     if (RED.settings.httpNodeRoot !== false) { | ||||
|  | ||||
|     var node = this; | ||||
|      | ||||
|     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.url = n.url; | ||||
|         this.method = n.method; | ||||
|  | ||||
|     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 node = this; | ||||
|          | ||||
|         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) { | ||||
|             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++) { | ||||
|                 if (routes[i].path == this.url) { | ||||
|                     routes.splice(i,1); | ||||
|                     //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); | ||||
|   | ||||
							
								
								
									
										61
									
								
								red.js
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								red.js
									
									
									
									
									
								
							| @@ -92,13 +92,24 @@ function formatRoot(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 || "/"); | ||||
| settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth; | ||||
| if (settings.httpAdminRoot !== false) { | ||||
|     settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/"); | ||||
|     settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth; | ||||
| } | ||||
|  | ||||
| settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/"); | ||||
| settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth; | ||||
| console.log("1:"+settings.httpNodeRoot); | ||||
| 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.uiHost = settings.uiHost||"0.0.0.0"; | ||||
| @@ -107,22 +118,28 @@ settings.flowFile = flowFile || settings.flowFile; | ||||
|  | ||||
| RED.init(server,settings); | ||||
|  | ||||
| if (settings.httpAdminAuth) { | ||||
| if (settings.httpAdminRoot !== false && settings.httpAdminAuth) { | ||||
|     app.use(settings.httpAdminRoot, | ||||
|         express.basicAuth(function(user, 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, | ||||
|         express.basicAuth(function(user, pass) { | ||||
|             return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass; | ||||
|         }) | ||||
|     ); | ||||
| } | ||||
| app.use(settings.httpAdminRoot,RED.httpAdmin); | ||||
| app.use(settings.httpNodeRoot,RED.httpNode); | ||||
| if (settings.httpAdminRoot !== false) { | ||||
|     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) { | ||||
|     settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth; | ||||
| @@ -137,13 +154,25 @@ if (settings.httpStatic) { | ||||
| } | ||||
|  | ||||
| RED.start().then(function() { | ||||
|     var listenPath = 'http'+(settings.https?'s':'')+'://'+ | ||||
|                      (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+ | ||||
|                      ':'+settings.uiPort+settings.httpAdminRoot; | ||||
|      | ||||
|     server.listen(settings.uiPort,settings.uiHost,function() { | ||||
|         util.log('[red] Server now running at '+listenPath); | ||||
|     }); | ||||
|     if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) { | ||||
|         var listenPath = 'http'+(settings.https?'s':'')+'://'+ | ||||
|                          (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() { | ||||
|             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'); | ||||
|     } | ||||
| }); | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user