Skip to content

Commit

Permalink
Merge branch 'master' into serhii_milestone_2_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhii Ofii authored Oct 20, 2024
2 parents 60c3ec3 + 757769e commit 343a57e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apprunner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:

# Specify the start phase command
run:
command: venv/bin/python3 linguaphoto.main # Adjust to the path to your application entry point
command: venv/bin/python3 -m linguaphoto.main # Adjust to the path to your application entry point
env:
- name: HOMEPAGE_URL
value: "https://linguaphoto.com"
Expand All @@ -36,4 +36,4 @@ run:
- name: OPENAI_API_KEY
value-from: "arn:aws:secretsmanager:us-east-1:725596835855:secret:linguaphoto-u59FHw:openai_key::"
- name: STRIPE_API_KEY
value-from: "arn:aws:secretsmanager:us-east-1:725596835855:secret:linguaphoto-u59FHw:stripe_private_key::"
value-from: "arn:aws:secretsmanager:us-east-1:725596835855:secret:linguaphoto-u59FHw:stripe_private_key::"
2 changes: 2 additions & 0 deletions linguaphoto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import socketio

from linguaphoto.api.api import router
from linguaphoto.socket_manager import (
Expand All @@ -23,6 +24,7 @@

app.include_router(router, prefix="")
app = socketio.ASGIApp(sio, app)

if __name__ == "__main__":
print("Starting webserver...")
uvicorn.run(app, port=8080, host="0.0.0.0")
2 changes: 1 addition & 1 deletion linguaphoto/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ openai

requests
stripe
python-socketio
python-socketio
47 changes: 47 additions & 0 deletions linguaphoto/socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Socket part."""

import socketio

from linguaphoto.settings import settings

# Create a new Socket.IO server with CORS enabled
sio = socketio.AsyncServer(
async_mode="asgi",
cors_allowed_origins=[settings.homepage_url], # Update this to match your frontend URL
)

# Dictionary to store connected users by their socket ID
connected_users: dict[str, str] = {}


# Handle client connection
@sio.event
async def connect(sid: str) -> None:
print(f"User connected: {sid}")


# Handle client disconnection
@sio.event
async def disconnect(sid: str) -> None:
if sid in connected_users:
print(f"User {connected_users[sid]} disconnected")
del connected_users[sid]


# Event for registering a specific user (e.g., after authentication)
@sio.event
async def register_user(sid: str, user_id: str) -> None:
connected_users[user_id] = sid
print(f"User {user_id} registered with session ID {sid}")


# Event to notify a specific user
async def notify_user(user_id: str, message: dict) -> None:
sid = connected_users.get(user_id)
if sid:
await sio.emit("notification", message, room=sid)
else:
print(f"User {user_id} is not connected")


# Export the `sio` instance so it can be used in other files
4 changes: 3 additions & 1 deletion linguaphoto/socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# Create a new Socket.IO server with CORS enabled
sio = socketio.AsyncServer(
async_mode="asgi",
cors_allowed_origins=["http://localhost:3000"]
cors_allowed_origins=[
settings.homepage_url
], # Update this to match your frontend URL
)
# Dictionary to store connected users by their socket ID
connected_users: dict[str, str] = {}
Expand Down

0 comments on commit 343a57e

Please sign in to comment.