mt5-manager/main.py

46 lines
1.7 KiB
Python
Raw Permalink Normal View History

2025-10-14 16:04:37 +03:00
'''
File: main.py
Description: Веб-приложение для веб-сервера терминалов, основной файл FastAPI
'''
__version__ = '0.1.0'
2025-10-04 15:10:53 +03:00
# Импортируем нужные классы из библиотек
2025-10-14 23:54:26 +03:00
from fastapi import FastAPI, Request, Path
2025-10-10 10:16:59 +03:00
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
2025-10-07 21:48:20 +03:00
from fastapi.responses import HTMLResponse, JSONResponse
2025-10-10 10:16:59 +03:00
from mt5_control import start_mt5, stop_mt5, load_instances
2025-10-04 15:10:53 +03:00
# Создаём объект приложения
app = FastAPI()
2025-10-14 14:59:40 +03:00
# Подключаем статические файлы
2025-10-14 16:04:37 +03:00
app.mount('/static', StaticFiles(directory='static'), name='static')
2025-10-14 14:59:40 +03:00
# Создаём объект для работы с шаблонами HTML-кода
2025-10-14 16:04:37 +03:00
templates = Jinja2Templates(directory='templates')
2025-10-10 10:16:59 +03:00
2025-10-06 23:34:05 +03:00
@app.get('/', response_class=HTMLResponse)
2025-10-10 10:16:59 +03:00
async def index(request: Request):
2025-10-14 16:04:37 +03:00
'''Обработчик GET / (корневой каталог) - панель управления терминалами'''
2025-10-10 10:16:59 +03:00
instances = load_instances()
2025-10-14 16:04:37 +03:00
return templates.TemplateResponse('index.html', {'request': request, 'instances': instances})
2025-10-07 21:48:20 +03:00
2025-10-14 14:59:40 +03:00
@app.post('/start/{name}')
2025-10-14 23:54:26 +03:00
async def start_instance(name: str = Path(..., description="Название экземпляра", example="MetaTrader5.1")):
'''Запуск экземпляра терминала'''
result = start_mt5(name)
2025-10-07 21:48:20 +03:00
return JSONResponse(result)
2025-10-14 14:59:40 +03:00
@app.post('/stop/{name}')
2025-10-14 23:54:26 +03:00
async def stop_instance(name: str = Path(..., description="Название экземпляра", example="MetaTrader5.1")):
'''Остановка экземпляра терминала'''
result = stop_mt5(name)
2025-10-07 21:48:20 +03:00
return JSONResponse(result)