M-Xeeshan/Project-Tracking
0
1from fastapi import FastAPI, WebSocket2from fastapi.staticfiles import StaticFiles3import asyncio4import websockets5import json6 7app = FastAPI()8 9# Mount static files for the web interface10app.mount("/static", StaticFiles(directory="static"), name="static")11 12# Store projects received from Admin Page Space13projects = {}14 15@app.on_event("startup")16async def startup_event():17 # Connect to Admin Page Space WebSocket on startup18 asyncio.create_task(connect_to_admin_page_ws())19 20async def connect_to_admin_page_ws():21 admin_page_ws_url = wss://M-Xeeshan-Project-Management.hf.space/ws # WebSocket endpoint22 while True:23 try:24 async with websockets.connect(admin_page_ws_url) as websocket:25 print("Connected to Admin Page Space WebSocket.")26 while True:27 data = await websocket.recv()28 project = json.loads(data)29 projects[project["unique_id"]] = project30 print(f"Received project: {project}")31 except Exception as e:32 print(f"WebSocket connection error: {e}. Retrying in 5 seconds...")33 await asyncio.sleep(5)34 35@app.get("/projects")36async def get_projects():37 return projects38 39@app.get("/")40async def read_root():41 with open("static/index.html", "r") as file:42 return file.read()43 