Skip to content

Commit 708ae81

Browse files
Reading data from postgres
1 parent 708b927 commit 708ae81

File tree

11 files changed

+190
-2
lines changed

11 files changed

+190
-2
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,17 @@ flake8:
3636
run_all:
3737
make init ruff flake8 tests_unit tests_integration
3838

39+
docker_compose_pull:
40+
docker compose -f docker-compose.yml pull
41+
42+
docker_compose_start:
43+
docker compose -f docker-compose.yml up --build -d
44+
45+
docker_compose_stop:
46+
docker compose -f docker-compose.yml down -v
47+
48+
docker_compose_restart:
49+
make docker_compose_stop docker_compose_start
50+
3951
docker:
4052
docker build --pull -t python-fastapi -f Dockerfile .

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ Docker dependencies needs to be started using docker compose before running the
4646

4747
make fast_dev
4848

49-
http://localhost:8000
49+
http://localhost:8000/docs
50+
51+
http://localhost:8000/context/docs
5052

5153
### Run in production mode
5254

5355
make fast_run
5456

55-
http://localhost:8000
57+
http://localhost:80000/docs
58+
59+
http://localhost:8000/context/docs
5660

5761
### Running with docker
5862

app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Settings(BaseSettings):
1313
app_db_database: str = "myuser"
1414
app_db_user: str = "myuser"
1515
app_db_password: str = "superpassword"
16+
app_db_min_con: int = 5
17+
app_db_max_con: int = 20
1618
# db settings end
1719

1820

app/db/__init__.py

Whitespace-only changes.

app/db/database_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from databases import Database
2+
from app.config import settings
3+
4+
DATABASE_URL = f"postgresql://{settings.app_db_user}:{settings.app_db_password}@{settings.app_db_host}/{settings.app_db_database}"
5+
database = Database(
6+
DATABASE_URL, min_size=settings.app_db_min_con, max_size=settings.app_db_max_con
7+
)

app/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55
from contextlib import asynccontextmanager
66

77
from app.config import settings
8+
from app.db.database_config import database
89
from app.db_schema_migrations.yoyo_migration import apply_db_migrations
910
from app.logging.logging_config import setup_logging
1011
from app.middleware.process_time import ProcessTimeMiddleware
1112
from app.middleware.request_id import RequestIdMiddleware
1213
from app.routers.sample import router as sample_router
14+
from app.routers.customer import router as customer_router
1315

1416
# Initialize logging at application startup
1517
setup_logging(json_logs=settings.app_json_logs)
1618

1719
context = FastAPI(openapi_url=settings.app_open_api_url)
1820

1921
context.include_router(sample_router)
22+
context.include_router(customer_router)
2023

2124

2225
@asynccontextmanager
2326
async def lifespan(_app: FastAPI):
2427
if settings.app_db_migration_enabled:
2528
apply_db_migrations()
29+
await database.connect()
2630
yield
31+
await database.disconnect()
2732

2833

2934
app = FastAPI(openapi_url=settings.app_open_api_url, lifespan=lifespan)

app/model/__init__.py

Whitespace-only changes.

app/model/customer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import dataclasses
2+
3+
4+
@dataclasses.dataclass(frozen=True)
5+
class Customer:
6+
first_name: str
7+
last_name: str

app/routers/customer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi import APIRouter
2+
3+
from app.db.database_config import database
4+
from app.model.customer import Customer
5+
6+
router = APIRouter()
7+
8+
9+
def to_customer(row) -> Customer:
10+
return Customer(first_name=row.first_name, last_name=row.last_name)
11+
12+
13+
@router.get("/customers/", tags=["users"])
14+
async def read_customers() -> list[Customer]:
15+
rows = await database.fetch_all(query="select * from customers")
16+
customers: list[Customer] = list(map(lambda row: to_customer(row), rows))
17+
return customers

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"psycopg2-binary",
1010
"yoyo-migrations",
1111
"testcontainers[postgresql]",
12+
"databases[asyncpg]",
1213
]
1314

1415
[dependency-groups]

0 commit comments

Comments
 (0)