-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
66 lines (57 loc) · 1.88 KB
/
server.ts
File metadata and controls
66 lines (57 loc) · 1.88 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
import "dotenv/config";
import { createServer } from "http";
import next from "next";
import { Server } from "socket.io";
import { registerSocketHandlers } from "./src/lib/socketHandlers";
import { prisma } from "./src/lib/db";
import type {
ClientToServerEvents,
ServerToClientEvents,
} from "./src/types/socket";
async function cleanupStaleSessions() {
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
const deleted = await prisma.session.deleteMany({
where: { lastActiveAt: { lt: thirtyDaysAgo } },
});
if (deleted.count > 0) {
console.log(`[Cleanup] Deleted ${deleted.count} stale sessions`);
}
}
const dev = process.env.NODE_ENV !== "production";
const hostname = process.env.HOSTNAME || "localhost";
const port = parseInt(process.env.PORT || "3200", 10);
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server<ClientToServerEvents, ServerToClientEvents>(
httpServer,
{
cors: {
origin: dev
? [
"http://rollinit.dev.local",
"http://rollinit-api.dev.local",
"http://localhost:3200",
]
: "https://rollinit.app",
methods: ["GET", "POST"],
},
// Increase ping timeout for mobile clients
pingTimeout: 60000,
}
);
io.on("connection", (socket) => {
console.log(`[Socket] Connected: ${socket.id}`);
registerSocketHandlers(io, socket);
socket.on("disconnect", (reason) => {
console.log(`[Socket] Disconnected: ${socket.id} (${reason})`);
});
});
httpServer.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
// Cleanup stale sessions on startup and every 24 hours
cleanupStaleSessions();
setInterval(cleanupStaleSessions, 24 * 60 * 60 * 1000);
});
});