diff --git a/index.ts b/index.ts index 82d87fd..77bf31d 100644 --- a/index.ts +++ b/index.ts @@ -9,30 +9,50 @@ interface Player { } const rooms = new Map }>(); +const pages: Record = { + "/": "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({ 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() }); } diff --git a/host.html b/lobby.html similarity index 100% rename from host.html rename to lobby.html diff --git a/student.html b/student.html deleted file mode 100644 index 8b58215..0000000 --- a/student.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - Escape the Island - - - - - - - -
-
-

Escape the Island

-
- -
-
-
-
- - -