Architecture Backend
Documentation de l'architecture backend FastAPI.
Structure Applicative
backend/app/
├── main.py # Entry point FastAPI
├── config.py # Pydantic Settings
├── database.py # SQLAlchemy async setup
├── cache.py # Redis management
├── context.py # Tenant context
├── dependencies.py # FastAPI DI
│
├── models/ # 13 modèles ORM
├── schemas/ # 15 schemas Pydantic
├── routers/ # 11 routers API
├── services/ # 19 services métier
├── middleware/ # 3 middlewares
└── utils/ # Helpers
Flow d'une Requête
sequenceDiagram
participant Client
participant CORS
participant RateLimit
participant TenantCtx
participant Router
participant Service
participant DB
Client->>CORS: HTTP Request
CORS->>RateLimit: Check origin
RateLimit->>TenantCtx: Check quota
TenantCtx->>Router: Inject tenant_id
Router->>Service: Call service
Service->>DB: Query (with RLS)
DB-->>Service: Results
Service-->>Client: JSON Response
Middlewares
| Ordre | Middleware | Fonction |
| 1 | CORS | Vérifie les origines |
| 2 | Rate Limiter | 60 req/min, 100 burst |
| 3 | Tenant Context | Injecte tenant_id |
| 4 | Security Headers | OWASP headers |
| 5 | Metrics | Prometheus |
Services (19)
| Service | Responsabilité |
LLMRouter | Routage intelligent |
LLMClients | Clients Ollama/Claude/GPT |
RAGService | Embeddings + recherche |
MemoryService | Mémoire contextuelle |
DocumentService | Upload, parsing |
VisionService | Claude Vision |
SpeechService | TTS/STT |
AuthService | JWT, RBAC |
Patterns Utilisés
Repository Pattern
class ConversationRepository:
async def get_by_id(self, id: UUID) -> Optional[Conversation]:
result = await self.session.execute(
select(Conversation).where(Conversation.id == id)
)
return result.scalar_one_or_none()
Dependency Injection
@router.post("/conversations")
async def create(
body: ConversationCreate,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
service = ConversationService(db)
return await service.create(user, body.title)