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(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}`); });