Skip to content

Commit fcc326c

Browse files
committed
feat(deploy): prepare application for Render deployment with PostgreSQL and Docker
1 parent 58f5630 commit fcc326c

6 files changed

Lines changed: 119 additions & 1 deletion

File tree

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.git
2+
.venv
3+
.pytest_cache
4+
.mypy_cache
5+
.ruff_cache
6+
__pycache__
7+
db.sqlite
8+
.env
9+
tests
10+
docs
11+
AGENTS.md
12+
README.md

Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Use the official uv image for building
2+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Enable bytecode compilation
8+
ENV UV_COMPILE_BYTECODE=1
9+
10+
# Copy project files
11+
COPY pyproject.toml uv.lock ./
12+
13+
# Install dependencies without the project itself
14+
RUN uv sync --frozen --no-install-project --no-dev
15+
16+
# Final stage
17+
FROM python:3.12-slim-bookworm
18+
19+
WORKDIR /app
20+
21+
# Copy the environment from the builder
22+
COPY --from=builder /app/.venv /app/.venv
23+
24+
# Add the virtual environment to the PATH
25+
ENV PATH="/app/.venv/bin:$PATH"
26+
27+
# Copy the rest of the application
28+
COPY . .
29+
30+
# Expose the port
31+
EXPOSE 8000
32+
33+
# Start command: run migrations then the app
34+
CMD alembic upgrade head && uvicorn src.main:app --host 0.0.0.0 --port 8000

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires-python = ">=3.12"
77
dependencies = [
88
"aiosqlite>=0.22.1",
99
"alembic>=1.18.4",
10+
"asyncpg>=0.31.0",
1011
"fastapi[standard]>=0.135.1",
1112
"pwdlib[argon2]>=0.3.0",
1213
"pyjwt>=2.12.1",

render.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
# Web Service
3+
- type: web
4+
name: fastapibanksystem
5+
runtime: docker
6+
plan: free
7+
region: oregon
8+
envVars:
9+
- key: DATABASE_URL
10+
fromDatabase:
11+
name: fastapibank-db
12+
property: connectionString
13+
- key: SECRET_KEY
14+
generateValue: true
15+
- key: ENVIRONMENT
16+
value: production
17+
18+
databases:
19+
- name: fastapibank-db
20+
plan: free
21+
region: oregon

src/core/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
including database URLs, environment contexts, and security keys.
55
"""
66

7-
from pydantic import Field
7+
from pydantic import Field, field_validator
88
from pydantic_settings import BaseSettings, SettingsConfigDict
99

1010

@@ -24,6 +24,14 @@ class Settings(BaseSettings):
2424

2525
database_url: str = Field(default="sqlite+aiosqlite:///db.sqlite")
2626

27+
@field_validator("database_url", mode="before")
28+
@classmethod
29+
def assemble_db_url(cls, v: str) -> str:
30+
"""Fix postgres scheme for SQLAlchemy compatibility."""
31+
if v and v.startswith("postgres://"):
32+
return v.replace("postgres://", "postgresql+asyncpg://", 1)
33+
return v
34+
2735
environment: str = Field(default="production")
2836

2937
secret_key: str = Field(

uv.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)