Update server.py
Browse files
server.py
CHANGED
|
@@ -1,71 +1,81 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
| 2 |
-
from fastapi.
|
| 3 |
-
from fastapi.
|
| 4 |
-
from fastapi.
|
| 5 |
-
import
|
| 6 |
-
import
|
| 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 |
-
return templates.TemplateResponse("
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
async def
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import HTMLResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.templating import Jinja2Templates
|
| 6 |
+
import json
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# Configure CORS
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], # Allows all origins
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"], # Allows all methods
|
| 17 |
+
allow_headers=["*"], # Allows all headers
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Serve static files from the "static" directory
|
| 21 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
+
|
| 23 |
+
# Set up Jinja2 templates
|
| 24 |
+
templates = Jinja2Templates(directory="templates")
|
| 25 |
+
|
| 26 |
+
with open("data.json", "r", encoding="utf8") as file:
|
| 27 |
+
maps_data = json.load(file)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.get("/", response_class=HTMLResponse)
|
| 31 |
+
async def read_index(request: Request):
|
| 32 |
+
return templates.TemplateResponse("index.html", {"request": request, "maps": maps_data})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.get("/map/{normalized_name}", response_class=HTMLResponse)
|
| 36 |
+
async def get_map_by_normalized_name(request: Request, normalized_name: str):
|
| 37 |
+
map_entry = maps_data.get(normalized_name)
|
| 38 |
+
if map_entry:
|
| 39 |
+
filtered_maps = [map_entry]
|
| 40 |
+
else:
|
| 41 |
+
filtered_maps = []
|
| 42 |
+
return templates.TemplateResponse("map.html", {"request": request, "maps": filtered_maps})
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class ConnectionManager:
|
| 46 |
+
def __init__(self):
|
| 47 |
+
self.active_connections: list[WebSocket] = []
|
| 48 |
+
|
| 49 |
+
async def connect(self, websocket: WebSocket):
|
| 50 |
+
await websocket.accept()
|
| 51 |
+
self.active_connections.append(websocket)
|
| 52 |
+
|
| 53 |
+
def disconnect(self, websocket: WebSocket):
|
| 54 |
+
self.active_connections.remove(websocket)
|
| 55 |
+
|
| 56 |
+
async def send_personal_message(self, message: str, websocket: WebSocket):
|
| 57 |
+
await websocket.send_text(message)
|
| 58 |
+
|
| 59 |
+
async def broadcast(self, message: str):
|
| 60 |
+
for connection in self.active_connections:
|
| 61 |
+
# print(f"Broadcast: {message}")
|
| 62 |
+
await connection.send_text(message)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
manager = ConnectionManager()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@app.websocket("/{client_id}")
|
| 69 |
+
async def websocket_endpoint(websocket: WebSocket, client_id: int):
|
| 70 |
+
await manager.connect(websocket)
|
| 71 |
+
try:
|
| 72 |
+
while True:
|
| 73 |
+
data = await websocket.receive_text()
|
| 74 |
+
await manager.broadcast(data) # Direktes Broadcasten der Daten
|
| 75 |
+
except WebSocketDisconnect:
|
| 76 |
+
manager.disconnect(websocket)
|
| 77 |
+
await manager.broadcast(f"Client #{client_id} left the chat")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|