-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (111 loc) · 4.34 KB
/
index.js
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
require('dotenv').config(); // Make sure this is at the top
const express = require("express");
const http = require('http'); // Required to create the server
const socketIo = require('socket.io');
const connectDb = require('./db.js');
const cors = require('cors'); // Import cors
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const { sendMessage, joinRoomAndFetchMessages } = require('./controllers/chat/chatController');
const app = express();
const PORT = process.env.PORT || 3000;
// Connect to MongoDB
connectDb();
// Swagger setup
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Express API Documentation with WebSocket',
version: '1.0.0',
description: 'API documentation for Express app with WebSocket integration',
},
servers: [
{
url: `https://hillffair-backend-2k24.onrender.com`,
},
],
},
apis: ['./routes/*.js'], // Paths to route files for Swagger to generate docs
};
const swaggerSpecs = swaggerJsdoc(swaggerOptions);
// Middleware for parsing JSON data
app.use(express.json());
// Middleware for parsing URL-encoded data
app.use(express.urlencoded({ extended: false }));
// Serve Swagger documentation at /api-docs
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
// Create the server with http.createServer to handle both HTTP and WebSocket
const server = http.createServer(app);
// Initialize Socket.IO on the server
const io = socketIo(server, {
cors: {
origin: "*", // Allow any origin for simplicity
methods: ["GET", "POST"]
}
});
// Middleware
app.use(cors());
// Import and use various routes
// const authRoutes = require("./routes/authRoute.js");
// app.use('/auth', authRoutes);
const otpRoutes = require("./routes/otpRoute.js");
app.use('/verify-otp', otpRoutes);
const votingRoutes = require("./routes/votingRoute.js");
app.use('/voting', votingRoutes);
const memberRoutes = require("./routes/memberRoute.js");
app.use('/members', memberRoutes);
const submitFormAnswersRoutes = require("./routes/submitFormAnswersRoute.js");
app.use('/submitFormAnswers', submitFormAnswersRoutes);
const matchRoutes = require("./routes/matchRoute.js");
app.use('/match', matchRoutes);
const highlightRoutes = require('./routes/highlightRoute');
app.use('/highlight', highlightRoutes);
const eventRoutes = require('./routes/eventRoute');
app.use('/event', eventRoutes);
const questionRoutes = require('./routes/questionRoute');
app.use('/questions', questionRoutes);
//Route fro user information filled during firebase authentication
const userRoutes = require('./routes/userRoute');
app.use('/user', userRoutes);
// Chat routes
const chatRoutes = require('./routes/chatRoutes');
app.use('/api/chat', chatRoutes);
// Socket.IO integration for chat functionality
// io.on('connection', (socket) => {
// console.log('New client connected');
// socket.on('joinRoomAndSendMessage', (userId1, userId2, messageContent) => {
// const chatController = require('./controllers/chat/chatController');
// chatController.sendMessage(socket, userId1, userId2, messageContent);
// });
// socket.on('disconnect', () => {
// console.log('Client disconnected');
// });
// });
io.on('connection', (socket) => {
console.log('New client connected');
socket.on('joinRoom', (userId1, userId2) => {
joinRoomAndFetchMessages(socket, userId1, userId2);
});
socket.on('joinRoomAndSendMessage', (userId1, userId2, messageContent) => {
console.log(messageContent)
sendMessage(socket, userId1, userId2, messageContent);
// joinRoomAndFetchMessages(socket, userId1, userId2); // Fetch previous messages after sending
});
// Disconnect and leave room
socket.on('disconnect', () => {
console.log('Client disconnected');
// const roomId = generateRoomId(userId1, userId2); // Assuming you have user IDs available
// socket.leave(roomId); // Leave the specific room
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ message: 'Something went wrong' });
});
// Start the server (use `server.listen` instead of `app.listen`)
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Swagger docs available at https://hillffair-backend-2k24.onrender.com/api-docs`);
});