-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (32 loc) · 1.52 KB
/
Copy pathserver.js
File metadata and controls
41 lines (32 loc) · 1.52 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
// Entry point: wires the optional database, the Express REST API, and the
// Socket.IO realtime server together, then starts listening.
//
// Architecture at a glance:
// - server/app.js Express REST API (auth + profiles/history)
// - server/socketHandlers.js Socket.IO realtime gameplay
// - server/gameStore.js in-memory active-game state (never hits Mongo)
// - server/chessRules.js server-authoritative move validation
// - utils/persistGame.js one-time persistence on game-over
require("dotenv").config();
const http = require("http");
const { Server } = require("socket.io");
const { connectDB } = require("./config/db");
const { socketAuth } = require("./middleware/auth");
const { createApp } = require("./server/app");
const { registerSocketHandlers } = require("./server/socketHandlers");
const PORT = process.env.PORT || 5001;
// Concrete origin(s) — required because we use credentialed CORS (cookies).
// "*" is not valid with credentials, so default to the local Next.js dev server.
const CLIENT_ORIGIN = process.env.CLIENT_ORIGIN
? process.env.CLIENT_ORIGIN.split(",").map((s) => s.trim())
: "http://localhost:3000";
// Kick off the (optional) database connection. Gameplay runs regardless.
connectDB();
const app = createApp(CLIENT_ORIGIN);
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: CLIENT_ORIGIN, credentials: true },
});
io.use(socketAuth);
registerSocketHandlers(io);
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));