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