cleanup: route table, lobby rename, missing-file 404s
This commit is contained in:
1 parent
e688c2bdf1
commit
2c783e07ca
3 files changed
+30
-40
No files matched your search
@@ -9,30 +9,50 @@ interface Player {
|
||||
}
|
||||
|
||||
const rooms = new Map<string, { teacherWs: any; players: Map<any, Player> }>();
|
||||
const pages: Record<string, string> = {
|
||||
"/": "index.html",
|
||||
"/host": "lobby.html",
|
||||
"/lobby": "lobby.html",
|
||||
"/waiting": "waiting.html",
|
||||
"/game": "game.html",
|
||||
"/trading": "trading.html",
|
||||
"/ws-test": "ws-test.html",
|
||||
};
|
||||
const server = Bun.serve<WebSocketData>({
|
||||
port: 3000,
|
||||
fetch(req, server) {
|
||||
async fetch(req, server) {
|
||||
const url = new URL(req.url);
|
||||
|
||||
console.log(req.method, url.pathname);
|
||||
|
||||
// If the request if for a Websocket upgrade, let Bun handle it
|
||||
//
|
||||
if (url.pathname === "/ws") {
|
||||
if (server.upgrade(req, { data: { role: "student", roomCode: "" } }))
|
||||
return;
|
||||
return new Response("WebSocket upgrade failed", { status: 400 });
|
||||
}
|
||||
if (url.pathname === "/") return new Response(Bun.file("index.html"));
|
||||
if (url.pathname === "/waiting")
|
||||
return new Response(Bun.file("waiting.html"));
|
||||
if (url.pathname === "/game") return new Response(Bun.file("game.html"));
|
||||
if (url.pathname === "/host") return new Response(Bun.file("host.html"));
|
||||
|
||||
const page = pages[url.pathname];
|
||||
if (page) {
|
||||
const file = Bun.file(page);
|
||||
if (!(await file.exists())) {
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
return new Response(Bun.file(page));
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname.startsWith("/client/") ||
|
||||
url.pathname.startsWith("/assets/")
|
||||
) {
|
||||
if (url.pathname.includes("..")) {
|
||||
return new Response("Bad path", { status: 400 });
|
||||
}
|
||||
return new Response(Bun.file("." + url.pathname));
|
||||
}
|
||||
|
||||
if (url.pathname === "/styles.css")
|
||||
return new Response(Bun.file("styles.css"));
|
||||
if (url.pathname === "/index.js") return new Response(Bun.file("index.js"));
|
||||
if (url.pathname.startsWith("/assets/"))
|
||||
return new Response(Bun.file("." + url.pathname));
|
||||
if (url.pathname == "/api/health") {
|
||||
return Response.json({ ok: true, time: Date.now() });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user