-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (61 loc) · 2.12 KB
/
index.js
File metadata and controls
74 lines (61 loc) · 2.12 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { createServer } from "node:http";
import { fileURLToPath } from "url";
import { hostname } from "node:os";
import { readdir } from "node:fs/promises";
import path from "node:path";
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const publicPath = path.join(__dirname, "public");
const fastify = Fastify({
serverFactory: (handler) => {
return createServer()
.on("request", (req, res) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
handler(req, res);
})
},
});
fastify.register(fastifyStatic, {
root: publicPath,
prefix: "/",
});
fastify.get("/subjects", async (req, reply) => {
return reply.sendFile("subjects/index.html");
});
fastify.get("/settings", async (req, reply) => {
return reply.sendFile("settings/index.html");
});
fastify.get("/about", async (req, reply) => {
return reply.sendFile("about/index.html");
});
fastify.get("/home", async (req, reply) => {
return reply.sendFile("home/index.html");
});
fastify.get("/api/games", async (req, reply) => {
try {
const gmsPath = path.join(publicPath, "gms");
const folders = await readdir(gmsPath, { withFileTypes: true });
let games = [];
for (const folder of folders) {
if (folder.isDirectory()) {
const files = await readdir(path.join(gmsPath, folder.name));
const icon = files.find(f => f.endsWith(".png") || f.endsWith(".jpg"));
games.push({
name: folder.name.replace(/-/g, " "),
url: `/gms/${folder.name}/index.html`,
icon: icon ? `/gms/${folder.name}/${icon}` : null
});
}
}
return games;
} catch (e) {
console.error(e);
return [];
}
});
const PORT = 8001;
fastify.listen({ port: PORT, host: "0.0.0.0" }, () => {
console.log(`Server running at http://localhost:${PORT}`);
});