The MyPacer API is the engine behind the MyPacer ecosystem. It serves two main functional domains:
- Pacing Engine: A mathematical core that generates dynamic pace tables based on user constraints (min/max pace, increments, custom distances).
- FFA Data Gateway: A bridge to French Athletics Federation data (
athle.fr).- Smart Search: Exposes a fuzzy-search endpoint over a local database of athletes (populated by the scraper) to solve the usability issues of the official website.
- Live Records: Fetches and parses athlete personal records (PRs) on-demand for real-time visualization.
- Docker and Docker Compose (recommended)
- Python 3.12+ (for local development without Docker)
- PostgreSQL (included in Docker setup)
Le dΓ©ploiement en production de l'API MyPacer est gΓ©rΓ© par le projet parent mypacer_infra qui orchestre l'ensemble des microservices MyPacer (API, frontend, base de donnΓ©es) via son propre docker-compose.yml.
Pour dΓ©ployer l'API en production :
- RΓ©fΓ©rez-vous au projet cmoron/mypacer_infra pour les instructions de dΓ©ploiement complet
- L'image Docker de production est disponible sur GHCR :
ghcr.io/cmoron/mypacer_api:latest-prod
Configuration requise (Γ dΓ©finir dans le .env du projet mypacer_infra) :
POSTGRES_DB=mypacer
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_secure_passwordFor development with automatic code reloading, use the dedicated development compose file.
Note for Linux/MacOS users: To avoid file permission issues between your host machine and the container, it's crucial to run the application with the same user ID (UID) and group ID (GID) as your current user. The Dockerfile is configured to accept these as build arguments.
Run the following command to start the development environment. It will build the image on first run, passing your user's UID/GID:
HOST_UID=$(id -u) HOST_GID=$(id -g) docker compose -f docker-compose.dev.yml up --buildOnce the container is built, you can start and stop it with:
# To start
HOST_UID=$(id -u) HOST_GID=$(id -g) docker compose -f docker-compose.dev.yml up
# To run in detached mode
HOST_UID=$(id -u) HOST_GID=$(id -g) docker compose -f docker-compose.dev.yml up -dThis configuration:
- Mounts your source code as a volume for live reloading.
- Runs uvicorn with the
--reloadflag. - Ensures the container user has the correct permissions on the mounted source code.
If you prefer to run the application without Docker:
- Clone the Repository:
git clone https://your-repository-url/mypacer_api.git
cd mypacer_api- Create and Activate a Virtual Environment:
For Linux/MacOS:
python3 -m venv venv
source venv/bin/activateFor Windows:
python -m venv venv
venv\Scripts\activate- Install Requirements:
pip install -r requirements.txt-
Set up PostgreSQL database and configure environment variables
-
Run the API:
uvicorn mypacer_api.main:app --reloadOr use the provided script:
./run.shThe API will be available at http://localhost:8000.
.github/workflows/ci.ymlexΓ©cute les tests avec PostgreSQL sur chaque push/PR..github/workflows/docker.ymlconstruit et pousse l'image multi-stage (cibleprod) vers GHCR :ghcr.io/cmoron/mypacer_api:latest-prod(tags semver/sha aussi).- Pour une construction locale :
docker build -t mypacer_api:dev --target dev . docker build -t mypacer_api:latest-prod --target prod .
Once the application is running, you can access:
- Interactive API documentation (Swagger UI): http://localhost:8000/docs
- Alternative API documentation (ReDoc): http://localhost:8000/redoc
The API provides the following endpoints:
- POST /generate_table: Generates a pace table based on the provided minimum pace, maximum pace, and increment values.
- Parameters:
min_pace,max_pace,increment,distances - Returns: A table of calculated times for each distance at each pace
- Parameters:
-
GET /get_athletes: Retrieves athlete information from the FFA database
- Query parameter:
name(athlete name to search for) - Returns: List of athletes matching the search
- Query parameter:
-
GET /get_athletes_from_db: Retrieves athlete information from the local database
- Query parameter:
name(athlete name to search for) - Returns: Athlete information from local database
- Query parameter:
-
GET /get_athlete_records: Retrieves athlete records from bases.athle.fr
- Query parameter:
ident(athlete ID) - Returns: Dictionary containing the athlete's records for various disciplines
- Query parameter:
- GET /database_status: Get information about the database state
- Returns: Number of clubs, number of athletes, and date of last update
Create a .env file in the project root with the following variables (or copy from .env.example):
POSTGRES_CONTAINER=mypacer-db
POSTGRES_DB=mypacer_db
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_secure_passwordSchema Ownership:
- β
Source of truth:
mypacer_infra/init-db/01-init-schema.sql(production/staging) - π Local copy:
db/init.sql(development only, for autonomous dev)
In development mode (docker-compose.dev.yml), the PostgreSQL database is automatically initialized with the db/init.sql script on first startup.
To reset your local development database:
# Stop containers and remove volumes
docker compose -f docker-compose.dev.yml down -v
# Restart (will reinitialize the schema)
HOST_UID=$(id -u) HOST_GID=$(id -g) docker compose -f docker-compose.dev.yml upNote: The db/init.sql file is a local copy for development autonomy. If the schema evolves, ensure it stays in sync with the reference in mypacer_infra.
The development environment automatically loads test data from db/02-test-data.sql on first startup:
- 15 clubs - Representative clubs from different regions of France
- ~50 athletes - Diverse profiles (male/female, different birth years, nationalities including FRA, MAR, GER, CGO)
This data is extracted from a production backup and allows you to:
- Test search functionality with realistic names
- Test filtering by gender, nationality, birth year
- Develop features without requiring the scraper to run
The test data includes:
- Clubs: CA MONTREUIL 93, CLERMONT AUVERGNE ATHLETISME, RACING CF (PARIS), etc.
- Athletes: ranging from 1934 to 1996 birth years
- Mix of ~35 male and ~15 female athletes
- International athletes (French, Moroccan, German, Congolese)
To run the test suite:
With Docker (development):
docker-compose -f docker-compose.dev.yml exec api pytestWithout Docker:
pytestEnsure that all tests pass successfully to confirm that the API is functioning as expected.
To generate the API documentation using Sphinx:
- Navigate to the docs directory:
cd docs- Run the following command to generate the documentation:
make html- The generated HTML documentation will be available in the
docs/_build/htmldirectory.
mypacer_api/
βββ mypacer_api/ # Main application package
β βββ main.py # FastAPI application entry point
β βββ models.py # Data models
β βββ dependencies.py # Dependency injection
β βββ core/ # Core modules (calculator, database, scrapper)
β βββ services/ # Business logic services
βββ db/ # Database files
β βββ init.sql # Database initialization script
βββ docs/ # Documentation and optimization guides
βββ tests/ # Test files
βββ Dockerfile # Docker image definition
βββ docker-compose.dev.yml # Docker services configuration (development)
βββ requirements.txt # Python dependencies
βββ README.md # This file
If port 8000 or 5432 is already in use, modify the port mappings in docker-compose.dev.yml:
ports:
- "8001:8000" # Use port 8001 on host insteadEnsure the PostgreSQL container is running and healthy:
docker compose -f docker-compose.dev.yml ps
docker compose -f docker-compose.dev.yml logs postgresIf you make changes to dependencies, rebuild the Docker image:
docker compose -f docker-compose.dev.yml build
docker compose -f docker-compose.dev.yml up -dFeel free to contribute to the MyPacer API. If you encounter any issues or have suggestions, please open an issue or a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.