Skip to content
Open
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
46 changes: 46 additions & 0 deletions pyconweb2022/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Git
.git
.gitignore

# Python
__pycache__
*.py[cod]
*$py.class
*.so
.Python
.venv
venv/
ENV/
env/

# Django
*.log
db.sqlite3
staticfiles/
media/

# IDE
.idea/
.vscode/
*.swp
*.swo

# Docker
Dockerfile
docker-compose*.yml
.docker

# Test
.coverage
htmlcov/
.pytest_cache/
.tox/

# OS
.DS_Store
Thumbs.db

# Misc
*.egg-info/
dist/
build/
26 changes: 26 additions & 0 deletions pyconweb2022/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM python:3.8-slim

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
default-libmysqlclient-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip

COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

RUN pip install gunicorn

COPY pyconweb2022/ /app/

EXPOSE 8000

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "pyconweb2022.wsgi:application"]
36 changes: 36 additions & 0 deletions pyconweb2022/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
web:
build:
context: ..
dockerfile: pyconweb2022/Dockerfile
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- DEBUG=True
- DJANGO_SETTINGS_MODULE=pyconweb2022.settings
- AWS_PSQL_HOST=db
- AWS_PSQL_PORT=5432
- AWS_PSQL_DATABASE=pyconweb
- AWS_PSQL_USER_ID=postgres
- AWS_PSQL_PW=postgres
depends_on:
- db
command: >
sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"

db:
image: postgres:14-alpine
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=pyconweb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5433:5432"

volumes:
postgres_data:
65 changes: 48 additions & 17 deletions pyconweb2022/pyconweb2022/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os.path
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -86,14 +86,24 @@

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases


DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
if os.getenv("AWS_PSQL_HOST"):
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": os.getenv("AWS_PSQL_HOST"),
"PORT": os.getenv("AWS_PSQL_PORT", "5432"),
"NAME": os.getenv("AWS_PSQL_DATABASE"),
"USER": os.getenv("AWS_PSQL_USER_ID"),
"PASSWORD": os.getenv("AWS_PSQL_PW"),
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
}


# Password validation
Expand Down Expand Up @@ -130,15 +140,36 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"

# S3
DEFAULT_FILE_STORAGE = (
"pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage"
)
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
AWS_S3_SESSION_PROFILE = "pycon"
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STATIC_S3_BUCKET")
STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "staticfiles"
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

if os.getenv("AWS_STATIC_S3_BUCKET"):
DEFAULT_FILE_STORAGE = (
"pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage"
)
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
AWS_S3_SESSION_PROFILE = "pycon"
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STATIC_S3_BUCKET")

STORAGES = {
"default": {
"BACKEND": "pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage",
},
"staticfiles": {
"BACKEND": "storages.backends.s3boto3.S3StaticStorage",
},
}
else:
STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
Expand Down