Chinthakayala, Sheshashailavas (sc2914) | d156997 | 2017-08-28 05:25:46 -0900 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2013 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | var http = require('http'); |
| 17 | var https = require('https'); |
| 18 | var util = require("util"); |
| 19 | var express = require("express"); |
| 20 | var crypto = require("crypto"); |
| 21 | var nopt = require("nopt"); |
| 22 | var path = require("path"); |
| 23 | var RED = require("./red/red.js"); |
| 24 | |
| 25 | var server; |
| 26 | var app = express(); |
| 27 | |
| 28 | var settingsFile = "./settings"; |
| 29 | var flowFile; |
| 30 | |
| 31 | var knownOpts = { |
| 32 | "settings":[path], |
| 33 | "v": Boolean, |
| 34 | "help": Boolean |
| 35 | }; |
| 36 | var shortHands = { |
| 37 | "s":["--settings"], |
| 38 | "?":["--help"] |
| 39 | }; |
| 40 | nopt.invalidHandler = function(k,v,t) { |
| 41 | // TODO: console.log(k,v,t); |
| 42 | } |
| 43 | |
| 44 | var parsedArgs = nopt(knownOpts,shortHands,process.argv,2) |
| 45 | |
| 46 | if (parsedArgs.help) { |
| 47 | console.log("Node-RED v"+RED.version()); |
| 48 | console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [flows.json]"); |
| 49 | console.log(""); |
| 50 | console.log("Options:"); |
| 51 | console.log(" -s, --settings FILE use specified settings file"); |
| 52 | console.log(" -v enable verbose output"); |
| 53 | console.log(" -?, --help show usage"); |
| 54 | console.log(""); |
| 55 | console.log("Documentation can be found at http://nodered.org"); |
| 56 | process.exit(); |
| 57 | } |
| 58 | if (parsedArgs.argv.remain.length > 0) { |
| 59 | flowFile = parsedArgs.argv.remain[0]; |
| 60 | } |
| 61 | |
| 62 | if (parsedArgs.settings) { |
| 63 | settingsFile = parsedArgs.settings; |
| 64 | } |
| 65 | try { |
| 66 | var settings = require(settingsFile); |
| 67 | } catch(err) { |
| 68 | if (err.code == 'MODULE_NOT_FOUND') { |
| 69 | console.log("Unable to load settings file "+settingsFile); |
| 70 | } else { |
| 71 | console.log(err); |
| 72 | } |
| 73 | process.exit(); |
| 74 | } |
| 75 | |
| 76 | if (parsedArgs.v) { |
| 77 | settings.verbose = true; |
| 78 | } |
| 79 | |
| 80 | if (settings.https) { |
| 81 | server = https.createServer(settings.https,function(req,res){app(req,res);}); |
| 82 | } else { |
| 83 | server = http.createServer(function(req,res){app(req,res);}); |
| 84 | } |
| 85 | server.setMaxListeners(0); |
| 86 | |
| 87 | function formatRoot(root) { |
| 88 | if (root[0] != "/") { |
| 89 | root = "/" + root; |
| 90 | } |
| 91 | if (root.slice(-1) != "/") { |
| 92 | root = root + "/"; |
| 93 | } |
| 94 | return root; |
| 95 | } |
| 96 | |
| 97 | if (settings.httpRoot === false) { |
| 98 | settings.httpAdminRoot = false; |
| 99 | settings.httpNodeRoot = false; |
| 100 | } else { |
| 101 | settings.httpRoot = settings.httpRoot||"/"; |
| 102 | settings.disableEditor = settings.disableEditor||false; |
| 103 | } |
| 104 | |
| 105 | if (settings.httpAdminRoot !== false) { |
| 106 | settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/"); |
| 107 | settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth; |
| 108 | } else { |
| 109 | settings.disableEditor = true; |
| 110 | } |
| 111 | |
| 112 | if (settings.httpNodeRoot !== false) { |
| 113 | settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/"); |
| 114 | settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth; |
| 115 | } |
| 116 | |
| 117 | settings.uiPort = settings.uiPort||1880; |
| 118 | settings.uiHost = settings.uiHost||"0.0.0.0"; |
| 119 | var appDir = path.dirname(require.main.filename); |
| 120 | if (settings.flowFile != null && settings.flowFile.indexOf(appDir) != -1){ |
| 121 | settings.flowFile = flowFile || settings.flowFile; |
| 122 | }else{ |
| 123 | settings.flowFile = flowFile || appDir + "/" + settings.flowFile; |
| 124 | } |
| 125 | |
| 126 | RED.init(server,settings); |
| 127 | |
| 128 | if (settings.httpAdminRoot !== false && settings.httpAdminAuth) { |
| 129 | app.use(settings.httpAdminRoot, |
| 130 | express.basicAuth(function(user, pass) { |
| 131 | return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass; |
| 132 | }) |
| 133 | ); |
| 134 | } |
| 135 | if (settings.httpNodeRoot !== false && settings.httpNodeAuth) { |
| 136 | app.use(settings.httpNodeRoot, |
| 137 | express.basicAuth(function(user, pass) { |
| 138 | return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass; |
| 139 | }) |
| 140 | ); |
| 141 | } |
| 142 | if (settings.httpAdminRoot !== false) { |
| 143 | app.use(settings.httpAdminRoot,RED.httpAdmin); |
| 144 | } |
| 145 | if (settings.httpNodeRoot !== false) { |
| 146 | app.use(settings.httpNodeRoot,RED.httpNode); |
| 147 | } |
| 148 | |
| 149 | if (settings.httpStatic) { |
| 150 | settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth; |
| 151 | if (settings.httpStaticAuth) { |
| 152 | app.use("/", |
| 153 | express.basicAuth(function(user, pass) { |
| 154 | return user === settings.httpStaticAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpStaticAuth.pass; |
| 155 | }) |
| 156 | ); |
| 157 | } |
| 158 | app.use("/",express.static(settings.httpStatic)); |
| 159 | } |
| 160 | |
| 161 | function getListenPath() { |
| 162 | var listenPath = 'http'+(settings.https?'s':'')+'://'+ |
| 163 | (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+ |
| 164 | ':'+settings.uiPort; |
| 165 | if (settings.httpAdminRoot !== false) { |
| 166 | listenPath += settings.httpAdminRoot; |
| 167 | } else if (settings.httpStatic) { |
| 168 | listenPath += "/"; |
| 169 | } |
| 170 | return listenPath; |
| 171 | } |
| 172 | |
| 173 | RED.start().then(function() { |
| 174 | if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) { |
| 175 | server.on('error', function(err) { |
| 176 | if (err.errno === "EADDRINUSE") { |
| 177 | util.log('[red] Unable to listen on '+getListenPath()); |
| 178 | util.log('[red] Error: port in use'); |
| 179 | } else { |
| 180 | util.log('[red] Uncaught Exception:'); |
| 181 | if (err.stack) { |
| 182 | util.log(err.stack); |
| 183 | } else { |
| 184 | util.log(err); |
| 185 | } |
| 186 | } |
| 187 | process.exit(1); |
| 188 | }); |
| 189 | server.listen(settings.uiPort,settings.uiHost,function() { |
| 190 | if (settings.httpAdminRoot === false) { |
| 191 | util.log('[red] Admin UI disabled'); |
| 192 | } |
| 193 | process.title = 'node-red'; |
| 194 | util.log('[red] Server now running at '+getListenPath()); |
| 195 | }); |
| 196 | } else { |
| 197 | util.log('[red] Running in headless mode'); |
| 198 | } |
| 199 | }).otherwise(function(err) { |
| 200 | util.log("[red] Failed to start server:"); |
| 201 | if (err.stack) { |
| 202 | util.log(err.stack); |
| 203 | } else { |
| 204 | util.log(err); |
| 205 | } |
| 206 | }); |
| 207 | |
| 208 | |
| 209 | process.on('uncaughtException',function(err) { |
| 210 | util.log('[red] Uncaught Exception:'); |
| 211 | if (err.stack) { |
| 212 | util.log(err.stack); |
| 213 | } else { |
| 214 | util.log(err); |
| 215 | } |
| 216 | process.exit(1); |
| 217 | }); |
| 218 | |
| 219 | process.on('SIGINT', function () { |
| 220 | RED.stop(); |
| 221 | // TODO: need to allow nodes to close asynchronously before terminating the |
| 222 | // process - ie, promises |
| 223 | process.exit(); |
| 224 | }); |