From 1bda98f4ebcb968972702e17915c518b90f1baef Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 15 Nov 2023 13:08:36 +0100 Subject: [PATCH] build: add postgresql db config (with sqlalchemy) (#20) * Install psycopg2 & sqlalchemy dependencies * Setup DB * Connect to DB on startup * Update documentation * Remove .env.example file, simplify setup --- .env.example => .env | 7 +++++++ .gitignore | 3 --- INSTALL.md | 11 +++++++---- README.md | 3 ++- app/api.py | 13 +++++++++++++ app/config.py | 8 ++++++++ app/db.py | 19 +++++++++++++++++++ requirements.txt | 3 ++- 8 files changed, 58 insertions(+), 9 deletions(-) rename .env.example => .env (75%) create mode 100644 app/db.py diff --git a/.env.example b/.env similarity index 75% rename from .env.example rename to .env index 27e8d349..918feda6 100644 --- a/.env.example +++ b/.env @@ -15,3 +15,10 @@ SENTRY_DNS= # Log level to use, DEBUG by default in dev LOG_LEVEL=DEBUG + +# Postgres database +POSTGRES_DB_NAME=postgres +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_HOST=localhost +POSTGRES_PORT=5432 diff --git a/.gitignore b/.gitignore index 20c2e1ea..81075bb7 100644 --- a/.gitignore +++ b/.gitignore @@ -109,9 +109,6 @@ ENV/ env.bak/ venv.bak/ -# Environment variables -.env - # Spyder project settings .spyderproject .spyproject diff --git a/INSTALL.md b/INSTALL.md index 0397312f..0536c758 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 @@ -21,11 +23,12 @@ source venv/bin/activate # install pip install -r requirements.txt - -# environment variables -# make a copy of *.env.example* and rename it to *.env* ``` +### With Docker + +TODO + ## Run locally ``` diff --git a/README.md b/README.md index d893d538..f26e79ce 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 database ## How to install on your local machine diff --git a/app/api.py b/app/api.py index b6079a20..28e55e27 100644 --- a/app/api.py +++ b/app/api.py @@ -8,8 +8,10 @@ from openfoodfacts.utils import get_logger from app.config import settings +from app.db import session from app.utils import init_sentry + logger = get_logger(level=settings.log_level.to_int()) description = """ @@ -32,6 +34,17 @@ init_sentry(settings.sentry_dns) +@app.on_event("startup") +async def startup(): + global db + db = session() + + +@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() diff --git a/app/db.py b/app/db.py new file mode 100644 index 00000000..9a7dcce6 --- /dev/null +++ b/app/db.py @@ -0,0 +1,19 @@ +from sqlalchemy import create_engine +from sqlalchemy.engine import URL +from sqlalchemy.orm import sessionmaker + +from app.config import settings + + +url = URL.create( + drivername="postgresql", + database=settings.postgres_db_name, + username=settings.postgres_user, + password=settings.postgres_password, + host=settings.postgres_host, + port=settings.postgres_port, +) + +engine = create_engine(url) + +session = sessionmaker(autocommit=False, autoflush=False, bind=engine) diff --git a/requirements.txt b/requirements.txt index f6410469..47413198 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,9 @@ 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 +sqlalchemy==2.0.23 uvicorn==0.23.2