Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add postgresql db config (with peewee) #19

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
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=open_prices
POSTGRES_USER=open_prices_team
POSTGRES_PASSWORD=password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
10 changes: 9 additions & 1 deletion 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 @@ -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*
```
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 13

## 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 @@ -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
Expand All @@ -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(
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()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading