forked from binnes/Node-RED-Docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (89 loc) · 3.1 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Requires statements and code for non-production mode usage
// if (!process.env.NODE_ENV || !process.env.NODE_ENV === 'production') {
// require('appmetrics-dash').attach();
//}
var http = require('http');
var https = require('https');
var util = require("util");
var express = require("express");
var crypto = require("crypto");
try { bcrypt = require('bcrypt'); }
catch(e) { bcrypt = require('bcryptjs'); }
var nopt = require("nopt");
var path = require("path");
var fs = require("fs-extra");
var RED = require("node-red");
var health = require('@cloudnative/health-connect');
// Create an Express app
var server;
var app = express();
process.env.NODE_RED_HOME = __dirname;
// Add a simple route for static content served from 'public'
app.use("/",express.static("public"));
var healthcheck = new health.HealthChecker();
app.use('/live', health.LivenessEndpoint(healthcheck));
app.use('/ready', health.ReadinessEndpoint(healthcheck));
app.use('/health', health.HealthEndpoint(healthcheck));
// Create the settings object from file
var settings = require("./settings.js");
var fs = require("fs");
if (process.env.NODE_RED_TLS_CERT && process.env.NODE_RED_TLS_KEY) {
settings.https = {
"key" : fs.readFileSync(process.env.NODE_RED_TLS_KEY),
"cert" : fs.readFileSync(process.env.NODE_RED_TLS_CERT)
};
if (process.env.NODE_RED_TLS_CA) {
settings.https.ca = fs.readFileSync(process.env.NODE_RED_TLS_CA);
}
if (process.env.NODE_RED_TLS_PASS_FILE) {
settings.https.passphrase = fs.readFileSync(process.env.NODE_RED_TLS_PASS_FILE);
} else if (process.env.NODE_RED_TLS_PASS_ENV) {
settings.https.passphrase = process.env.NODE_RED_TLS_PASS_ENV;
}
settings.requireHttps = true;
}
if (settings.https) {
server = https.createServer(settings.https,function(req,res) {app(req,res);});
} else {
server = http.createServer(function(req,res) {app(req,res);});
}
server.setMaxListeners(0);
function formatRoot(root) {
if (root[0] != "/") {
root = "/" + root;
}
if (root.slice(-1) != "/") {
root = root + "/";
}
return root;
}
if (settings.httpRoot === false) {
settings.httpAdminRoot = false;
settings.httpNodeRoot = false;
} else {
settings.httpRoot = settings.httpRoot||"/";
settings.disableEditor = settings.disableEditor||false;
}
if (settings.httpAdminRoot !== false) {
settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
} else {
settings.disableEditor = true;
}
if (settings.httpNodeRoot !== false) {
settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
}
if (settings.uiPort === undefined){
settings.uiPort = 1880;
}
settings.uiHost = settings.uiHost||"0.0.0.0";
// Initialise the runtime with a server and settings
RED.init(server,settings);
// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);
// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);
server.listen(settings.uiPort);
// Start the runtime
RED.start();