80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
# app/src/bootstrap/app_factory.py
|
||
|
||
from __future__ import annotations
|
||
|
||
from aiogram import Bot, Dispatcher
|
||
from aiogram.client.default import DefaultBotProperties
|
||
|
||
from src.bootstrap.application import ApplicationComposition
|
||
from src.bootstrap.logging import setup_logging
|
||
from src.bootstrap.trade_stream_runtime import (
|
||
build_trade_stream_production_runtime,
|
||
)
|
||
from src.core.config import load_settings
|
||
from src.notifications.targets import NotificationTargetRegistry
|
||
from src.storage.schema import init_schema
|
||
from src.telegram.routers import setup_routers
|
||
from src.trading.journal.service import JournalService
|
||
|
||
|
||
def create_app() -> ApplicationComposition:
|
||
settings = load_settings()
|
||
|
||
setup_logging(settings.log_level)
|
||
|
||
journal = JournalService()
|
||
|
||
try:
|
||
init_schema()
|
||
except Exception as exc:
|
||
try:
|
||
journal.log_critical(
|
||
"app_bootstrap_failed",
|
||
f"Не удалось инициализировать схему БД: {exc}",
|
||
{
|
||
"env": settings.app_env,
|
||
"exchange_name": settings.exchange_name,
|
||
"default_symbol": settings.default_symbol,
|
||
},
|
||
)
|
||
except Exception:
|
||
pass
|
||
raise
|
||
|
||
trade_stream_runtime = (
|
||
build_trade_stream_production_runtime(settings)
|
||
)
|
||
|
||
bot = Bot(
|
||
token=settings.bot_token,
|
||
default=DefaultBotProperties(parse_mode=settings.bot_parse_mode),
|
||
)
|
||
|
||
NotificationTargetRegistry.set_bot(bot)
|
||
|
||
dispatcher = Dispatcher()
|
||
|
||
setup_routers(dispatcher)
|
||
|
||
try:
|
||
journal.log_info(
|
||
"app_started",
|
||
"Приложение запущено",
|
||
{
|
||
"env": settings.app_env,
|
||
"exchange_name": settings.exchange_name,
|
||
"default_symbol": settings.default_symbol,
|
||
"trade_stream_enabled": (
|
||
settings.trade_stream.enabled
|
||
),
|
||
},
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
return ApplicationComposition(
|
||
bot=bot,
|
||
dispatcher=dispatcher,
|
||
trade_stream_runtime=trade_stream_runtime,
|
||
)
|