-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
63 lines (45 loc) · 1.55 KB
/
server.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
import { serve } from "https://deno.land/std@0.167.0/http/server.ts";
const images = await loadFiles("images");
const routes = {
root: new URLPattern({ pathname: "/" }),
docs: new URLPattern({ pathname: "/docs" }),
statistics: new URLPattern({ pathname: "/statistics" }),
specific: new URLPattern({ pathname: "/:id" }),
};
async function serverHandler({ url }: Request) {
if (routes.root.exec(url)) {
const randomPic = images[Math.floor(Math.random() * images.length)];
const response = await fetch(randomPic.path);
return new Response(response.body);
}
if (routes.docs.exec(url)) {
return Response.redirect("https://github.com/UltiRequiem/kira-api");
}
if (routes.statistics.exec(url)) {
return Response.json({ totalImages: images.length, images });
}
const specific = routes.specific.exec(url);
if (specific) {
const id = parseInt(specific.pathname.groups.id);
const result = images.find((image) => image.id === id);
if (!result) {
return Response.json(
{ error: "Not found (try /statistics)" },
{ status: 404 },
);
}
const response = await fetch(result.path);
return new Response(response.body);
}
return new Response(undefined, { status: 404 });
}
serve(serverHandler);
async function loadFiles(dir: string) {
let id = 1;
const files: { id: number; path: URL }[] = [];
for await (const dirEntry of Deno.readDir(`./${dir}`)) {
const path = new URL(`./${dir}/${dirEntry.name}`, import.meta.url);
files.push({ id: id++, path });
}
return files;
}