forked from Syndic1/HGS-Cards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·93 lines (72 loc) · 3.51 KB
/
Copy pathserver.js
File metadata and controls
executable file
·93 lines (72 loc) · 3.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @file Application Entry Point
* @description Bootstraps the Cards Against Humanity web game server. Sets up Express for
* serving static assets and REST API routes, attaches Socket.IO for real-time game
* communication, and schedules periodic cleanup of abandoned games.
* @requires dotenv
* @requires express
* @requires http
* @requires socket.io
* @requires path
* @requires ./routes/decks
* @requires ./routes/games
* @requires ./sockets/index
* @requires ./services/gameService
*/
// Load environment variables from .env before any other module reads them
require('dotenv').config();
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const path = require('path');
const app = express();
// Wrap Express in a raw HTTP server so Socket.IO can share the same port
const server = http.createServer(app);
// Initialise Socket.IO on the shared HTTP server; clients connect via the default `/socket.io` path
const io = new Server(server);
// ---------------------------------------------------------------------------
// Middleware
// ---------------------------------------------------------------------------
// Parse JSON request bodies (used by the REST API for game creation, etc.)
app.use(express.json());
// Parse URL-encoded form data (used by the deck upload form)
app.use(express.urlencoded({ extended: true }));
// Serve the front-end static files (HTML, CSS, client-side JS) from the public directory
app.use(express.static(path.join(__dirname, 'public')));
// ---------------------------------------------------------------------------
// REST API Routes
// ---------------------------------------------------------------------------
// Deck management endpoints: list, upload (CSV), and delete decks
app.use('/api/decks', require('./routes/decks'));
// Game management endpoints: create a game and retrieve game state
app.use('/api/games', require('./routes/games'));
/**
* Catch-all route for the game page. The game code is parsed client-side from
* the URL, so we always serve the same static HTML regardless of the code value.
*
* @param {string} req.params.code - The 6-character game code embedded in the URL.
*/
app.get('/game/:code', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'game.html'));
});
// ---------------------------------------------------------------------------
// Socket.IO — Real-time Game Communication
// ---------------------------------------------------------------------------
// Register all Socket.IO event handlers (lobby + gameplay) on the shared `io` instance
require('./sockets/index')(io);
// ---------------------------------------------------------------------------
// Periodic Cleanup of Abandoned Games
// ---------------------------------------------------------------------------
const { cleanupAbandonedGames } = require('./services/gameService');
// Run cleanup immediately on startup to purge any stale games left over from a previous crash
cleanupAbandonedGames();
// Re-run cleanup every 5 minutes (300 000 ms) to keep the database tidy
setInterval(cleanupAbandonedGames, 5 * 60 * 1000);
// ---------------------------------------------------------------------------
// Start Listening
// ---------------------------------------------------------------------------
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || '0.0.0.0'; // Bind to all interfaces by default
server.listen(PORT, HOST, () => {
console.log(`Cards Against Humanity server running on http://${HOST}:${PORT}`);
});