-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (33 loc) · 1.27 KB
/
server.js
File metadata and controls
40 lines (33 loc) · 1.27 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
import express from 'express';
import ExpressWs from 'express-ws';
import cors from 'cors';
import bodyParser from 'body-parser';
import config from './config.js';
import { Logger } from './utils/logger.js';
import { loggerMiddleware } from './middleware/http.js';
import { createAuthRoutes } from './routes/auth.js';
import { createRoomRoutes } from './routes/rooms.js';
import { createProjectRoutes } from './routes/projects.js';
import { createHistoryRoutes } from './routes/history.js';
import { WebSocketService } from './services/WebSocketService.js';
// Initialize Express and WebSocket
const { app } = ExpressWs(express());
// Middleware
app.use(cors({ origin: '*' }));
app.use(bodyParser.json());
app.use(loggerMiddleware);
// Initialize WebSocket service
const wsService = new WebSocketService();
// Routes
app.use('/api/auth', createAuthRoutes());
app.use('/api/rooms', createRoomRoutes());
app.use('/api/projects', createProjectRoutes());
app.use('/api/projects', createHistoryRoutes());
// WebSocket endpoint
app.ws('/room/:id', (ws, req) => {
const token = req.query.token;
const roomName = req.params.id;
wsService.handleConnection(ws, token, roomName);
});
// Start server
app.listen(config.port, () => Logger.log('SERVER', `Ready at http://localhost:${config.port}`));