61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from app.config import Settings
|
|
from app.db import Database
|
|
from app.repositories.applications import ApplicationRepository
|
|
from app.repositories.chats import ChatRepository
|
|
from app.repositories.users import UserRepository
|
|
from app.services.application_service import ApplicationService
|
|
from app.services.external_client import ExternalTicketClient
|
|
from app.services.request_state import MemoryRequestStateStore, RedisRequestStateStore, RequestStateStore
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Container:
|
|
settings: Settings
|
|
db: Database
|
|
applications: ApplicationRepository
|
|
chats: ChatRepository
|
|
users: UserRepository
|
|
app_service: ApplicationService
|
|
request_state: RequestStateStore
|
|
|
|
|
|
async def build_container(settings: Settings) -> Container:
|
|
db = Database(settings.db_path)
|
|
applications = ApplicationRepository(db)
|
|
chats = ChatRepository(db)
|
|
users = UserRepository(db)
|
|
external = ExternalTicketClient(settings.external_api_create_url_with_key)
|
|
app_service = ApplicationService(
|
|
repo=applications,
|
|
external_client=external,
|
|
channel_local_enabled=settings.channel_local_enabled,
|
|
channel_external_enabled=settings.channel_external_enabled,
|
|
)
|
|
|
|
request_state: RequestStateStore
|
|
if settings.context_backend == "redis":
|
|
try:
|
|
from redis.asyncio import Redis
|
|
|
|
redis_client = Redis.from_url(settings.redis_url, decode_responses=True)
|
|
await redis_client.ping()
|
|
request_state = RedisRequestStateStore(redis_client)
|
|
except Exception:
|
|
request_state = MemoryRequestStateStore()
|
|
else:
|
|
request_state = MemoryRequestStateStore()
|
|
|
|
return Container(
|
|
settings=settings,
|
|
db=db,
|
|
applications=applications,
|
|
chats=chats,
|
|
users=users,
|
|
app_service=app_service,
|
|
request_state=request_state,
|
|
)
|
|
|