A FastAPI-based backend for the Plant-Based project, providing search and vendor management capabilities.
- Search API: Endpoints for searching plant-based products.
- Vendor Management: CRUD operations for vendors (in progress).
- Health Checks: Basic and deep health monitoring.
- Infrastructure: Appwrite Functions.
src/adapters/rest/fastapi_server.py: FastAPI application definition and local server entry point.src/handlers/: API route definitions (search.py,health.py,vendor.py).src/schemas/: Pydantic models for request/response validation.src/adapters/: Data storage and third-party adapters.appwrite_functions/: Entry point and configuration for Appwrite deployment.
- Python 3.12+
- uv
-
Install dependencies:
uv sync
-
Run the application:
uv run uvicorn src.adapters.rest.fastapi_app:app --reload
The API will be available at http://127.0.0.1:8000.
FastAPI automatically generates interactive API documentation. You can access these endpoints in your browser:
- Swagger UI (OpenAPI 3.0):
http://127.0.0.1:8000/docs- Interactive API documentation with the ability to test endpoints directly. - ReDoc:
http://127.0.0.1:8000/redoc- Alternative API documentation view. - OpenAPI JSON Schema:
http://127.0.0.1:8000/openapi.json- Raw OpenAPI specification in JSON format.
The /docs endpoint provides a user-friendly interface to:
- Browse all available endpoints
- View request/response schemas
- Test API endpoints with custom parameters
- See detailed descriptions and parameter requirements
The project uses a structured logging system with different adapters based on the execution environment.
- DefaultLogger: Used during local development. Outputs to the console.
- AppwriteLogger: Used when running as an Appwrite Function. Forwards logs to the Appwrite console (
context.logandcontext.error). - SentryLogger: Integrates with Sentry for error tracking and performance monitoring.
Logging is configured via environment variables:
LOG_LEVEL: Sets the minimum log level for the application (e.g.,DEBUG,INFO,WARNING,ERROR). Defaults toINFO.SENTRY_DSN: The Data Source Name for Sentry integration. If provided,SentryLoggerwill initialize Sentry.
Note: By default, Sentry is configured to capture only WARNING and ERROR logs as events, even if LOG_LEVEL is set to INFO or DEBUG.
PostgreSQL migrations use Alembic for version control, allowing you to upgrade and downgrade database schema versions. A backup is automatically created before each migration.
export DEV_POSTGRES_HOST=localhost
export DEV_POSTGRES_PORT=5432
export DEV_POSTGRES_DB=pbapi_local
export DEV_POSTGRES_USER=postgres
export DEV_POSTGRES_PASSWORD=postgres(make sure PostgreSQL creds are set in the .env file)
Run migrations (upgrade to latest):
uv run python db_migration.py --env $ENV_NAME --db postgres --action upDowngrade one revision:
uv run python db_migration.py --env dev --db postgres --action downDowngrade to specific revision:
uv run python db_migration.py --env $ENV_NAME --db postgres --action down --revision legacy_000_schemaShow migration history:
uv run python db_migration.py --env $ENV_NAME --db postgres --action historyShow current revision:
uv run python db_migration.py --env $ENV_NAME --db postgres --action currentCreate new migration:
uv run python db_migration.py --env $ENV_NAME --db postgres --action create -m "add_new_column"Skip backup (not recommended for production):
uv run python db_migration.py --env $ENV_NAME --db postgres --action up --no-backupThe backup utility creates SQL dumps before migrations and can be used standalone:
# Create a backup
uv run python db_backup.py backup --env $ENV_NAME
# List available backups
uv run python db_backup.py list --env $ENV_NAME
# Restore from backup
uv run python db_backup.py restore --env $ENV_NAME --file backups/receipt_parser_dev_20260103_120000.sql
# Cleanup old backups (keep last 10)
uv run python db_backup.py cleanup --env $ENV_NAME --keep 10--env: Required. One ofprod,stage,dev,test,local--db: Database type.postgres(default:postgres)--action: Migration action. One ofup,down,history,current,create(default:up)--revision: Target revision for up/down (default:headfor up,-1for down)--message,-m: Migration message (required forcreateaction)--no-backup: Skip backup before migration
Run tests using pytest:
pytestThe project is deployed as an Appwrite Function.
To create a custom domain for the function, run:
appwrite proxy create-function-rule --domain pbapi.fra.appwrite.run --function-id pbapiThe project includes a GitHub Actions workflow in .github/workflows/deploy-appwrite.yml that automatically deploys the function to Appwrite on pushes to the main branch.
To enable this, you need to configure the following secrets in your GitHub repository:
APPWRITE_ENDPOINT: Your Appwrite API endpoint (e.g.,https://cloud.appwrite.io/v1).APPWRITE_PROJECT_ID: Your Appwrite Project ID.APPWRITE_API_KEY: An Appwrite API key with sufficient permissions to deploy functions.