-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (109 loc) · 3.2 KB
/
index.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
#!/usr/bin/env node
var os = require('os');
var fs = require('fs');
var path = require('path');
var wait = require('wait.for');
var yaml = require('js-yaml');
var handlerFile = path.resolve(process.cwd() + '/index.js');
var jouleFile = path.resolve(process.cwd() + '/../.joule.yml');
var eventsFile = path.resolve(process.cwd() + '/events.json');
var networkInterfaces = os.networkInterfaces();
var ipAddress;
for(var adpt in networkInterfaces) {
var ifaces = networkInterfaces[adpt];
for(var idx in ifaces) {
var iface = ifaces[idx];
if(iface.internal === false && iface.family === 'IPv4') {
ipAddress = iface.address;
}
}
}
var context = {
succeed: function(input) {
console.log(input);
}
};
var events;
if(process.version != 'v0.10.36') {
console.log('WARN: Deployed Joules run using nodejs version v0.10.36. You are currently running ' + process.version + '.\n');
}
try {
events = JSON.parse(fs.readFileSync(eventsFile, 'utf8'));
if(!events) {
console.log('Unable to JSON parse ' + eventsFile + '.');
return;
} else if(!events['events']) {
console.log('Unable to find `events[\'events\']` in events.json file.');
return;
}
if(typeof(events.env) === 'object') {
for(var envKey in events.env) {
process.env[envKey] = events.env[envKey];
}
}
} catch(e) {
if (e.code == 'ENOENT') {
console.log('Failed to read ' + eventsFile + '.');
} else {
console.log('Exception when trying to access or parse ' + eventsFile + '.');
}
return;
}
try {
yaml = yaml.safeLoad(fs.readFileSync(jouleFile, 'utf8'));
} catch(e) {
console.log(e);
return;
}
var mapRequestIntoPayload = function(method, path, payload) {
var apiPath = ''
, apiQuery = ''
, apiQueryAsObject = {}
, pathAsArray
, returnedPayload = {httpMethod: method, remoteAddr: ipAddress};
// check if path has a ?
if(path.indexOf('?') === -1) {
apiPath = path;
} else {
apiPath = path.substr(0, path.indexOf('?'));
apiQuery = path.substr(path.indexOf('?')+1);
var tmp1 = apiQuery.split('&')
, tmp1parts;
for(var tmp1cnt=0; tmp1cnt<tmp1.length; tmp1cnt++) {
tmp1parts = tmp1[tmp1cnt].split('=');
apiQueryAsObject[tmp1parts[0]] = tmp1parts[1];
}
}
if(method === 'POST' || method === 'PUT') {
returnedPayload['post'] = payload;
} else if(method === 'GET') {
for (var attrname in payload) { apiQueryAsObject[attrname] = payload[attrname]; }
}
if(apiPath === '/') {
pathAsArray = [];
} else {
pathAsArray = apiPath.split('/').slice(1);
}
returnedPayload['path'] = pathAsArray;
returnedPayload['query'] = apiQueryAsObject;
return returnedPayload;
};
var run = function(method, path, payload) {
var handler = require(handlerFile), mappedPayload;
try {
payload = mapRequestIntoPayload(method, path, payload);
wait.for(handler.handler, payload, context);
} catch(e) {
console.log('FAILURE: ' + e.message);
console.log(e.stack);
}
};
var i;
var callEvents = events['events'];
for(var method in callEvents) {
for(var path in callEvents[method]) {
for(var i=0; i<callEvents[method][path].length; i++) {
wait.launchFiber(run, method, path, callEvents[method][path][i]);
}
}
}