Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "FastAPI Jinja2 Postgres WebApp",
"image": "mcr.microsoft.com/devcontainers/python:3.13-bookworm",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
},

"runArgs": [
"--add-host=host.docker.internal:host-gateway"
],

"forwardPorts": [8000, 5432],
"portsAttributes": {
"8000": { "label": "FastAPI", "onAutoForward": "openBrowser" },
"5432": { "label": "PostgreSQL" }
},

"initializeCommand": "bash \"${localWorkspaceFolder}/.devcontainer/ensure-env.sh\"",
"postStartCommand": "docker compose -f .devcontainer/docker-compose.yml up -d",
"postCreateCommand": "bash .devcontainer/init-env.sh && curl -LsSf https://astral.sh/uv/install.sh | sh && ~/.local/bin/uv sync",

"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
}
}
15 changes: 15 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
db:
image: postgres:16
restart: unless-stopped
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
POSTGRES_DB: ${DB_NAME:-fastapi-jinja2-postgres-webapp}
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:
17 changes: 17 additions & 0 deletions .devcontainer/ensure-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail

# Run from repo root
cd "$(dirname "${BASH_SOURCE[0]}")/.."

# Ensure a .env exists BEFORE docker compose evaluates env_file
if [ ! -f ".env" ]; then
if [ -f ".env.example" ]; then
cp .env.example .env || true
else
touch .env
fi
fi

echo ".env ensured for compose evaluation."

46 changes: 46 additions & 0 deletions .devcontainer/init-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail

# Run from repo root
cd "$(dirname "${BASH_SOURCE[0]}")/.."

# Ensure a working .env exists
if [ ! -f ".env" ]; then
if [ -f ".env.example" ]; then
cp .env.example .env || true
else
touch .env
fi
fi

# Ensure DB_HOST points to host.docker.internal for DooD sibling container access
if grep -q '^DB_HOST=' .env; then
sed -i 's/^DB_HOST=.*/DB_HOST=host.docker.internal/' .env
else
echo 'DB_HOST=host.docker.internal' >> .env
fi

generate_secret() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -base64 32
else
python - <<'PY'
import base64, os
print(base64.b64encode(os.urandom(32)).decode('ascii'))
PY
fi
}

# Ensure SECRET_KEY exists and is non-empty/non-placeholder
if grep -q '^SECRET_KEY=' .env; then
current_secret="$(grep '^SECRET_KEY=' .env | cut -d= -f2-)"
if [ -z "${current_secret}" ] || [ "${current_secret}" = "changeme" ] || [ "${current_secret}" = "REPLACE_ME" ]; then
new_secret="$(generate_secret)"
sed -i "s/^SECRET_KEY=.*/SECRET_KEY=${new_secret}/" .env
fi
else
echo "SECRET_KEY=$(generate_secret)" >> .env
fi

echo "Environment prepared. DB_HOST set to 'host.docker.internal' and SECRET_KEY ensured."

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.devcontainer
__pycache__
*.pyc
.env
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ services:
db:
image: postgres:latest
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
POSTGRES_DB: ${DB_NAME:-fastapi-jinja2-postgres-webapp}
ports:
- "${DB_PORT}:5432"
- "${DB_PORT:-5432}:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

Expand Down