-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.js
125 lines (109 loc) · 3.43 KB
/
bootstrap.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
"use strict";
const express = require("express");
const app = express();
const handlers = require("./dist/handlers");
const bodyParser = require("body-parser");
const isArray = a => {
return !!a && a.constructor === Array;
};
const isObject = a => {
return !!a && a.constructor === Object;
};
if (process.env.RAW_BODY === "true") {
app.use(bodyParser.raw({ type: "*/*" }));
} else {
var jsonLimit = process.env.MAX_JSON_SIZE || "100kb"; //body-parser default
app.use(bodyParser.json({ limit: jsonLimit }));
app.use(bodyParser.raw()); // "Content-Type: application/octet-stream"
app.use(bodyParser.text({ type: "text/*" }));
}
app.disable("x-powered-by");
function parseJson(str) {
try {
const data = JSON.parse(str);
return data;
} catch (e) {
return null;
}
}
const middleware = async (req, res) => {
try {
let inputData = {
req: {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
path: req.path
},
...(process.env ? process.env : {})
};
// --- consolidate all input (including possible env vars)
if (req.body) {
if (typeof req.body === "string") {
// --- faas-cli has a bug could introduce an extra newline to the input T_T
const jsonData = parseJson(req.body.trim());
if (isObject(inputData)) {
inputData = {
...inputData,
...jsonData
};
} else if (isArray(inputData)) {
inputData.req.body = inputData;
}
} else if (isObject(req.body)) {
inputData = {
...inputData,
...req.body
};
}
}
let handler;
if (!inputData.handler) {
handler = handlers.default;
} else {
handler = handlers[inputData.handler];
}
if (typeof handler !== "function") {
throw new Error(
`Can't locate requested handler: ${
inputData.handler ? inputData.handler : "default"
}`
);
}
const result = await handler(inputData);
if (Array.isArray(result) || isObject(result)) {
res.status(200).json(result);
} else if (typeof result === "string") {
res.set("Content-Type", "text/plain");
res.status(200).send(result);
} else {
res.set("Content-Type", "text/plain");
if (typeof result === "undefined" || result === null) {
res.status(200).end();
} else {
res.status(200).send("" + result);
}
}
} catch (e) {
console.error(e);
res.set("Content-Type", "text/plain");
if (e.stack) {
res.status(500).send("" + e.stack);
} else {
res.status(500).send("" + e);
}
}
};
app.get("/healthz", (req, res) => {
res.status(200).send("OK");
});
app.post("/*", middleware);
app.get("/*", middleware);
app.patch("/*", middleware);
app.put("/*", middleware);
app.delete("/*", middleware);
const port = process.env.http_port || 3000;
app.listen(port, () => {
console.log(`OpenFaaS Node.js listening on port: ${port}`);
});