-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (67 loc) · 2.34 KB
/
index.js
File metadata and controls
76 lines (67 loc) · 2.34 KB
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
const { default: axios } = require("axios");
const express = require("express");
const app = express();
const port = 8008;
app.use(express.json());
// Egress endpoint. For use when your enclave has egress enabled
app.get("/egress", async (req, res) => {
try {
const result = await axios.get(
"https://jsonplaceholder.typicode.com/posts/1",
);
res.send({ ...result.data });
} catch (err) {
console.log("Could not send request out of enclave", err);
res.status(500).send({ msg: "Error from within the enclave!" });
}
});
// Compute endpoint. Adds two numbers together and returns the sum
app.all("/compute", async (req, res) => {
try {
result = parseInt(req.body.a) + parseInt(req.body.b);
res.send({ sum: result });
} catch (err) {
console.log("Could not compute sum of a and b", err);
res.status(500).send({ msg: "Error from within the enclave!" });
}
});
// Encrypt endpoint. Calls out to the encrypt API in the enclave to encrypt the request body
app.all("/encrypt", async (req, res) => {
try {
const result = await axios.post("http://127.0.0.1:9999/encrypt", req.body);
res.send({ ...result.data });
} catch (err) {
console.log("Could not encrypt body", err);
res.status(500).send({ msg: "Error from within the enclave!" });
}
});
// Decrypt endpoint. Calls out to the decrypt API in the enclave to decrypt the request body
// This is for demo purposes - the enclave will automatically decrypt fields as they go into the enclave
app.all("/decrypt", async (req, res) => {
try {
const result = await axios.post("http://127.0.0.1:9999/decrypt", req.body);
res.send({ ...result.data });
} catch (err) {
console.log("Could not decrypt body", err);
res.status(500).send({ msg: "Error from within the enclave!" });
}
});
app.get("/health", (req, res) => {
// perform some healthcheck...
return res.send("OK");
});
// Simple hello world endpoint. Add a body and it will be returned in the response.
app.all("*name", async (req, res) => {
try {
res.send({
response: "Hello! I'm writing to you from within an enclave",
...req.body,
});
} catch (err) {
console.log("Could not handle hello request", err);
res.status(500).send({ msg: "Error from within the enclave!" });
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});