39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { readFileSync } from "fs";
|
|
import { handleMessage, handleClose } from "./ws-handler";
|
|
|
|
const PORT = parseInt(process.env.PORT || "3009", 10);
|
|
|
|
const server = Bun.serve({
|
|
port: PORT,
|
|
hostname: "0.0.0.0",
|
|
fetch(req, server) {
|
|
const url = new URL(req.url);
|
|
if (url.pathname === "/ws") {
|
|
if (!server.upgrade(req)) return new Response("WS upgrade failed", { status: 400 });
|
|
return undefined as any;
|
|
}
|
|
if (url.pathname === "/styles.css") {
|
|
return new Response(readFileSync("./src/public/styles.css", "utf-8"), {
|
|
headers: { "Content-Type": "text/css" },
|
|
});
|
|
}
|
|
if (url.pathname === "/script.js") {
|
|
return new Response(readFileSync("./src/public/script.js", "utf-8"), {
|
|
headers: { "Content-Type": "text/javascript" },
|
|
});
|
|
}
|
|
return new Response(readFileSync("./src/public/index.html", "utf-8"), {
|
|
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
});
|
|
},
|
|
websocket: {
|
|
perMessageDeflate: true,
|
|
maxPayloadLength: 32 * 1024,
|
|
message(ws, msg) { if (typeof msg === "string") handleMessage(ws, msg); },
|
|
close(ws) { handleClose(ws); },
|
|
open(_ws) {},
|
|
},
|
|
});
|
|
|
|
console.log(`\x1b[32m[BUZZER]\x1b[0m → \x1b[36mhttp://localhost:${server.port}\x1b[0m`);
|