Update versions in relesae notes
[ccsdk/distribution.git] / dgbuilder / red.js
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 var fs = require('fs')
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.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);});
86 } else {
87     server = http.createServer(function(req,res){app(req,res);});
88 }
89 server.setMaxListeners(0);
90
91 function formatRoot(root) {
92     if (root[0] != "/") {
93         root = "/" + root;
94     }
95     if (root.slice(-1) != "/") {
96         root = root + "/";
97     }
98     return root;
99 }
100
101 if (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
109 if (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
116 if (settings.httpNodeRoot !== false) {
117     settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
118     settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
119 }
120
121 settings.uiPort = settings.uiPort||1880;
122 settings.uiHost = settings.uiHost||"0.0.0.0";
123 var appDir = path.dirname(require.main.filename);
124 if (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
130 RED.init(server,settings);
131
132 if (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 }
139 if (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 }
146 if (settings.httpAdminRoot !== false) {
147     app.use(settings.httpAdminRoot,RED.httpAdmin);
148 }
149 if (settings.httpNodeRoot !== false) {
150     app.use(settings.httpNodeRoot,RED.httpNode);
151 }
152
153 if (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
165 function getListenPath() {
166     var listenPath = 'http'+(settings.enableHttps?'s':'')+'://'+
167                     (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
177 RED.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
213 process.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
223 process.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 });

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use