-
Notifications
You must be signed in to change notification settings - Fork 2
/
node-red.js
215 lines (188 loc) · 5.39 KB
/
node-red.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const http = require('http');
const express = require('express');
const RED = require('node-red');
const { existsSync } = require('fs');
const { join, dirname } = require('path');
const { createLogger, format, transports } = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const { combine, timestamp, printf } = format;
const nrRuntimeSettings = require('./settings');
/* ------ Don't mess with anything below - unless you're a nerd ;-) ------ */
const pathPrefix = process.platform === 'win32' ? 'c:/' : '/';
const logFormat = printf(({ level, message, label, timestamp }) => {
switch (typeof message) {
case 'object':
message = JSON.stringify(message);
break;
}
return `[${timestamp}] ${level}\t${label?.padEnd(23, ' ')}: ${message}`;
});
const consoleLogger = createLogger({
format: combine(
format.colorize(),
timestamp({ format: 'YYYY-MM-DD hh:mm:ss' }),
logFormat
),
transports: [new transports.Console()]
});
let flowLogger;
if (process.pkg !== undefined) {
const transport = new DailyRotateFile({
filename: join(dirname(process.argv0), 'sfe-%DATE%.log'),
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20m',
maxFiles: '7d'
});
flowLogger = createLogger({
format: combine(timestamp({ format: 'YYYY-MM-DD hh:mm:ss' }), logFormat),
transports: [transport]
});
}
// In develop mode?
const develop = process.argv[2] === '--develop';
// The embedded path of the userDir (don't mess)
const ns = '{SFE_PROJECT_DIR}';
const userDir = 'NRUserDir';
const userDirPath = `${pathPrefix}snapshot/${ns}/build/${userDir}`;
// File exists?
const checkFileExists = (path) => {
return existsSync(path);
};
const isEmbedded = checkFileExists(userDirPath);
// Get userDir
const getUserDir = () => {
if (develop) {
return join(__dirname, userDir);
}
if (process.pkg !== undefined) {
if (isEmbedded) {
return userDirPath;
} else {
return join(dirname(process.argv0), userDir);
}
}
};
// Node-RED log
const nrLog = (level, label, message) => {
if (process.pkg !== undefined && flowLogger !== undefined) {
flowLogger.log({ level, label: label, message });
}
consoleLogger.log({ level, label: `FLOW:${label}`, message });
};
// Main
const run = async () => {
console.clear();
consoleLogger.info({ label: 'Node Version', message: process.versions.node });
const app = express();
const server = http.createServer(app);
delete nrRuntimeSettings.userDir;
delete nrRuntimeSettings.logging;
delete nrRuntimeSettings.editorTheme;
delete nrRuntimeSettings.readOnly;
delete nrRuntimeSettings.contextStorage.file.config?.dir;
const nrSettings = {
userDir: getUserDir(),
logging: {
console: {
level: 'off',
metrics: false,
audit: false
}
},
editorTheme: {
header: {
title: `Node-RED SFE ${develop ? '[Design Time]' : '[Run Time]'}`
},
page: {
title: `Node-RED SFE ${develop ? '[Design Time]' : '[Run Time]'}`
},
projects: {
enabled: false
},
tours: false
},
...nrRuntimeSettings
};
if (!nrSettings.functionGlobalContext) {
nrSettings.functionGlobalContext = {};
}
nrSettings.functionGlobalContext.SFELOG = nrLog;
if (develop) {
nrSettings.disableEditor = false;
}
if (isEmbedded) {
nrSettings.editorTheme.header.image = `${pathPrefix}snapshot/${ns}/build/resources/node-red.png`;
nrSettings.editorTheme.page.css = `${pathPrefix}snapshot/${ns}/build/resources/sfe.css`;
nrSettings.readOnly = true;
nrSettings.editorTheme.login = {
image: `${pathPrefix}snapshot/${ns}/build/resources/node-red-256-embedded.png`
};
/* Re-configure file context store */
if (nrSettings.contextStorage.file.config === undefined) {
nrSettings.contextStorage.file.config = {};
}
nrSettings.contextStorage.file.config.dir = join(
dirname(process.argv0),
'./'
);
} else {
nrSettings.editorTheme.login = {
image: `${pathPrefix}snapshot/${ns}/build/resources/node-red-256-external.png`
};
}
// Initialize Node-RED with the given settings
RED.init(server, nrSettings);
app.use(nrSettings.httpAdminRoot, RED.httpAdmin);
app.use(nrSettings.httpNodeRoot, RED.httpNode);
consoleLogger.info({
label: 'Node-RED Version',
message: RED.settings.version
});
consoleLogger.info({
label: 'Mode',
message: develop
? 'Design Time'
: isEmbedded
? 'Run Time (Embedded)'
: 'Run Time'
});
consoleLogger.info({ label: 'Namespace', message: ns });
consoleLogger.info({
label: 'Embedded UserDir Found',
message: isEmbedded.toString()
});
consoleLogger.info({ label: 'UserDir', message: getUserDir() });
consoleLogger.info({ label: 'Flow File', message: nrSettings.flowFile });
consoleLogger.info({
label: 'UI Enabled',
message: (!nrSettings.disableEditor).toString()
});
// Start the HTTP server
server.on('error', (e) => {
consoleLogger.error({
label: 'Could Not Start Server',
message: e.message
});
});
server.on('listening', (e) => {
RED.start()
.catch((err) => {
consoleLogger.error({ label: 'Could not start', message: err.message });
})
.then(() => {
if (!nrSettings.disableEditor) {
consoleLogger.info({
label: 'UI Endpoint',
message: `http://127.0.0.1:${nrSettings.uiPort}${nrSettings.httpAdminRoot}`
});
}
});
});
server.listen(nrSettings.uiPort);
};
// Run the main function and handle any errors
run().catch((err) => {
consoleLogger.error({ label: 'Could not start', message: err.message });
process.exit(1);
});