-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (56 loc) · 2 KB
/
Copy pathmain.py
File metadata and controls
73 lines (56 loc) · 2 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
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
from fastapi import FastAPI, WebSocket
from fastapi.middleware.cors import CORSMiddleware
from .routes import router
from .websocket import connect_user, disconnect_user, send_message
from .database import messages_collection
import json
from datetime import datetime
app = FastAPI()
# ✅ CORS (important for frontend)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ✅ include routes
app.include_router(router)
# ✅ Home route (optional)
@app.get("/")
def home():
return {"message": "Chat backend is running 🚀"}
# ✅ WebSocket endpoint
@app.websocket("/ws/{user_id}")
async def websocket_endpoint(websocket: WebSocket, user_id: str):
user_id = user_id.lower() # normalize
await connect_user(user_id, websocket)
try:
while True:
data = await websocket.receive_text()
msg = json.loads(data)
print("📩 Received:", msg)
# 🔥 Typing event
if msg.get("type") == "typing":
await send_message(msg["receiver"], msg)
continue
# ❌ Validate message
if "sender" not in msg or "receiver" not in msg:
print("❌ Invalid message:", msg)
continue
# ✅ Normalize usernames
msg["sender"] = msg["sender"].lower()
msg["receiver"] = msg["receiver"].lower()
# ✅ Add metadata
msg["timestamp"] = datetime.utcnow().isoformat()
msg["read"] = False
# ✅ Save to MongoDB
try:
messages_collection.insert_one(msg)
except Exception as e:
print("❌ DB Error:", e)
# ✅ Send to receiver
await send_message(msg["receiver"], msg)
except Exception as e:
print("❌ WebSocket crashed:", e)
await disconnect_user(user_id)