-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
33 lines (25 loc) · 895 Bytes
/
index.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
import dotenv from "dotenv";
dotenv.config();
import { getSession } from "./src/server/session";
import { handleRequest } from "./src/server/routes";
import { createWebSocketHandler } from "./src/server/websocket";
import { setAllUsersOffline } from "./src/server/status";
const port = process.env.PORT || 5177;
// Set all users to offline when server starts
await setAllUsersOffline();
const wsHandler = createWebSocketHandler();
const server: any = Bun.serve({
port,
async fetch(req) {
const sessionId = req.headers
.get("Cookie")
?.split(";")
.find((c) => c.trim().startsWith("sessionId="))
?.split("=")[1];
const user = sessionId ? await getSession(sessionId) : null;
return handleRequest(req, server, user);
},
websocket: wsHandler.handler,
});
wsHandler.setServer(server);
console.log(`Chat server running at http://localhost:${port}`);