-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (104 loc) · 2.32 KB
/
server.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
"use strict";
const Hapi = require("@hapi/hapi");
const axios = require("axios");
const server = Hapi.server({
port: 3000,
host: "0.0.0.0", // needed for Render deployment
});
const buildBody = (device_uid, to, from) => {
return {
size: 500,
sort: [
{
when_captured: {
order: "desc",
},
},
],
query: {
bool: {
must: [],
filter: [
{
bool: {
should: [
{
match_phrase: {
device_urn: "note:" + device_uid,
},
},
],
},
},
{
range: {
service_uploaded: {
format: "strict_date_optional_time",
gte: from,
lte: to,
},
},
},
],
},
},
};
};
const url =
"https://40ad140d461d810ac41ed710b5c7a5b6.us-west-2.aws.found.io:9243/_search";
const headers = {
"Content-Type": "application/json",
Authorization:
"Basic " +
new Buffer(
process.env.SAFECAST_USERNAME + ":" + process.env.SAFECAST_PASSWORD
).toString("base64"),
};
const init = async () => {
await server.register([
{
plugin: require("@hapi/inert"),
options: {},
},
{
plugin: require("hapi-pino"),
options: {
prettyPrint: true,
logEvents: ["response", "onPostStart"],
},
},
]);
server.route({
method: "GET",
path: "/",
options: {
cors: {
origin: ["http://localhost:5555", "https://airnote.live"],
},
handler: async (request, h) => {
const device_uid = request.query.device_uid;
const to = request.query.to;
const from = request.query.from;
try {
const response = await axios.post(
url,
buildBody(device_uid, to, from),
{
headers: headers,
}
);
return h.response(response.data).type("application/json").code(200);
} catch (err) {
return h.response().code(500);
}
},
},
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});
init();