Skip to content

openfoodfacts/folksonomy_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Folksonomy API

A lightweight REST API designed for the Open Food Facts Folksonomy Engine.

Contributor's Guide

Check out our Contributor's Guide. Feel free to improve it or ask questions!

Dependencies

  • Language: Python 3.x
  • Framework: FastAPI
  • Database: PostgreSQL

Development

You should create unit tests for each new feature or API change (see test_main.py).

To run tests, simply launch:

PYTHONASYNCIODEBUG=1 pytest tests/ folksonomy/
  • The PYTHONASYNCIODEBUG=1 environment variable is important to ensure there are no pending asyncio tasks (a sign of potential issues).
  • Warning: Running tests will wipe the database. Do not run tests in production.

Docker Setup

Using Docker is the easiest way to get started with the Folksonomy API. It requires minimal setup and ensures a consistent development environment.

Requirements

  • Docker 17.06.0+
  • Docker Compose 1.21.0+

Quick Start

  1. Clone the repository:

    git clone https://github.com/openfoodfacts/folksonomy_api.git
    cd folksonomy_api
  2. Copy the example settings file:

    cp local_settings_docker_example.py local_settings.py
  3. Create an environment configuration file:

    cp .env.example .env
  4. Start the services:

    docker compose up -d
  5. Initialize the database (necessary on the first run or after migrations):

    docker exec -it folksonomy_api-folksonomy_api-1 python db-migration.py
  6. Access the API:

  7. Stop the services:

    docker compose down

Configuration

The Docker setup uses environment variables defined in docker-compose.yml. You can modify these as needed:

  • POSTGRES_USER: Database username
  • POSTGRES_PASSWORD: Database password
  • POSTGRES_DATABASE: Database name
  • POSTGRES_HOST: Database host (default: db)

Additional settings (such as authentication) can be configured in local_settings.py.

Note: The PostgreSQL service uses port 5433 to avoid conflicts with any local PostgreSQL installations.

Working with the Database

To connect to the PostgreSQL database inside the Docker container:

docker compose exec db psql -U folksonomy -d folksonomy

Traditional Setup

If you prefer installing everything directly on your machine (without Docker):

  1. Install Python 3.9+
  2. Install Poetry
  3. Install PostgreSQL 13+
  4. Follow the instructions in INSTALL.md to install the requirements and create a database user.
  5. Copy local_settings_example.py and rename it to local_settings.py.
  6. Update the parameters in local_settings.py as needed.
  7. That's it!

Generating an OpenAPI Document

FastAPI uses OpenAPI (formerly Swagger) and JSON Schema.

You can generate the OpenAPI JSON document in two ways:

./generate_openapi_json.py

If running inside Docker:

docker compose exec api python generate_openapi_json.py

Code Style

This project uses Ruff for linting and code formatting.

  • We recommend using pre-commit hooks for automatic linting, but it's optional.
  • See CONTRIBUTING.md for more details on code style and linting.

Deployment

Deployment information is available here: Open Food Facts - Folksonomy Deployment