-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
58 lines (46 loc) · 1.74 KB
/
Copy pathindex.ts
File metadata and controls
58 lines (46 loc) · 1.74 KB
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
import homepage from "./index.html";
function getRandomInt(min: number, max: number) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
}
Bun.serve({
// `routes` requires Bun v1.2.3+
routes: {
"/": homepage,
"/api/status": new Response("OK"),
// Хто я? (трекер, варіант №4)
"/api/whoami": (req: Request) => {
return new Response(`Your IP is ${req.headers.get("X-Forwarded-For")?.split(",")[0]}\n`);
},
// Кубик (рандомайзер, варіант №2)
"/api/d6": () => {
const dice = getRandomInt(1, 7);
return new Response(`The dice rolled a number ${dice}\n`);
},
// Календар (варіант №1)
"/api/today": () => {
const today = new Date();
return new Response(`Today is ${today.toDateString()} \n`);
},
// Годинник (варіант №3)
"/api/now": () => {
const today = new Date();
return new Response(`The time now is ${today.toTimeString()} \n`);
},
// Відлік (варіант №5)
"/api/weekend": () => {
const today = new Date();
const weekday = today.getDay();
const days = (6 - weekday) % 7;
return new Response(`There are ${days === 0 ? 7 : days} days left to Saturday \n`);
},
// Wildcard route for all routes that start with "/api/" and aren't otherwise matched
"/api/*": Response.json({ message: "Not found" }, { status: 404 }),
},
// (optional) fallback for unmatched routes:
// Required if Bun's version < 1.2.3
fetch() {
return new Response("Not Found", { status: 404 });
},
});