Endpoints, configuration, tests
Les endpoints
Adresse de base : https://api.trinity.jostud.com (prod), https://api.dev.trinity.jostud.com (dev).
| Méthode | Adresse | Corps attendu | Réponse |
|---|---|---|---|
GET | /health | — | 200 {"status": "ok"} |
GET | /api/products/ | — | 200 tableau de produits, trié par id |
POST | /api/products/ | produit (sans id ni dates) | 201 produit créé, ou 400 |
GET | /api/products/{id}/ | — | 200 produit, ou 404 |
PUT | /api/products/{id}/ | produit complet | 200, 400 ou 404 |
PATCH | /api/products/{id}/ | champs à modifier | 200, 400 ou 404 |
DELETE | /api/products/{id}/ | — | 204, ou 404 |
GET | /admin/ | — | interface d'administration Django |
Un produit en JSON :
json
{
"id": 1,
"name": "Pommes",
"description": "Pommes bio",
"price": "2.50",
"stock": 10,
"created_at": "2026-11-09T09:46:22.076414Z",
"updated_at": "2026-11-09T09:46:22.076448Z"
}Le prix est une chaîne : c'est un décimal exact. Les règles de chaque champ sont dans Erreurs et validation.
Tester à la main
Ouvre /api/products/ dans un navigateur : DRF affiche une page navigable, avec un formulaire pour envoyer un POST.
Les variables d'environnement
| Variable | Rôle | Défaut | dev | prod |
|---|---|---|---|---|
DEBUG | Mode debug | True | True | False |
SECRET_KEY | Clé de signature Django | clé de dev si DEBUG | variable CI DJANGO_SECRET_KEY | variable CI DJANGO_SECRET_KEY |
ALLOWED_HOSTS | Domaines acceptés, séparés par des virgules | localhost,127.0.0.1 | api.dev.trinity.jostud.com | api.trinity.jostud.com,localhost |
DB_HOST | Hôte PostgreSQL ; vide = SQLite | vide | db | db |
DB_NAME | Nom de la base | — | trinity | trinity |
DB_USER | Utilisateur | — | trinity | trinity |
DB_PASSWORD | Mot de passe | — | variable CI POSTGRES_PASSWORD | variable CI POSTGRES_PASSWORD |
DB_PORT | Port | 5432 | 5432 | 5432 |
localhost est dans ALLOWED_HOSTS en prod pour que le healthcheck Docker (curl http://localhost:8000/health) soit accepté.
sh
DEBUG=True
SECRET_KEY=dev-secret-key-change-in-production
ALLOWED_HOSTS=localhost,127.0.0.1
# PostgreSQL
DB_NAME=trinity_db
DB_USER=trinity_user
DB_PASSWORD=trinity_pass
DB_HOST=localhost
DB_PORT=5432Les versions
| Outil | Version |
|---|---|
| Python (image Docker) | 3.11, slim |
| Django | 4.2 (>=4.2,<5.0) |
| Django REST Framework | dernière version au moment du build |
| Gunicorn, psycopg2-binary, pytest, pytest-django, pytest-cov, flake8 | dernières versions au moment du build |
txt
Django>=4.2,<5.0
djangorestframework
psycopg2-binary
python-dotenv
gunicorn
pytest
pytest-django
pytest-cov
flake8Versions non figées
À part Django, aucune version n'est fixée : deux builds à une semaine d'écart peuvent installer des versions différentes. Voir Limites.
Les tests
sh
cd api
DB_HOST= pytest --cov=products --cov-report=term
flake8 products trinity_apiDans le pipeline, api:test produit :
| Fichier | Format | Affiché dans GitLab |
|---|---|---|
api/report.xml | l'onglet Tests | |
api/coverage.xml | Cobertura | la dans le diff |
Les jobs du pipeline
| Job | Stage | Quand | Ce qu'il fait |
|---|---|---|---|
api:lint | lint | à chaque push qui modifie api/ | flake8 products trinity_api |
api:test | test | à chaque push qui modifie api/ | pytest + rapports |
api:build | build | à chaque push sur develop ou main | construit Dockerfile.prod, pousse api:<sha> |
yml
api:lint:
stage: lint
image: python:3.11-slim
script:
- pip install --no-cache-dir flake8
- cd api
- flake8 products trinity_api
rules:
- changes:
- api/**/*
api:test:
stage: test
image: python:3.11-slim
variables:
DB_HOST: ""
script:
- pip install --no-cache-dir -r api/requirements.txt
- cd api
- pytest --junitxml=report.xml --cov=products --cov-report=term --cov-report=xml:coverage.xml
artifacts:
when: always
reports:
junit: api/report.xml
coverage_report:
coverage_format: cobertura
path: api/coverage.xml
rules:
- changes:
- api/**/*
api:build:
stage: build
image: docker:27.5.1-cli
services:
- name: docker:27.5.1-dind
alias: docker
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: /certs
DOCKER_TLS_VERIFY: "1"
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
script:
- docker build --pull --file api/Dockerfile.prod --tag "$CI_REGISTRY_IMAGE/api:$CI_COMMIT_SHORT_SHA" api
- docker push "$CI_REGISTRY_IMAGE/api:$CI_COMMIT_SHORT_SHA"
rules:
- if: '$CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "main"'Les deux Dockerfiles
Dockerfile.dev | Dockerfile.prod | |
|---|---|---|
| Serveur | runserver | Gunicorn, 3 workers |
| Étapes | une seule | deux : builder → image finale |
| Utilisateur | root | appuser (non root) |
| Au démarrage | migrations lancées par Compose | entrypoint.sh : migrate + collectstatic |
| Healthcheck | non | curl /health toutes les 30 s |