002-backend-framework.md 3.6 KB

ADR-002: Choix du framework backend - FastAPI vs NestJS

Statut

Accepté

Contexte

Remplacement du backend PHP par une API REST moderne. Comparaison entre :

  • FastAPI (Python)
  • NestJS (TypeScript/Node.js)

Décision

Choix : FastAPI (Python)

Justification

FastAPI - Avantages

  • Performance : Comparable à NestJS, async natif
  • Écosystème Python : Compatible avec outils système existants
  • Documentation auto : OpenAPI/Swagger intégré
  • Validation : Pydantic avec types Python
  • ARM support : Excellent sur Raspberry Pi
  • Scripts existants : Réutilisation logique bash → Python
  • Simplicité : Moins de boilerplate que NestJS

FastAPI - Architecture

from fastapi import FastAPI, BackgroundTasks
from pydantic import BaseModel

app = FastAPI(title="WaouW API")

class SiteCreate(BaseModel):
    domain: str
    cms_type: Literal["wordpress", "joomla", "prestashop"]
    ssl_enabled: bool = True

@app.post("/sites/")
async def create_site(site: SiteCreate, background_tasks: BackgroundTasks):
    """Créer un nouveau site de façon idempotente"""
    background_tasks.add_task(provision_site, site)
    return {"status": "provisioning", "site_id": generate_id()}

NestJS - Avantages évalués

  • TypeScript : Type safety end-to-end
  • Architecture : Patterns enterprise (DI, modules)
  • Écosystème : NPM packages
  • Performance : Très bon

NestJS - Inconvénients

  • Complexité : Plus de boilerplate
  • Ressources : Node.js + modules NPM lourds
  • Scripts : Intégration bash plus complexe
  • Learning curve : Plus technique

Conséquences

Stack technique

# requirements.txt
fastapi[all]==0.104.1
uvicorn[standard]==0.24.0
sqlalchemy==2.0.23
alembic==1.12.1
celery[redis]==5.3.4
pydantic==2.5.0
python-multipart==0.0.6
python-jose[cryptography]==3.3.0
passlib[bcrypt]==1.7.4

Structure API

backend/
├── app/
│   ├── __init__.py
│   ├── main.py              # FastAPI app
│   ├── core/
│   │   ├── config.py        # Settings
│   │   ├── security.py      # JWT, auth
│   │   └── database.py      # SQLAlchemy
│   ├── api/
│   │   ├── sites.py         # Sites endpoints
│   │   ├── devices.py       # Device management
│   │   └── auth.py          # Authentication
│   ├── models/
│   │   ├── site.py          # SQLAlchemy models
│   │   └── device.py
│   ├── schemas/
│   │   ├── site.py          # Pydantic schemas
│   │   └── device.py
│   └── services/
│       ├── provisioning.py  # Business logic
│       └── docker_manager.py
├── tests/
├── alembic/                 # DB migrations
└── scripts/                 # Deployment helpers

Intégration avec existant

  • Scripts bash : Appel via subprocess avec validation
  • Configuration : Migration des configs PHP → Pydantic
  • Base de données : Migration progressive MySQL → PostgreSQL

Performance RPi

  • ASGI : uvicorn avec workers optimisés
  • Memory : Profiling et limits
  • Startup : Lazy loading des modules

Alternatives considérées

  • Django REST Framework : Trop lourd pour l'usage
  • Express.js : Moins de features que NestJS
  • Go Gin : Performance excellente mais écosystème limité
  • Rust Axum : Trop complexe pour l'équipe

Date

2024-01-15

Auteurs

Architecture Team