feat: New Server class for FastAPI websockets
This commit is contained in:
35
src/pyg2o/server_v3.py
Normal file
35
src/pyg2o/server_v3.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from fastapi import WebSocket, FastAPI, Depends
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
|
||||
class Server:
|
||||
|
||||
def __init__(self, app: FastAPI):
|
||||
self._security = HTTPBasic()
|
||||
self._register_routes(app)
|
||||
|
||||
def _register_routes(self, app):
|
||||
@app.get('/auth')
|
||||
async def pyg2o_auth(credentials: HTTPBasicCredentials = Depends(self._security)):
|
||||
...
|
||||
|
||||
@app.websocket('/pyg2o')
|
||||
async def pyg2o_main(websocket: WebSocket):
|
||||
await self._handle_server_connection(websocket)
|
||||
|
||||
@app.websocket('/pyg2o/{playerid}')
|
||||
async def pyg2o_client(websocket: WebSocket, playerid: int):
|
||||
await self._handle_client_connection(websocket, playerid)
|
||||
|
||||
# I have to do this crap, because I've spent like 2hrs to figure out why pyright ignores type: ignore stuff
|
||||
# I give up
|
||||
_ = pyg2o_main
|
||||
_ = pyg2o_client
|
||||
|
||||
async def _verify_server_token(self, headers):
|
||||
...
|
||||
|
||||
async def _handle_server_connection(self, websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
|
||||
async def _handle_client_connection(self, websocket: WebSocket, playerid: int):
|
||||
...
|
||||
Reference in New Issue
Block a user