-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (56 loc) · 1.66 KB
/
index.ts
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
import { getData as getEnergaData } from "./energa.js";
import { getData as getPgnigData } from "./pgnig.js";
import http from "node:http";
import pkg from "./package.json";
type EnergaData = Awaited<ReturnType<typeof getEnergaData>>;
type PgnigData = Awaited<ReturnType<typeof getPgnigData>>;
const cacheTimeMs = 60 * 60 * 24 * 1000;
const cache: {
energaData?: { data: EnergaData; timestamp: number };
pgnigData?: { data: PgnigData; timestamp: number };
} = {};
const hostname = "0.0.0.0";
const port = 3000;
const server = http.createServer(async (req, res) => {
res.setHeader("Content-Type", "application/json");
const now = Date.now();
try {
if (!cache.energaData || cache.energaData.timestamp + cacheTimeMs < now) {
try {
const data = await getEnergaData();
cache.energaData = {
data,
timestamp: now,
};
} catch (err) {
console.error(`Error on getEnergaData`, err);
}
}
if (!cache.pgnigData || cache.pgnigData.timestamp + cacheTimeMs < now) {
try {
const data = await getPgnigData();
cache.pgnigData = {
data,
timestamp: now,
};
} catch (err) {
console.error(`Error on getPgnigData`, err);
}
}
const data = {
...cache.energaData?.data,
...cache.pgnigData?.data,
};
res.statusCode = 200;
console.log(data);
res.end(JSON.stringify(data));
} catch (err) {
console.error(err);
res.statusCode = 500;
res.end(JSON.stringify(err));
}
});
console.log(`Starting ${pkg.name}`);
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});