-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (94 loc) · 2.86 KB
/
server.js
File metadata and controls
108 lines (94 loc) · 2.86 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const dotenv = require('dotenv');
const path = require('path');
// Load environment variables
dotenv.config();
// Import routes and services
const gameRoutes = require('./routes/game');
const walletRoutes = require('./routes/wallet');
const cryptoRoutes = require('./routes/crypto');
const GameService = require('./services/GameService');
const WebSocketService = require('./services/WebSocketService');
const logger = require('./utils/logger');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// Middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Rate limiting
const limiter = rateLimit({
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS) || 15 * 60 * 1000,
max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 100,
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);
// Routes
app.use('/api/game', gameRoutes);
app.use('/api/wallet', walletRoutes);
app.use('/api/crypto', cryptoRoutes);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Initialize services
const gameService = new GameService();
const webSocketService = new WebSocketService(io, gameService);
webSocketService.startCryptoPriceUpdates();
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/crypto-crash', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
logger.info('Connected to MongoDB');
// Start the game service
gameService.start();
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
logger.info(`WebSocket server ready for connections`);
});
})
.catch((error) => {
logger.error('MongoDB connection error:', error);
process.exit(1);
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
gameService.stop();
server.close(() => {
logger.info('Server closed');
mongoose.connection.close(() => {
logger.info('MongoDB connection closed');
process.exit(0);
});
});
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
gameService.stop();
server.close(() => {
logger.info('Server closed');
mongoose.connection.close(() => {
logger.info('MongoDB connection closed');
process.exit(0);
});
});
});
module.exports = { app, server, io };