blob: 460b2efd532d1ba066a41d5208cba1c42f8d123d [file] [log] [blame]
Chinthakayala, Sheshashailavas (sc2914)d1569972017-08-28 05:25:46 -09001/**
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 **/
16var http = require('http');
17var https = require('https');
18var util = require("util");
19var express = require("express");
20var crypto = require("crypto");
21var nopt = require("nopt");
22var path = require("path");
23var RED = require("./red/red.js");
Chinthakayala, Sheshashailavas (sc2914)347321e2019-08-02 16:22:16 +000024var fs = require('fs')
Chinthakayala, Sheshashailavas (sc2914)d1569972017-08-28 05:25:46 -090025var server;
26var app = express();
27
28var settingsFile = "./settings";
29var flowFile;
30
31var knownOpts = {
32 "settings":[path],
33 "v": Boolean,
34 "help": Boolean
35};
36var shortHands = {
37 "s":["--settings"],
38 "?":["--help"]
39};
40nopt.invalidHandler = function(k,v,t) {
41 // TODO: console.log(k,v,t);
42}
43
44var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
45
46if (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}
58if (parsedArgs.argv.remain.length > 0) {
59 flowFile = parsedArgs.argv.remain[0];
60}
61
62if (parsedArgs.settings) {
63 settingsFile = parsedArgs.settings;
64}
65try {
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
76if (parsedArgs.v) {
77 settings.verbose = true;
78}
79
Chinthakayala, Sheshashailavas (sc2914)347321e2019-08-02 16:22:16 +000080if (settings.enableHttps) {
81 //server = https.createServer(settings.https,function(req,res){app(req,res);});
82 server = https.createServer({
83 key: fs.readFileSync('certs/node-key.pem'),
84 cert: fs.readFileSync('certs/node-cert.pem'),
85 requireHttps : true},function(req,res){app(req,res);});
Chinthakayala, Sheshashailavas (sc2914)d1569972017-08-28 05:25:46 -090086} else {
87 server = http.createServer(function(req,res){app(req,res);});
88}
89server.setMaxListeners(0);
90
91function formatRoot(root) {
92 if (root[0] != "/") {
93 root = "/" + root;
94 }
95 if (root.slice(-1) != "/") {
96 root = root + "/";
97 }
98 return root;
99}
100
101if (settings.httpRoot === false) {
102 settings.httpAdminRoot = false;
103 settings.httpNodeRoot = false;
104} else {
105 settings.httpRoot = settings.httpRoot||"/";
106 settings.disableEditor = settings.disableEditor||false;
107}
108
109if (settings.httpAdminRoot !== false) {
110 settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
111 settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
112} else {
113 settings.disableEditor = true;
114}
115
116if (settings.httpNodeRoot !== false) {
117 settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
118 settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
119}
120
121settings.uiPort = settings.uiPort||1880;
122settings.uiHost = settings.uiHost||"0.0.0.0";
123var appDir = path.dirname(require.main.filename);
124if (settings.flowFile != null && settings.flowFile.indexOf(appDir) != -1){
125 settings.flowFile = flowFile || settings.flowFile;
126}else{
127 settings.flowFile = flowFile || appDir + "/" + settings.flowFile;
128}
129
130RED.init(server,settings);
131
132if (settings.httpAdminRoot !== false && settings.httpAdminAuth) {
133 app.use(settings.httpAdminRoot,
134 express.basicAuth(function(user, pass) {
135 return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass;
136 })
137 );
138}
139if (settings.httpNodeRoot !== false && settings.httpNodeAuth) {
140 app.use(settings.httpNodeRoot,
141 express.basicAuth(function(user, pass) {
142 return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass;
143 })
144 );
145}
146if (settings.httpAdminRoot !== false) {
147 app.use(settings.httpAdminRoot,RED.httpAdmin);
148}
149if (settings.httpNodeRoot !== false) {
150 app.use(settings.httpNodeRoot,RED.httpNode);
151}
152
153if (settings.httpStatic) {
154 settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
155 if (settings.httpStaticAuth) {
156 app.use("/",
157 express.basicAuth(function(user, pass) {
158 return user === settings.httpStaticAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpStaticAuth.pass;
159 })
160 );
161 }
162 app.use("/",express.static(settings.httpStatic));
163}
164
165function getListenPath() {
Chinthakayala, Sheshashailavas (sc2914)347321e2019-08-02 16:22:16 +0000166 var listenPath = 'http'+(settings.enableHttps?'s':'')+'://'+
Chinthakayala, Sheshashailavas (sc2914)d1569972017-08-28 05:25:46 -0900167 (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+
168 ':'+settings.uiPort;
169 if (settings.httpAdminRoot !== false) {
170 listenPath += settings.httpAdminRoot;
171 } else if (settings.httpStatic) {
172 listenPath += "/";
173 }
174 return listenPath;
175}
176
177RED.start().then(function() {
178 if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) {
179 server.on('error', function(err) {
180 if (err.errno === "EADDRINUSE") {
181 util.log('[red] Unable to listen on '+getListenPath());
182 util.log('[red] Error: port in use');
183 } else {
184 util.log('[red] Uncaught Exception:');
185 if (err.stack) {
186 util.log(err.stack);
187 } else {
188 util.log(err);
189 }
190 }
191 process.exit(1);
192 });
193 server.listen(settings.uiPort,settings.uiHost,function() {
194 if (settings.httpAdminRoot === false) {
195 util.log('[red] Admin UI disabled');
196 }
197 process.title = 'node-red';
198 util.log('[red] Server now running at '+getListenPath());
199 });
200 } else {
201 util.log('[red] Running in headless mode');
202 }
203}).otherwise(function(err) {
204 util.log("[red] Failed to start server:");
205 if (err.stack) {
206 util.log(err.stack);
207 } else {
208 util.log(err);
209 }
210});
211
212
213process.on('uncaughtException',function(err) {
214 util.log('[red] Uncaught Exception:');
215 if (err.stack) {
216 util.log(err.stack);
217 } else {
218 util.log(err);
219 }
220 process.exit(1);
221});
222
223process.on('SIGINT', function () {
224 RED.stop();
225 // TODO: need to allow nodes to close asynchronously before terminating the
226 // process - ie, promises
227 process.exit();
228});