diff --git a/Dockerfile b/Dockerfile index e69de29..4b6cec3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM python:3.12.4-slim-bullseye + +WORKDIR /home + +# Copy requirements first for better caching +COPY ./requirements.txt . + + +RUN python3 -m pip install -r requirements.txt && \ + apt-get clean && \ + apt-get autoclean && \ + apt-get autoremove -y && \ + rm -rf /var/lib/apt/lists/* + +EXPOSE 8000 + + +COPY ./entrypoint.sh . +COPY ./chatApp ./chatApp + +# Set permissions for entrypoint +RUN chmod +x entrypoint.sh + +CMD ["./entrypoint.sh"] diff --git a/docker-compose.yaml b/docker-compose.yaml index e69de29..a643bff 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -0,0 +1,41 @@ +services: + + fastapi-chat: + build: . + container_name: fastapi_chat + environment: + RUN_PORT: 8000 + depends_on: + - mongodb-server + restart: unless-stopped + env_file: + - ./env/fast-api.env + + mongodb-server: + image: mongodb/mongodb-community-server:latest + container_name: mongodb_server + restart: unless-stopped + env_file: + - ./env/mongodb.env + volumes: + - type: volume + source: mongodb_data_volume + target: /data/db + ports: + - '27117:27017' + + nginx: + build: ./nginx + container_name: chat_nginx + restart: always + volumes: + # - ./nginx/configs/:/etc/nginx/conf.d + - ./uploads:/home/media + ports: + - 8000:80 + depends_on: + - mongodb-server + - fastapi-chat + +volumes: + mongodb_data_volume: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..da1f509 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +RUN_PORT=${RUN_PORT:-8000} + +exec uvicorn chatApp.main:app --host 0.0.0.0 --port ${RUN_PORT} diff --git a/jsTester/index.html b/jsTester/index.html new file mode 100644 index 0000000..ef08b8a --- /dev/null +++ b/jsTester/index.html @@ -0,0 +1,18 @@ + + + + + + + + + Socket io tester + + +

welcome to Socket io tester

+ + diff --git a/jsTester/scripts.js b/jsTester/scripts.js new file mode 100644 index 0000000..f81e1a2 --- /dev/null +++ b/jsTester/scripts.js @@ -0,0 +1,71 @@ +const ROOM_ID = "66a7731a11fe8199049c8dbd"; // put your room ID here +const USER1_ID = "66a765df185ba34c8e4a4b1a"; // put your user ID here +const PRV_ROOM_ID = "66a785f6473ff39a7496f918"; // put your private room id to send message + +// Establish the Socket.IO connection +const sio = io("http://127.0.0.1:8000/", { + path: "/socket.io", + transports: ['websocket', 'polling'] +}); + +// Handle connection event +sio.on("connect", () => { + console.log("Connected to the server with SID:", sio.id); + + // Emit the 'join_room' event for a public room + // sio.emit('joining_public_room', { room_id: ROOM_ID, user_id: USER1_ID }); + + // Send a public message + // sio.emit('send_public_message', { room_id: ROOM_ID, message: "Hello, world JS!", user_id: USER1_ID }); + + // emit the join_room event for a private room + // sio.emit('joining_private_room', {room_id: PRV_ROOM_ID, user_id: USER1_ID}); + + // Send a private message + // sio.emit('send_private_message', {room_id: PRV_ROOM_ID, user_id: USER1_ID, message: "hello private!"}); +}); + +// Handle disconnection event +sio.on("disconnect", () => { + console.log("Disconnected from the server"); +}); + +// Handle connection errors +sio.on("connect_error", (err) => { + console.log("Connection error:", err.message); +}); + +// Listen for the 'client_count' event +sio.on("client_count", (count) => { + console.log(`Number of connected clients: ${count}`); +}); + +// Listen for the 'room_count' event +sio.on("room_count", (count) => { + console.log(`Number of connected clients in the room: ${count}`); +}); + +// Listen for the 'user_joined' event +sio.on("user_joined", (user) => { + console.log(`${user} joined the chat room`); +}); + +// Listen for the 'user_left' event +sio.on("user_left", (user) => { + console.log(`${user} left the chat room`); +}); + +// Listen for the 'message' event +sio.on("message", ({ sid, message, message_id, user_id }) => { + console.log(`Message: ${message_id} with content ${message} from ${user_id}`); +}); + +// Listen for the 'private_message' event +sio.on("private_message", ({ sid, message, recipient_id, sender_id }) => { + console.log(`Private message: ${message} from ${sender_id} to ${recipient_id}`); +}); + +// Handle general errors +sio.on("error", (err) => { + console.error("An error occurred:", err); +}); diff --git a/nginx/.Dockerignore b/nginx/.Dockerignore new file mode 100644 index 0000000..e69de29 diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..b498617 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx + +COPY configs/ /etc/nginx/conf.d/ diff --git a/nginx/configs/chatApp.conf b/nginx/configs/chatApp.conf new file mode 100644 index 0000000..eb63f51 --- /dev/null +++ b/nginx/configs/chatApp.conf @@ -0,0 +1,35 @@ +upstream chat-app { + server fastapi-chat:8000; +} + + +server { + listen 80; + listen [::]:80; + server_name myChat.fastapi.org www.myChat.fastapi.org; + + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + + location /media/ { + autoindex on; + alias /home/media/; + } + + location / { + proxy_pass http://chat-app; + proxy_ssl_server_name on; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /socket.io/ { + proxy_pass http://chat-app; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +}