diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000..d70cafc --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,25 @@ +name: Build and Deploy to Docker Hub +on: + push: + branches: + - master +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Check Out Repo + uses: actions/checkout@v2 + - name: Log in to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Build and Push Docker image + uses: docker/build-push-action@v2 + with: + context: ./backend + file: ./backend/Dockerfile + push: true + tags: somnath910/statuscode1:latest + - name: Verify Pushed Image + run: docker pull somnath910/statuscode1:latest \ No newline at end of file diff --git a/backend/src/controllers/chatControllers.ts b/backend/src/controllers/chatControllers.ts index 1d4d0cf..2df40f3 100644 --- a/backend/src/controllers/chatControllers.ts +++ b/backend/src/controllers/chatControllers.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; import { PrismaClient } from '@prisma/client'; -import { getCachedData, setCachedData } from '../lib/redis'; + const prisma = new PrismaClient(); @@ -9,11 +9,7 @@ export const getChatHistory = async (req: Request, res: Response) => { const { roomId } = req.params; try { - // const cacheKey = `search:${roomId}`; - // const cachedResults = await getCachedData(cacheKey); - // if (cachedResults) { - // return res.status(200).json(JSON.parse(cachedResults)); - // } + const messages = await prisma.message.findMany({ where: { chatRoomId: roomId }, include: { @@ -28,7 +24,6 @@ export const getChatHistory = async (req: Request, res: Response) => { message: message.content, at: message.timestamp, })); - // await setCachedData(cacheKey, JSON.stringify(messageFormat), 3600); res.json(messageFormat); } catch (error) { diff --git a/backend/src/server.ts b/backend/src/server.ts index ba1a751..8ce30f9 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -73,108 +73,108 @@ wss.on('connection', (ws) => { break; case 'sendMessage': - try { - const chatRoom = await prisma.chatRoom.findUnique({ - where: { id: roomId }, - include: { - users: { - select: { user_id: true, username: true, email: true } // Ensure to fetch email - } - } - }); - - if (!chatRoom) { - ws.send(JSON.stringify({ type: 'error', data: { message: 'Chat room not found' } })); - return; - } - - const newMessage = await prisma.message.create({ - data: { - content: data.message, - sender: { connect: { user_id: data.userId } }, - chatRoom: { connect: { id: data.roomId } }, - } - }); - - // Broadcast the message to all clients in the room - wss.clients.forEach(client => { - const rooms = clientRoomMap.get(client); - if (client.readyState === WebSocket.OPEN && rooms?.has(data.roomId)) { - client.send(JSON.stringify({ type: 'newMessage', data: { roomId: data.roomId, message: newMessage } })); - } - }); - - chatRoom.users.forEach(user => { - const isUserConnected = [...wss.clients].some( - client => clientRoomMap.get(client)?.has(data.roomId) && client.readyState === WebSocket.OPEN && user.user_id === data.userId - ); - - console.log('User connected:', isUserConnected); - if (!isUserConnected && user.user_id !== data.userId) { - const htmlContent = ` - -
- - - -You've received a new message in chat room ${data.roomId}.
-Click the link below and enter the room id to view the message:
- - -You've received a new message in chat room ${data.roomId}.
+Click the link below and enter the room id to view the message:
+ + +