-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
31 lines (24 loc) · 907 Bytes
/
main.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
import { deepMerge } from "https://deno.land/[email protected]/collections/mod.ts";
const endpoints = Deno.env.get("ENDPOINTS")?.split(",");
if (!endpoints)
throw "ENDPOINTS env var not set. Should be comma-separated list of JSON endpoints to merge";
console.log("endpoints", endpoints);
const portenv = Deno.env.get("PORT");
const port = portenv ? parseInt(portenv) : 8000;
const handler = async (_request: Request): Promise<Response> => {
let mergedJson = {};
for (const endpoint of endpoints) {
try {
const resp = await fetch(endpoint);
const respJson = await resp.json();
mergedJson = deepMerge(mergedJson, respJson);
} catch(e) {
console.error(e)
}
}
return new Response(JSON.stringify(mergedJson), {
headers: [["content-type", "application/json"]],
});
};
console.log(`HTTP server running: http://0.0.0.0:${port}/`);
Deno.serve({ port }, handler);