Skip to content

Commit 17c30b9

Browse files
committed
Implemented Dockerfile and docker-compose for api and db services. TODO: implement db migrations
1 parent 8cbe765 commit 17c30b9

File tree

7 files changed

+123
-16
lines changed

7 files changed

+123
-16
lines changed

backend/.dockerignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
*.egg-info/
8+
dist/
9+
build/
10+
*.egg
11+
12+
# Virtual environments
13+
venv/
14+
env/
15+
ENV/
16+
.venv
17+
18+
# Environment variables
19+
.env
20+
.env.local
21+
.env.*.local
22+
23+
# IDE
24+
.vscode/
25+
.idea/
26+
*.swp
27+
*.swo
28+
*~
29+
.DS_Store
30+
31+
# Testing
32+
.pytest_cache/
33+
.coverage
34+
htmlcov/
35+
.tox/
36+
.hypothesis/
37+
38+
# Documentation
39+
*.md
40+
docs/
41+
42+
# Git
43+
.git/
44+
.gitignore
45+
.gitattributes
46+
47+
# Docker
48+
Dockerfile
49+
.dockerignore
50+
docker-compose.yml
51+
52+
# CI/CD
53+
.github/
54+
.gitlab-ci.yml
55+
56+
# Logs
57+
*.log

backend/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.11-slim
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt .
6+
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
7+
8+
COPY app ./app
9+
10+
ENV PYTHONUNBUFFERED=1
11+
12+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
13+
14+

backend/app/core/config.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
from pathlib import Path
44

55
BASE_DIR = Path(__file__).resolve().parent.parent.parent
6-
load_dotenv(BASE_DIR / ".env")
6+
# load_dotenv(BASE_DIR / ".env")
77

8-
DB_USER = os.getenv("POSTGRES_USER")
9-
DB_PASSWORD = os.getenv("POSTGRES_PASSWORD")
8+
DB_USER = os.getenv("POSTGRES_USER", "eventrelayuser")
9+
DB_PASSWORD = os.getenv("POSTGRES_PASSWORD", "123")
1010
DB_NAME = os.getenv("POSTGRES_DB", "eventrelay")
11-
DB_HOST = os.getenv("POSTGRES_HOST", "LOCALHOST")
12-
DB_PORT = os.getenv("POSTGRES_PORT", 5432)
11+
DB_HOST = os.getenv("POSTGRES_HOST", "db")
12+
DB_PORT = os.getenv("POSTGRES_PORT", "5432")
13+
14+
print(f"DEBUG - DB Config: user={DB_USER}, db={DB_NAME}, host={DB_HOST}, port={DB_PORT}")
15+
16+
DATABASE_URL = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
17+
print(f"DEBUG - DATABASE_URL: {DATABASE_URL}")
1318

14-
DATABASE_URL = f"postgresql+asyncpg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

backend/app/db.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
from databases import Database
22
from app.core.config import DATABASE_URL
33

4-
database = Database(DATABASE_URL)
5-
6-
async def connect_db():
7-
await database.connect()
8-
9-
async def disconnect_db():
10-
await database.disconnect()
4+
database = Database(DATABASE_URL)

backend/app/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import FastAPI
22
from app.api.routes import events
3-
from app.db import connect_db, disconnect_db
3+
from app.db import database
44

55

66
app = FastAPI(title="EventRelay")
@@ -13,8 +13,8 @@ def health():
1313

1414
@app.on_event("startup")
1515
async def startup():
16-
await connect_db()
16+
await database.connect()
1717

1818
@app.on_event("shutdown")
1919
async def shutdown():
20-
await disconnect_db()
20+
await database.disconnect()

backend/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ uvicorn[standard]>=0.32.0
44
pydantic>=2.9.0
55

66
# Database
7+
databases[asyncpg]>=0.9.0
78
psycopg2-binary>=2.9.9
9+
asyncpg>=0.30.0
10+
11+
# Environment
12+
python-dotenv>=1.0.1
813

914
# Cache/Queue
1015
redis>=5.2.0

docker-compose.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
services:
2+
api:
3+
build: ./backend
4+
ports:
5+
- "8000:8000"
6+
environment:
7+
POSTGRES_USER: eventrelayuser
8+
POSTGRES_PASSWORD: 123
9+
POSTGRES_DB: eventrelay
10+
POSTGRES_HOST: db
11+
POSTGRES_PORT: 5432
12+
depends_on:
13+
db:
14+
condition: service_healthy
15+
16+
db:
17+
image: postgres:16
18+
environment:
19+
POSTGRES_USER: eventrelayuser
20+
POSTGRES_PASSWORD: 123
21+
POSTGRES_DB: eventrelay
22+
ports:
23+
- "5432:5432"
24+
volumes:
25+
- postgres_data:/var/lib/postgresql/data
26+
healthcheck:
27+
test: ["CMD-SHELL", "pg_isready -U eventrelayuser -d eventrelay"]
28+
interval: 5s
29+
timeout: 5s
30+
retries: 5
31+
32+
volumes:
33+
postgres_data:

0 commit comments

Comments
 (0)