- FastAPI + Python 3.12 backend - Meta WhatsApp Business API client (official) - OpenAI GPT-4o with function calling - RAG vector store with pgvector - ERPNext Frappe REST client - Celery + Redis async task queue - PostgreSQL with migrations (Alembic) - Docker Compose full stack - Enterprise logging, metrics, health checks
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
"""FastAPI dependency injection providers."""
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
from fastapi import Request
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from src.infrastructure.db import AsyncSessionLocal
|
|
from src.infrastructure.redis import RedisCache, get_redis
|
|
|
|
|
|
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
|
"""Yield a database session for FastAPI dependency injection."""
|
|
session = AsyncSessionLocal()
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
async def get_cache() -> AsyncGenerator[RedisCache, None]:
|
|
"""Yield a Redis cache instance."""
|
|
redis = await get_redis()
|
|
yield RedisCache(redis)
|
|
|
|
|
|
def get_client_ip(request: Request) -> str:
|
|
"""Extract client IP from request, handling proxies."""
|
|
forwarded = request.headers.get("x-forwarded-for")
|
|
if forwarded:
|
|
return forwarded.split(",")[0].strip()
|
|
return request.client.host if request.client else "unknown"
|