feat: Добавлена библиотека Loguru
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
import loguru
|
||||
import asyncio
|
||||
from weakref import WeakValueDictionary, WeakSet, finalize
|
||||
from collections import UserDict
|
||||
@@ -18,7 +18,7 @@ class TopicWeakDict(UserDict):
|
||||
super().__setitem__(key, value)
|
||||
|
||||
class Server:
|
||||
_logger: logging.Logger = logging.getLogger(__name__)
|
||||
_logger: loguru.Logger = loguru.logger
|
||||
_static_tokens: list[str] = []
|
||||
_temp_tokens: list[str] = []
|
||||
_requests: WeakValueDictionary[str, asyncio.Future] = WeakValueDictionary()
|
||||
@@ -27,7 +27,6 @@ class Server:
|
||||
|
||||
@classmethod
|
||||
def init(cls, *, app: FastAPI, static_tokens: list[str] = []):
|
||||
cls._logger.addHandler(logging.NullHandler())
|
||||
cls._static_tokens = static_tokens
|
||||
|
||||
cls._requests: WeakValueDictionary[str, asyncio.Future] = WeakValueDictionary()
|
||||
@@ -76,7 +75,7 @@ class Server:
|
||||
@classmethod
|
||||
def _register_routes(cls, app):
|
||||
@app.websocket('/pyg2o')
|
||||
async def pyg2o(websocket: WebSocket, token: str, topics: str):
|
||||
async def pyg2o(websocket: WebSocket, token: str, topics: str | None):
|
||||
await cls._handle_connection(websocket, token, topics)
|
||||
|
||||
_ = pyg2o
|
||||
@@ -96,10 +95,10 @@ class Server:
|
||||
cls._topics[topic].discard(connection)
|
||||
|
||||
@classmethod
|
||||
async def _handle_connection(cls, connection: WebSocket, token: str, topics: str):
|
||||
async def _handle_connection(cls, connection: WebSocket, token: str, topics: str | None):
|
||||
|
||||
cls._logger.exception(f'PyG2O соединение отклонено: получен токен {token}\nStatic Tokens: {cls._static_tokens}')
|
||||
if not await cls._process_query_params(connection, token, topics):
|
||||
cls._logger.exception(f'PyG2O соединение отклонено: получен токен {token}\nStatic Tokens: {cls._static_tokens}')
|
||||
await connection.close()
|
||||
return
|
||||
|
||||
@@ -120,7 +119,7 @@ class Server:
|
||||
cls._logger.exception(f'Ошибка WebSocket подключения: {e}')
|
||||
|
||||
@classmethod
|
||||
async def _process_query_params(cls, connection: WebSocket, token: str, topics: str) -> bool:
|
||||
async def _process_query_params(cls, connection: WebSocket, token: str, topics: str | None) -> bool:
|
||||
|
||||
if token not in cls._static_tokens and token not in cls._temp_tokens:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user