Skip to content

Commit

Permalink
Make database url configurable (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
akeamc authored Jan 29, 2025
1 parent 1fc54df commit a87a5da
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ RUN sed -i '1s;^;host all postgres samenet trust\n;' /etc/postgresql/16/main/pg_
# python venv must be installed through apt
RUN apt install -y python3.11 python3.11-venv sudo git

ENV ENV=development
ENV ENVIRONMENT=development
ENV DATABASE_URL="postgresql+psycopg://postgres:password@localhost:5432/postgres"

# In prod we must NOT run our container with root user, security risk.
# In the future I think we should use non-root user event in dev to make environment similar to prod
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
15 changes: 4 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
FROM debian:12.7
FROM python:3.12-slim

ENV ENVIRONMENT=production

ARG USERNAME=deployuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN apt update -y && \
apt install -y postgresql-common && \
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y && \
apt install -y postgresql-16 sed python3.11 python3.11-venv sudo git && \
sed -i '1s;^;host all postgres samenet md5\n;' /etc/postgresql/16/main/pg_hba.conf && \
apt clean && rm -rf /var/lib/apt/lists/*

RUN groupadd --gid $USER_GID $USERNAME && \
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME && \
apt-get update && \
Expand All @@ -24,12 +17,12 @@ USER $USERNAME

WORKDIR /app

RUN python3.11 -m venv venv
RUN python3 -m venv venv

COPY . .

RUN . venv/bin/activate && pip install --no-cache-dir -r requirements.txt
RUN . venv/bin/activate && pip install --no-cache-dir -r requirements.txt && pip install psycopg2-binary

EXPOSE 8000

CMD ["uvicorn", "main:app" ,"--host", "0.0.0.0", "--port", "8000"]
CMD ["venv/bin/uvicorn", "main:app" ,"--host", "0.0.0.0", "--port", "8000"]
3 changes: 2 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from sqlalchemy.orm import sessionmaker, Session
from db_models import base_model
from db_models import *
import os


# SQLALCHEMY_DATABASE_URL = "sqlite:///./database.sqlite"
SQLALCHEMY_DATABASE_URL = "postgresql+psycopg://postgres:password@localhost:5432/postgres"
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL")

engine = create_engine(SQLALCHEMY_DATABASE_URL)
session_factory = sessionmaker(engine, expire_on_commit=False)
Expand Down
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def generate_unique_id(route: APIRoute):

@asynccontextmanager
async def lifespan(app: FastAPI):
if os.getenv("ENV") == "development":
if os.getenv("ENVIRONMENT") == "development":
# Not needed if you setup a migration system like Alembic
init_db()
with session_factory() as db:
Expand All @@ -29,7 +29,7 @@ async def lifespan(app: FastAPI):


# No Swagger/OpenAPI page for production
no_docs = os.getenv("ENV") == "production"
no_docs = os.getenv("ENVIRONMENT") == "production"

dev_origins = [
"http://localhost",
Expand All @@ -43,7 +43,7 @@ async def lifespan(app: FastAPI):
generate_unique_id_function=generate_unique_id,
)

if os.getenv("ENV") == "development":
if os.getenv("ENVIRONMENT") == "development":
app.add_middleware(
CORSMiddleware,
allow_origins=dev_origins,
Expand Down

0 comments on commit a87a5da

Please sign in to comment.