File size: 1,492 Bytes
05d8628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import express from 'express';
import http from 'http';
import path from 'path';
import { Server } from 'socket.io';
import cors from 'cors';
import { setupSocketHandlers } from './socket/socketHandler';
import { ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData } from './types';

const app = express();
const server = http.createServer(app);

// Prod-Ready CORS
app.use(cors({
  origin: "*",
  methods: ["GET", "POST"]
}));

// SERVE FRONTEND (Hugging Face Mono-container Support)
const clientBuildPath = path.join(__dirname, '../../client/out');
app.use(express.static(clientBuildPath));

// Handle Next.js Routing (SPA support)
app.get('*', (req, res, next) => {
    // If it's a socket or API call, skip
    if (req.url.startsWith('/socket.io')) return next();
    res.sendFile(path.join(clientBuildPath, 'index.html'), (err) => {
        if (err) {
            // Fallback for Next.js non-static export if needed, 
            // but we'll assume static export for HF simplicity
            next();
        }
    });
});

const io = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>(server, {
  cors: {
    origin: "*", // Critical for Hugging Face / Vercel communication
    methods: ["GET", "POST"],
    credentials: true
  }
});

setupSocketHandlers(io);

// Hugging Face expects port 7860
const PORT = process.env.PORT || 7860;

server.listen(PORT, () => {
  console.log(`🚀 Game Server is running on port ${PORT}`);
});