-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.6 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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const { healthz } = require('express-healthz');
const app = express();
app.use(healthz);
app.use('/v1/execute', bodyParser.json(), async (req, res) => {
try {
const data = {
body: req.body,
headers: req.headers,
protocol: req.protocol,
host: req.hostname,
pathname: req.originalUrl,
method: req.method
};
const url = `${process.env.APPWRITE_ENDPOINT}/functions/${process.env.APPWRITE_FUNCTION_ID}/executions`;
const body = {
async: false,
data: JSON.stringify(data)
};
const headers = {
'Content-Type': 'application/json',
'x-appwrite-key': process.env.APPWRITE_API_KEY,
'x-appwrite-project': process.env.APPWRITE_PROJECT_ID
};
const response = await axios.post(
url,
body,
{
headers
}
);
res.json({
data: response.data,
code: response.status,
});
} catch(err) {
// Axios error
if (err.response) {
res.json({
data: err.response.data,
code: err.response.status,
});
return;
}
// Generic error
res.json({
data: "Unexpected server error: " + (err.message ? err.message : err),
code: 500,
});
}
});
app.use(express.static('site'));
app.listen(process.env.PORT);