This repository has been archived by the owner on Apr 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
startup-install.plugin.js
145 lines (118 loc) · 3.81 KB
/
startup-install.plugin.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
'use strict';
const Waterfall = require('async').waterfall;
const Http = require('http');
const Express = require('express');
const RED = require('node-red');
const EmbeddedStart = require('node-red-embedded-start');
const NR = require('../datasources').NodeRED;
const GlobalFlow = require('../config/global-flow.json');
const AppConstants = require('../config/app-constants');
const installGlobalFlow = (callback) => {
NR.flow.findAll((err, body) => {
if (err) {
console.error(new Error(err));
callback(err);
}
const globalFlowId = body.find((element) => {
return element.type === 'tab' && element.label === 'Global Flow';
});
if (!globalFlowId) {
console.log('Installing Global Flow.');
NR.flow.save(GlobalFlow, (err, id) => {
if (err) {
console.error(new Error(err));
callback(err);
}
if (!id) {
const err = new Error('Error creating global flow');
console.error(err);
callback(err);
}
callback(null);
});
}
else {
console.log('Updating Global Flow at :' + globalFlowId.id);
NR.flow.update(globalFlowId.id, GlobalFlow, (err, flow) => {
if (err) {
console.error(new Error(err));
callback(err);
}
if (!flow) {
const err = new Error('Error creating global flow');
console.error(err);
callback(err);
}
console.log('Global Flow Updated');
callback(null);
});
}
});
};
const startNodeRED = (callback) => {
// Create an Express app
const app = Express();
// Add a simple route for static content served from 'public'
app.use('/', Express.static('public'));
// Create a server
const server = Http.createServer(app);
// Create the settings object - see default settings.js file for other options
const settings = {
httpAdminRoot: '/',
httpNodeRoot: '/',
userDir: AppConstants.NODE_RED_DATA,
flowFile: 'conveyor_flows.json',
apiMaxLength: '500gb',
logging: {
// Console logging
console: {
level: 'error',
metrics: false,
audit: false
}
},
functionGlobalContext: {} // enables global context
};
// 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(1880);
// Start the runtime
RED.start().then(EmbeddedStart(RED)).then((result) => {
// result is whatever RED.start() resolved to
// RED.node.getFlows() etc are now ready to use
//Still calls a little bit too fast so adding a delay.
setTimeout(() => {
callback(null);
}, 1000 * 5);
}).catch((err) => {
if (/^timed out/.test(err.message)) {
// embeddedStart() timed out
// the value that RED.start() resolved to is available as err.result
}
if (err) {
console.log(err);
callback(err);
}
});
};
exports.register = (server, options, next) => {
Waterfall([
startNodeRED,
installGlobalFlow
], (err) => {
if (err) {
throw err;
}
else {
next();
}
});
};
exports.register.attributes = {
name: 'startup',
version: '0.0.1'
};