Endpoints, configuration, tests
Endpoints
Base address: https://api.trinity.jostud.com (prod), https://api.dev.trinity.jostud.com (dev).
| Method | Address | Expected body | Response |
|---|---|---|---|
GET | /health | — | 200 {"status": "ok"} |
GET | /api/products/ | — | 200 array of products, sorted by id |
POST | /api/products/ | product (without id or dates) | 201 created product, or 400 |
GET | /api/products/{id}/ | — | 200 product, or 404 |
PUT | /api/products/{id}/ | full product | 200, 400 or 404 |
PATCH | /api/products/{id}/ | fields to change | 200, 400 or 404 |
DELETE | /api/products/{id}/ | — | 204, or 404 |
GET | /admin/ | — | Django admin interface |
A product as 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"
}The price is a string: it is an exact decimal. The rules for each field are in Errors and validation.
Test by hand
Open /api/products/ in a browser: DRF shows a browsable page, with a form to send a POST.
Environment variables
| Variable | Role | Default | dev | prod |
|---|---|---|---|---|
DEBUG | Debug mode | True | True | False |
SECRET_KEY | Django signing key | dev key if DEBUG | CI variable DJANGO_SECRET_KEY | CI variable DJANGO_SECRET_KEY |
ALLOWED_HOSTS | Accepted domains, comma-separated | localhost,127.0.0.1 | api.dev.trinity.jostud.com | api.trinity.jostud.com,localhost |
DB_HOST | PostgreSQL host; empty = SQLite | empty | db | db |
DB_NAME | Database name | — | trinity | trinity |
DB_USER | User | — | trinity | trinity |
DB_PASSWORD | Password | — | CI variable POSTGRES_PASSWORD | CI variable POSTGRES_PASSWORD |
DB_PORT | Port | 5432 | 5432 | 5432 |
localhost is in ALLOWED_HOSTS in prod so the Docker health check (curl http://localhost:8000/health) is accepted.
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=5432Versions
| Tool | Version |
|---|---|
| Python (Docker image) | 3.11, slim |
| Django | 4.2 (>=4.2,<5.0) |
| Django REST Framework | latest version at build time |
| Gunicorn, psycopg2-binary, pytest, pytest-django, pytest-cov, flake8 | latest versions at build time |
txt
Django>=4.2,<5.0
djangorestframework
psycopg2-binary
python-dotenv
gunicorn
pytest
pytest-django
pytest-cov
flake8Unpinned versions
Apart from Django, no version is pinned: two builds a week apart may install different versions. See Limits.
Tests
sh
cd api
DB_HOST= pytest --cov=products --cov-report=term
flake8 products trinity_apiIn the pipeline, api:test produces:
| File | Format | Shown in GitLab |
|---|---|---|
api/report.xml | the Tests tab | |
api/coverage.xml | Cobertura | in the diff |
Pipeline jobs
| Job | Stage | When | What it does |
|---|---|---|---|
api:lint | lint | on every push that changes api/ | flake8 products trinity_api |
api:test | test | on every push that changes api/ | pytest + reports |
api:build | build | on every push to develop or main | builds Dockerfile.prod, pushes 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"'The two Dockerfiles
Dockerfile.dev | Dockerfile.prod | |
|---|---|---|
| Server | runserver | Gunicorn, 3 workers |
| Stages | one | two: builder → final image |
| User | root | appuser (non-root) |
| At startup | migrations run by Compose | entrypoint.sh: migrate + collectstatic |
| Health check | no | curl /health every 30 s |