From 7ef1df4d65d6d0185f5911a3e3a4540bef5028d8 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Fri, 10 Nov 2023 00:38:47 +0100 Subject: [PATCH 1/3] Add psycopg2-binary dependency --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index f6410469..cc34518a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ fastapi==0.103.1 jinja2==3.1.2 openfoodfacts==0.1.10 peewee==3.17.0 +psycopg2-binary==2.9.9 pydantic-settings==2.0.3 requests==2.31.0 sentry-sdk[fastapi]==1.31.0 From e247d0a9a4c8aea1135f44e1af8399253370d61c Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Fri, 10 Nov 2023 00:39:52 +0100 Subject: [PATCH 2/3] Connect to DB on startup --- .env.example | 7 +++++++ app/api.py | 13 +++++++++++++ app/config.py | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/.env.example b/.env.example index 27e8d349..3b1a2c0b 100644 --- a/.env.example +++ b/.env.example @@ -15,3 +15,10 @@ SENTRY_DNS= # Log level to use, DEBUG by default in dev LOG_LEVEL=DEBUG + +# Postgres database +POSTGRES_DB_NAME=open_prices +POSTGRES_USER=open_prices_team +POSTGRES_PASSWORD=password +POSTGRES_HOST=localhost +POSTGRES_PORT=5432 diff --git a/app/api.py b/app/api.py index b6079a20..a2da9274 100644 --- a/app/api.py +++ b/app/api.py @@ -6,6 +6,7 @@ from fastapi.responses import PlainTextResponse from fastapi.templating import Jinja2Templates from openfoodfacts.utils import get_logger +from playhouse.postgres_ext import PostgresqlExtDatabase from app.config import settings from app.utils import init_sentry @@ -32,6 +33,18 @@ init_sentry(settings.sentry_dns) +@app.on_event("startup") +async def startup(): + global db + db = PostgresqlExtDatabase(settings.postgres_db_name, user=settings.postgres_user, password=settings.postgres_password, host=settings.postgres_host, port=settings.postgres_port) + db.connect() + + +@app.on_event("shutdown") +async def shutdown(): + db.close() + + @app.get("/", response_class=HTMLResponse) def main_page(request: Request): return templates.TemplateResponse( diff --git a/app/config.py b/app/config.py index c4ab79a0..d6a96af0 100644 --- a/app/config.py +++ b/app/config.py @@ -1,6 +1,7 @@ from enum import Enum from pydantic_settings import BaseSettings +from pydantic_settings import SettingsConfigDict class LoggingLevel(Enum): @@ -27,8 +28,15 @@ def to_int(self): class Settings(BaseSettings): + postgres_db_name: str + postgres_user: str + postgres_password: str + postgres_host: str + postgres_port: int = 5432 sentry_dns: str | None = None log_level: LoggingLevel = LoggingLevel.INFO + model_config = SettingsConfigDict(env_file=".env", extra="ignore") + settings = Settings() From d347d7b6f253ac361ea2f54a6bdbb3831c70b3de Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Fri, 10 Nov 2023 00:53:36 +0100 Subject: [PATCH 3/3] Update documentation --- INSTALL.md | 10 +++++++++- README.md | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 0397312f..16bb0c47 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -3,10 +3,12 @@ ## Prerequisites - Python 3.10 (lower version may be OK, but untested) -- pip +- Postgresql 13 (lower version may be OK, but untested) ## Setup +### Without Docker + ``` # clone repo git clone https://github.com/openfoodfacts/open-prices.git @@ -22,6 +24,12 @@ source venv/bin/activate # install pip install -r requirements.txt +# create Postgresql database +psql -c "CREATE USER open_prices_team WITH PASSWORD 'password'" +psql -c "CREATE DATABASE open_prices OWNER open_prices_team" +psql -c "GRANT ALL PRIVILEGES ON DATABASE open_prices to open_prices_team" +psql -c "ALTER USER open_prices_team CREATEROLE CREATEDB" + # environment variables # make a copy of *.env.example* and rename it to *.env* ``` diff --git a/README.md b/README.md index d893d538..fcd623e1 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ A REST API designed to interact with the Open Food Facts _Open Prices_ database. ## Dependencies -* Python 3.12 +* Python 3.10 * [FastAPI](https://fastapi.tiangolo.com/) framework +* Postgresql 13 ## How to install on your local machine