-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
76 lines (60 loc) · 2.26 KB
/
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
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
/// <reference no-default-lib="true"/>
/// <reference lib="deno.ns" />
/// <reference lib="esnext" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="es2020.intl" />
import { renderExample } from "./preact_rts.tsx";
const handleNoop = async (request: Request) => {
const queryParams = new URL(request.url).searchParams;
const N = parseInt(queryParams.get("N") || "5", 10);
await new Promise((resolve) => setTimeout(resolve, N * 1000));
return { status: "ok" };
};
const handleCpu = (request: Request) => {
const queryParams = new URL(request.url).searchParams;
const N = parseInt(queryParams.get("N") || "5", 10);
const C = parseInt(queryParams.get("C") || "5", 10);
const O = parseInt(queryParams.get("O") || "1", 10);
if (O) {
return { status: "ok", body: renderExample(N, C) };
}
renderExample(N, C);
return { status: "ok" };
};
const handleData = async (request: Request) => {
const queryParams = new URL(request.url).searchParams;
const addresses: string[] = [];
for (let i = 1; i <= 10; i++) {
const addressParam = queryParams.get(`address${i}`);
if (addressParam) {
addresses.push(addressParam);
}
}
const fetchPromises = addresses.map((address) => fetch(address));
const results = (await Promise.all(fetchPromises)).map((response) => response.status);
const statusCounts = results.reduce((counts, status) => {
counts[status] = (counts[status] || 0) + 1;
return counts;
}, {} as Record<number, number>);
return statusCounts;
};
const router = (request: Request) => {
const url = new URL(request.url);
switch (url.pathname) {
case "/noop":
return handleNoop(request);
case "/cpu":
return handleCpu(request);
case "/data":
return handleData(request);
default:
return { status: "not found" };
}
};
console.log("Server is running on http://localhost:8000");
const handler = async (request: Request): Promise<Response> => {
const result = await router(request);
return new Response(JSON.stringify(result), { headers: new Headers({ "Content-Type": "application/json" }) });
}
Deno.serve({ port: 8000 }, handler);