From 07829fa16ec978546916e6adcc9e86a518c7efd1 Mon Sep 17 00:00:00 2001 From: Zayden Jung Date: Fri, 15 May 2026 21:17:21 +0800 Subject: [PATCH] connect to database; separate dev and prod DATABASE_URL --- .gitignore | 2 ++ Dockerfile | 9 +++++++++ db.py | 8 ++++++++ docker-compose.yml | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 Dockerfile create mode 100644 db.py create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index 505a3b1..898e654 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ wheels/ # Virtual environments .venv + +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c504d0d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.14-slim + +WORKDIR /app + +COPY . . + +RUN pip install --no-cache-dir fastapi uvicorn sqlalchemy psycopg2-binary httpx python-jose + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/db.py b/db.py new file mode 100644 index 0000000..7ebdf59 --- /dev/null +++ b/db.py @@ -0,0 +1,8 @@ +import os +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +DATABASE_URL = os.getenv("DATABASE_URL") + +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(bind=engine) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cbd7c28 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: "3.9" + +services: + db: + image: postgres:16-alpine + restart: always + environment: + POSTGRES_USER: ${SERVICE_USER_POSTGRESQL} + POSTGRES_PASSWORD: ${SERVICE_PASSWORD_POSTGRESQL} + POSTGRES_DB: roleplay + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: + - CMD-SHELL + - 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}' + interval: 5s + timeout: 20s + retries: 10 + + app: + build: . + depends_on: + - db + environment: + DATABASE_URL: postgresql://${SERVICE_USER_POSTGRESQL}:${SERVICE_PASSWORD_POSTGRESQL}@db:5432/roleplay + GOOGLE_CLIENT_ID: your_client_id + GOOGLE_CLIENT_SECRET: your_secret + ports: + - "8000:8000" + +volumes: + postgres_data: \ No newline at end of file