camptocamp.org API developer manual
There are two ways to set up your development environment:
- Dev Container — the recommended approach; everything runs inside a pre-configured container, no local Python install required.
- Manual setup — set up Python, services, and configuration by hand on your host machine.
Note: For details on the "Mobilité douce" (public transport) features, see SMART-ORIGIN-README.md.
A Dev Container configuration is provided in the .devcontainer/ directory. It gives you a fully configured development environment with all companion services (PostgreSQL/PostGIS, Elasticsearch, Redis) running as side containers.
- Docker Desktop (or a compatible Docker engine with Docker Compose)
- VS Code with the Dev Containers extension
-
Clone the repository and open it in VS Code:
git clone https://github.com/c2corg/v6_api cd v6_api code .
-
When prompted, click "Reopen in Container" — or open the command palette (
Ctrl+Shift+P/Cmd+Shift+P) and select:Dev Containers: Reopen in Container -
Wait for the container to build and the
postCreateCommandsetup script to finish. On the first run this takes a few minutes — it will:- Install all Python dependencies
- Generate the configuration files
- Create and initialize both the dev and test databases
- Populate the Elasticsearch indexes
-
You're ready to go! See Running the API, Running tests, and Linting below.
| Service | Hostname | Port |
|---|---|---|
| PostgreSQL | postgresql |
5432 |
| Elasticsearch | elasticsearch |
9200 |
| Redis | redis |
6379 |
| API (when run) | localhost |
6543 |
All ports are forwarded to your host machine automatically.
If dependencies or the Dockerfile change, rebuild via the command palette:
Dev Containers: Rebuild Container
Or to re-run the setup script without rebuilding:
bash .devcontainer/setup.shFor more details, see .devcontainer/README.md.
- A computer running Linux or Windows (on Windows, take a look at WSL, it will make your developer life easier). Maybe MacOs can work, but it was not tested.
- Python 3.9.x installed with pip and the virtualenv package (currently, it will not work with python 3.10+)
- On Ubuntu, you can install it from the deadsnakes repository if your embedded python version is too recent (https://askubuntu.com/questions/1318846/how-do-i-install-python-3-9)
- On Windows, use Microsoft Store to install it (https://apps.microsoft.com/detail/9p7qfqmjrfp7)
- A docker environment with docker compose plugin (either docker desktop or a manual installation)
- A development IDE is not mandatory, but highly recommended (pycharm, vscode...)
- If you have GNU make installed, you can use it to run all the commands below to initialize and run your development environment (run
make helpto see what can be done) instead of running them "by hand"
- Clone the git repository
- Cd inside your cloned repo
- Create a virtual environment in it:
python3.9 -m venv venv- Activate the virtual environment (
⚠️ Do not forget to activate it in all terminal instances you intend to use)
# Linux (or WSL)
source venv/bin/activate
# Or Windows Powershell
.\venv\Scripts\Activate.ps1- Install the API and its python dependencies in the virtual environment with the following command:
pip install -e ".[dev]"- Start the necessary tools (postgres database, redis, and elasticsearch) in docker containers:
docker compose up -d- Go to the config directory and copy the
env.local.sampletoenv.local - You should not need to modify it as it is already filled with the necessary values to be compatible with the docker compose defaults but if necessary you can
- Update the
*.inifile:
# Linux or WSL
./scripts/env_replace config/env.default config/env.dev config/env.local < alembic.ini.in > alembic.ini
./scripts/env_replace config/env.default config/env.dev config/env.local < common.ini.in > common.ini
./scripts/env_replace config/env.default config/env.dev config/env.local < development.ini.in > development.ini
# Or Windows Powershell (Yes you should really have a look at WSL)
Get-Content alembic.ini.in | cmd /c "python .\scripts\env_replace config/env.default config/env.dev config/env.local > alembic.ini"
Get-Content common.ini.in | cmd /c "python .\scripts\env_replace config/env.default config/env.dev config/env.local > common.ini"
Get-Content development.ini.in | cmd /c "python .\scripts\env_replace config/env.default config/env.dev config/env.local > development.ini"- Initialize database:
docker compose exec -u postgres -T postgresql /v6_api/scripts/database/create_schema.sh
initialize_c2corg_api_db development.ini- Initialize test database:
docker compose exec -u postgres -T postgresql /v6_api/scripts/database/create_test_schema.sh- Initialize elastic search indexes
fill_es_index development.ini- Make sure the postgres redis and elasticsearch docker containers are running
docker ps- If you do not see them running, restart them (without the initialization steps that should have been done just once when setting up the environment):
docker compose up -d- Run the API (this command will not exit so you should run it in a separate terminal):
pserve development.ini --reload- Run background jobs (run each of these commands in a separate terminal, as they will not exit):
python c2corg_api/scripts/es/syncer.py development.ini
# Does not work in Windows, as it uses signal.pause() which is only available in unix based OSes (Did I already tell you that you should really look at WSL ?)
python c2corg_api/scripts/jobs/scheduler.py development.ini- To check that everything is OK, go to http://localhost:6543/health in your browser
- Update the test.ini file:
# Linux or WSL
./scripts/env_replace config/env.default config/env.dev config/env.local < test.ini.in > test.ini
# Or Windows Powershell
Get-Content test.ini.in | cmd /c "python .\scripts\env_replace config/env.default config/env.dev config/env.local > test.ini"
- To run tests , you can either run them directly with pytest (Again, it looks like some tests will not pass in Windows):
pytest- Or directly in your favorite development IDE (pycharm, vscode...)
- Run the linter:
flake8 c2corg_api es_migration- Flush redis cache:
python c2corg_api/scripts/redis-flushdb.py development.ini