-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (106 loc) · 3.28 KB
/
Copy pathapp.js
File metadata and controls
128 lines (106 loc) · 3.28 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const { createServer } = require('http');
const { Server } = require('socket.io');
require('dotenv').config();
// Import routes
const authRoutes = require('./routes/auth');
const marketplaceRoutes = require('./routes/marketplace');
const steamRoutes = require('./routes/steam');
const paymentRoutes = require('./routes/payments');
const userRoutes = require('./routes/users');
// Import middleware
const { authenticateToken } = require('./middleware/auth');
const errorHandler = require('./middleware/errorHandler');
const logger = require('./utils/logger');
// Import Steam bot manager
const SteamBotManager = require('./services/steamBotManager');
const app = express();
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: process.env.CLIENT_URL || "http://localhost:3000",
methods: ["GET", "POST"]
}
});
// Security middleware
app.use(helmet());
app.use(cors({
origin: process.env.CLIENT_URL || "http://localhost:3000",
credentials: true
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use(limiter);
// Body parsing middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Session configuration
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Database connection
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => logger.info('Connected to MongoDB'))
.catch(err => logger.error('MongoDB connection error:', err));
// Initialize Steam bot manager
const steamBotManager = new SteamBotManager();
steamBotManager.initialize();
// Make io and steamBotManager available to routes
app.use((req, res, next) => {
req.io = io;
req.steamBotManager = steamBotManager;
next();
});
// Routes
app.use('/api/auth', authRoutes);
app.use('/api/marketplace', marketplaceRoutes);
app.use('/api/steam', steamRoutes);
app.use('/api/payments', paymentRoutes);
app.use('/api/users', userRoutes);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Socket.io connection handling
io.on('connection', (socket) => {
logger.info(`User connected: ${socket.id}`);
socket.on('join-room', (userId) => {
socket.join(`user-${userId}`);
logger.info(`User ${userId} joined their room`);
});
socket.on('disconnect', () => {
logger.info(`User disconnected: ${socket.id}`);
});
});
// Error handling middleware
app.use(errorHandler);
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
});
module.exports = app;