feat: added static for main page, added views.py: index, get_condig, websocket_endpoint (functionality for receiving commands from the frontend), added main.py: creating an application, connecting static and connecting a router, added Settings.py: class Settings with model_config and stream_url
This commit is contained in:
14
src/web_robot_control/main.py
Normal file
14
src/web_robot_control/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import FastAPI
|
||||
from starlette.staticfiles import StaticFiles
|
||||
|
||||
from web_robot_control.views import router
|
||||
|
||||
|
||||
# создаем экземпляр FastAPI
|
||||
app = FastAPI()
|
||||
|
||||
# подключаем статические файлы
|
||||
app.mount('/static', StaticFiles(directory='static'), name='static')
|
||||
|
||||
# подключаем роутер
|
||||
app.include_router(router)
|
||||
16
src/web_robot_control/settings.py
Normal file
16
src/web_robot_control/settings.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Класс для данных конфига"""
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file = '.env',
|
||||
env_file_encoding='utf-8',
|
||||
extra='ignore'
|
||||
)
|
||||
|
||||
stream_url: str
|
||||
|
||||
|
||||
settings = Settings()
|
||||
61
src/web_robot_control/views.py
Normal file
61
src/web_robot_control/views.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from fastapi import APIRouter, WebSocket
|
||||
from fastapi.requests import Request
|
||||
from fastapi.responses import HTMLResponse, Response
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from starlette.websockets import WebSocketDisconnect
|
||||
import httpx
|
||||
|
||||
from web_robot_control.settings import settings
|
||||
|
||||
|
||||
# Создаем объект роутера
|
||||
router = APIRouter()
|
||||
|
||||
# Создаём объект для рендеринга html-шаблонов
|
||||
templates = Jinja2Templates(directory='static')
|
||||
|
||||
|
||||
@router.get('/', response_class=HTMLResponse)
|
||||
async def index(request: Request) -> Response:
|
||||
"""
|
||||
Асинхронная функция для получения главной страницы приложения.
|
||||
"""
|
||||
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name='index.html',
|
||||
context={'title': 'Web-robot-control - Главная', 'name_robot': 'Bot1'}
|
||||
)
|
||||
|
||||
@router.get('/config')
|
||||
async def get_config() -> dict:
|
||||
"""Aсинхронная функция для получения stream_url."""
|
||||
|
||||
return {'stream_url': settings.stream_url}
|
||||
|
||||
|
||||
@router.websocket('/ws')
|
||||
async def websocket_endpoint(websocket: WebSocket) -> None:
|
||||
# Установка содединения по веб-сокету
|
||||
await websocket.accept()
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
while True:
|
||||
# Получение команды от клиента (с веб-сокета)
|
||||
command = await websocket.receive_text()
|
||||
print(f'Получена команда: {command}')
|
||||
|
||||
# Todo: здесь будет логика валидации команд
|
||||
|
||||
# Todo: здесь будет логика обработки команды
|
||||
|
||||
except WebSocketDisconnect:
|
||||
print('WebSocket отключен') # Todo: для вывода ошибок будет настроен logger
|
||||
# Todo: вместо Exception будут добавлена ловля других ошибок
|
||||
# (после того как функция будет полностью дописана)
|
||||
except Exception as err:
|
||||
err_text = f'Ошибка: {str(err)}'
|
||||
await websocket.send_text(err_text)
|
||||
print(err_text)
|
||||
Reference in New Issue
Block a user