-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
303 lines (264 loc) · 9.55 KB
/
server.js
File metadata and controls
303 lines (264 loc) · 9.55 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
const http = require('http');
const socketIo = require('socket.io');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const connectDB = require('./config/db');
// Import routes
const userRoutes = require('./routes/user');
const collabRoutes = require('./routes/collab');
const storyRoutes = require('./routes/story');
const purchaseRoutes = require('./routes/purchase');
const contactRoutes = require('./routes/contact');
const workspaceRoutes = require('./routes/workspace');
const aiRoutes = require('./routes/ai');
// Import middleware
const errorHandler = require('./middleware/error');
const app = express();
const server = http.createServer(app);
const sessionSecret = process.env.SESSION_SECRET;
const mongoUri = process.env.MONGO_URI || process.env.MONGODB_URI;
const publicRoot = path.resolve(__dirname, 'public');
const socketAllowedOrigins = (process.env.SOCKET_CORS_ORIGIN || process.env.CORS_ORIGIN || 'http://localhost:3000')
.split(',')
.map(origin => origin.trim())
.filter(Boolean);
const ROOM_TTL_MS = 24 * 60 * 60 * 1000;
const ROOM_CLEANUP_INTERVAL_MS = 60 * 60 * 1000;
if (!sessionSecret) {
throw new Error('Missing SESSION_SECRET environment variable');
}
if (!mongoUri) {
throw new Error('Missing MONGO_URI (or MONGODB_URI) environment variable');
}
function resolveSafePublicPath(requestPath = '') {
const normalized = path.posix.normalize(`/${String(requestPath || '')}`).replace(/^\/+/, '');
const resolved = path.resolve(publicRoot, normalized);
if (resolved === publicRoot || resolved.startsWith(`${publicRoot}${path.sep}`)) {
return resolved;
}
return null;
}
const io = socketIo(server, {
cors: {
origin: socketAllowedOrigins,
methods: ["GET", "POST"]
}
});
// Connect to MongoDB
connectDB();
// Middleware
app.use(cors({
origin: process.env.CORS_ORIGIN || 'http://localhost:3000',
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// Serve static files from the public directory
app.use(express.static(path.join(__dirname, 'public')));
// Add session middleware
app.use(session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: mongoUri,
ttl: 24 * 60 * 60 // 1 day
}),
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000, // 1 day
sameSite: 'lax'
}
}));
// Routes
app.use('/api/users', userRoutes);
app.use('/api/collab', collabRoutes);
app.use('/api/stories', storyRoutes);
app.use('/api/purchases', purchaseRoutes);
app.use('/api/contact', contactRoutes);
app.use('/api/workspaces', workspaceRoutes);
app.use('/api/ai', aiRoutes);
// Public routes that don't require authentication
app.get('/collaborations/:page', (req, res) => {
const safePath = resolveSafePublicPath(`collaborations/${req.params.page}`);
if (!safePath) {
return res.status(400).send('Invalid path');
}
return res.sendFile(safePath, error => {
if (error) {
return res.redirect('/login.html');
}
});
});
// Handle all other routes by serving the appropriate HTML file
app.get('*', (req, res) => {
const safePath = resolveSafePublicPath(req.path);
const indexPath = path.join(publicRoot, 'index.html');
if (!safePath) {
return res.status(400).send('Invalid path');
}
if (req.path.startsWith('/collaborations/') && !(req.session && req.session.userId)) {
return res.redirect('/login.html');
}
return res.sendFile(safePath, error => {
if (error) {
return res.sendFile(indexPath);
}
});
});
// Store active collaborations
const activeCollaborations = new Map();
function touchCollaboration(collaboration) {
if (collaboration) {
collaboration.lastActivity = Date.now();
}
}
setInterval(() => {
const cutoff = Date.now() - ROOM_TTL_MS;
activeCollaborations.forEach((collaboration, room) => {
if ((collaboration.lastActivity || 0) < cutoff) {
activeCollaborations.delete(room);
}
});
}, ROOM_CLEANUP_INTERVAL_MS);
// Socket.IO connection handling
io.on('connection', (socket) => {
console.log('New client connected:', socket.id);
// Join collaboration room
socket.on('joinRoom', ({ room, isPrimaryUser }) => {
socket.join(room);
if (!activeCollaborations.has(room)) {
activeCollaborations.set(room, {
users: new Map(), // Changed to Map to store user type
canvasState: null,
chat: [],
primaryUser: isPrimaryUser ? socket.id : null,
lastActivity: Date.now()
});
}
const collab = activeCollaborations.get(room);
touchCollaboration(collab);
// Only a client that explicitly joins as primary can become the drawer.
if (isPrimaryUser) {
collab.primaryUser = socket.id;
}
// Store user type
collab.users.set(socket.id, {
isPrimary: socket.id === collab.primaryUser
});
// Send current canvas state to new user
if (collab.canvasState) {
socket.emit('canvasState', collab.canvasState);
}
// Notify all users in the room about new user
io.to(room).emit('userJoined', {
userCount: collab.users.size,
isPrimaryUser: socket.id === collab.primaryUser
});
});
// Handle chat messages
socket.on('chatMessage', payload => {
const room = payload?.room;
if (!room || !activeCollaborations.has(room)) {
return;
}
const collab = activeCollaborations.get(room);
touchCollaboration(collab);
socket.to(room).emit('chatMessage', {
message: String(payload?.message || ''),
sender: String(payload?.sender || 'Collaborator'),
sentAt: payload?.sentAt || new Date().toISOString(),
attachment: payload?.attachment || null
});
});
// Handle drawing events - only allow from primary user
socket.on('draw', (data) => {
const collab = activeCollaborations.get(data.room);
if (collab && socket.id === collab.primaryUser) {
touchCollaboration(collab);
socket.to(data.room).emit('draw', data);
}
});
// Handle canvas state updates - only allow from primary user
socket.on('canvasState', (data) => {
const collab = activeCollaborations.get(data.room);
if (collab && socket.id === collab.primaryUser) {
touchCollaboration(collab);
collab.canvasState = data.state;
socket.to(data.room).emit('canvasState', data.state);
}
});
// Handle clear canvas - only allow from primary user
socket.on('clear', (data) => {
const collab = activeCollaborations.get(data.room);
if (collab && socket.id === collab.primaryUser) {
touchCollaboration(collab);
if (typeof data.state === 'string') {
collab.canvasState = data.state;
}
socket.to(data.room).emit('clear', { state: data.state || '' });
}
});
// Handle undo/redo - only allow from primary user
socket.on('undo', (data) => {
const collab = activeCollaborations.get(data.room);
if (collab && socket.id === collab.primaryUser) {
touchCollaboration(collab);
if (typeof data.state === 'string') {
collab.canvasState = data.state;
}
socket.to(data.room).emit('undo', { state: data.state || '' });
}
});
socket.on('redo', (data) => {
const collab = activeCollaborations.get(data.room);
if (collab && socket.id === collab.primaryUser) {
touchCollaboration(collab);
if (typeof data.state === 'string') {
collab.canvasState = data.state;
}
socket.to(data.room).emit('redo', { state: data.state || '' });
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
activeCollaborations.forEach((collab, room) => {
if (collab.users.has(socket.id)) {
collab.users.delete(socket.id);
// If primary user left, clear that status
if (collab.primaryUser === socket.id) {
collab.primaryUser = null;
}
// Notify remaining users about the disconnection
io.to(room).emit('userLeft', {
userCount: collab.users.size
});
if (collab.users.size === 0) {
activeCollaborations.delete(room);
}
}
});
});
});
// Error handling middleware
app.use(errorHandler);
// Only start server if MongoDB connects successfully
mongoose.connection.once('open', () => {
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
});
mongoose.connection.on('error', (err) => {
console.error(`MongoDB connection error: ${err}`);
process.exit(1);
});