|
1 |
| -from fastapi import APIRouter, WebSocket, WebSocketDisconnect |
2 |
| -from fastapi.responses import HTMLResponse |
| 1 | +from fastapi import APIRouter, Request, WebSocket, WebSocketDisconnect |
| 2 | +from fastapi.templating import Jinja2Templates |
3 | 3 |
|
4 |
| -router = APIRouter() |
5 |
| - |
6 |
| - |
7 |
| -class ConnectionManager: |
8 |
| - def __init__(self): |
9 |
| - self.active_connections: list[WebSocket] = [] |
10 |
| - |
11 |
| - async def connect(self, websocket: WebSocket): |
12 |
| - await websocket.accept() |
13 |
| - self.active_connections.append(websocket) |
14 |
| - |
15 |
| - def disconnect(self, websocket: WebSocket): |
16 |
| - self.active_connections.remove(websocket) |
| 4 | +from template_fastapi.repositories.chats import ChatRepository |
| 5 | +from template_fastapi.settings.chats import get_chats_settings |
17 | 6 |
|
18 |
| - async def send_personal_message(self, message: str, websocket: WebSocket): |
19 |
| - await websocket.send_text(message) |
20 |
| - |
21 |
| - async def broadcast(self, message: str): |
22 |
| - for connection in self.active_connections: |
23 |
| - await connection.send_text(message) |
24 |
| - |
25 |
| - |
26 |
| -manager = ConnectionManager() |
| 7 | +router = APIRouter() |
| 8 | +templates = Jinja2Templates(directory="template_fastapi/templates") |
| 9 | +chat_repository = ChatRepository() |
27 | 10 |
|
28 | 11 |
|
29 | 12 | @router.get(
|
30 | 13 | "/chats/",
|
31 | 14 | tags=["chats"],
|
32 | 15 | )
|
33 |
| -async def get(): |
34 |
| - # read the HTML file |
35 |
| - try: |
36 |
| - with open("template_fastapi/templates/chats.html") as file: |
37 |
| - html = file.read() |
38 |
| - except FileNotFoundError: |
39 |
| - html = "<h1>Chat page not found</h1>" |
40 |
| - except Exception as e: |
41 |
| - html = f"<h1>Error reading chat page: {str(e)}</h1>" |
42 |
| - return HTMLResponse(html) |
| 16 | +async def get(request: Request): |
| 17 | + """Get the chat page with configurable WebSocket URL.""" |
| 18 | + settings = get_chats_settings() |
| 19 | + return templates.TemplateResponse( |
| 20 | + "chats.html", |
| 21 | + { |
| 22 | + "request": request, |
| 23 | + "websocket_url": settings.chats_websocket_url, |
| 24 | + }, |
| 25 | + ) |
43 | 26 |
|
44 | 27 |
|
45 | 28 | @router.websocket(
|
46 | 29 | "/ws/{client_id}",
|
47 | 30 | )
|
48 | 31 | async def websocket_endpoint(websocket: WebSocket, client_id: int):
|
49 |
| - await manager.connect(websocket) |
| 32 | + """WebSocket endpoint for chat functionality.""" |
| 33 | + await chat_repository.manager.connect(websocket) |
50 | 34 | try:
|
51 | 35 | while True:
|
52 | 36 | data = await websocket.receive_text()
|
53 |
| - await manager.send_personal_message(f"You wrote: {data}", websocket) |
54 |
| - await manager.broadcast(f"Client #{client_id} says: {data}") |
| 37 | + await chat_repository.handle_client_message(data, websocket, client_id) |
55 | 38 | except WebSocketDisconnect:
|
56 |
| - manager.disconnect(websocket) |
57 |
| - await manager.broadcast(f"Client #{client_id} left the chat") |
| 39 | + chat_repository.manager.disconnect(websocket) |
| 40 | + await chat_repository.handle_client_disconnect(client_id) |
0 commit comments