Skip to content

Commit

Permalink
build: add postgresql db config (with sqlalchemy) (#20)
Browse files Browse the repository at this point in the history
* Install psycopg2 & sqlalchemy dependencies

* Setup DB

* Connect to DB on startup

* Update documentation

* Remove .env.example file, simplify setup
  • Loading branch information
raphodn authored Nov 15, 2023
1 parent 019b054 commit 1bda98f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .env.example → .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ ENV/
env.bak/
venv.bak/

# Environment variables
.env

# Spyder project settings
.spyderproject
.spyproject
Expand Down
11 changes: 7 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

```
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum

from pydantic_settings import BaseSettings
from pydantic_settings import SettingsConfigDict


class LoggingLevel(Enum):
Expand All @@ -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()
19 changes: 19 additions & 0 deletions app/db.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1bda98f

Please sign in to comment.