-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlambda.js
117 lines (110 loc) · 3.36 KB
/
lambda.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
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const compression = require('compression')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
const RED = require('node-red')
let server;
let headless = (process.env.HEADLESS === "true") || false;
let reloadFlow = (process.env.RELOAD_FLOW === "true") || false;
let settings = {
disableEditor: true,
httpAdminRoot: false,
httpNodeRoot: '/',
// httpStatic: 'public',
awsRegion: process.env.AWS_REGION,
awsS3Bucket: process.env.S3_BUCKET,
awsS3Appname: process.env.AWS_LAMBDA_FUNCTION_NAME,
storageModule: require('node-red-contrib-storage-s3'),
functionGlobalContext: { },
credentialSecret: process.env.NODE_RED_SECRET || "a-secret-key"
};
if (headless) {
settings.httpRoot = false;
settings.httpAdminRoot = false;
settings.httpNodeRoot = false;
}
// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below, then redeploy (`npm run package-deploy`)
const binaryMimeTypes = [
'application/javascript',
'application/json',
'application/octet-stream',
'application/xml',
'font/eot',
'font/opentype',
'font/otf',
'image/jpeg',
'image/png',
'image/svg+xml',
'text/comma-separated-values',
'text/css',
'text/html',
'text/javascript',
'text/plain',
'text/text',
'text/xml'
]
if (!headless) {
app.use(compression())
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(awsServerlessExpressMiddleware.eventContext())
server = awsServerlessExpress.createServer(app, null, binaryMimeTypes)
}
let init = (() => {
if (headless) {
RED.init(settings)
}else{
RED.init(server, settings)
//app.use(settings.httpAdminRoot,RED.httpAdmin);
app.use(settings.httpNodeRoot,RED.httpNode);
}
return new Promise((resolve, reject) => {
let deployed;
RED.events.on("runtime-event", deployed = function(data){
if (data.id === "runtime-deploy") {
RED.events.removeListener("runtime-event", deployed);
// console.log('flow deployed');
resolve();
}
})
RED.start();
});
})()
function setup(){
return init.then(() => {
return new Promise((resolve, reject) => {
if (reloadFlow) {
RED.nodes.loadFlows().then(() => { resolve() });
}else{
resolve();
}
});
});
}
exports.handler = (event, context, callback) => {
setup().then(()=>{
if (headless) {
let handlers = {};
function clearHandlers(){
for(var key in handlers) RED.events.removeListener(key, handlers[key]);
}
function setHandlers(){
for(var key in handlers) RED.events.once(key, handlers[key]);
}
handlers['aws:lambda:done:' + context.awsRequestId] = function(msg){ clearHandlers(); callback(null, msg) };
handlers['aws:lambda:error'] = function(msg){ clearHandlers(); callback(msg) };
setHandlers();
RED.events.emit('aws:lambda:invoke', event, context)
}else{
awsServerlessExpress.proxy(server, event, context)
}
})
}