-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: some of state transition and event looping fix!: refactor items api
- Loading branch information
Gaisberg
authored and
Gaisberg
committed
Aug 4, 2024
1 parent
3aea8a4
commit b91710a
Showing
19 changed files
with
635 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name: Docker Build and Push Dev | |
on: | ||
push: | ||
branches: | ||
- main | ||
- dev | ||
|
||
jobs: | ||
build-and-push-dev: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
data/ | ||
logs/ | ||
settings.json | ||
ignore.txt | ||
.vscode | ||
.git | ||
makefile | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
from loguru import logger | ||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect | ||
|
||
router = APIRouter( | ||
prefix="/ws", | ||
tags=["websocket"], | ||
responses={404: {"description": "Not found"}}) | ||
|
||
class ConnectionManager: | ||
def __init__(self): | ||
self.active_connections: list[WebSocket] = [] | ||
|
||
async def connect(self, websocket: WebSocket): | ||
await websocket.accept() | ||
logger.debug("Frontend connected!") | ||
self.active_connections.append(websocket) | ||
await websocket.send_json({"type": "health", "status": "running"}) | ||
|
||
def disconnect(self, websocket: WebSocket): | ||
logger.debug("Frontend disconnected!") | ||
self.active_connections.remove(websocket) | ||
|
||
async def send_personal_message(self, message: str, websocket: WebSocket): | ||
await websocket.send_text(message) | ||
|
||
async def send_log_message(self, message: str): | ||
await self.broadcast({"type": "log", "message": message}) | ||
|
||
async def send_item_update(self, item: json): | ||
await self.broadcast({"type": "item_update", "item": item}) | ||
|
||
async def broadcast(self, message: json): | ||
for connection in self.active_connections: | ||
try: | ||
await connection.send_json(message) | ||
except RuntimeError: | ||
self.active_connections.remove(connection) | ||
|
||
|
||
manager = ConnectionManager() | ||
|
||
|
||
@router.websocket("") | ||
async def websocket_endpoint(websocket: WebSocket): | ||
await manager.connect(websocket) | ||
try: | ||
while True: | ||
await websocket.receive_text() | ||
except WebSocketDisconnect: | ||
manager.disconnect(websocket) | ||
except RuntimeError: | ||
manager.disconnect(websocket) |
Oops, something went wrong.