diff --git a/pyconweb2022/.dockerignore b/pyconweb2022/.dockerignore new file mode 100644 index 0000000..06fb9a1 --- /dev/null +++ b/pyconweb2022/.dockerignore @@ -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/ diff --git a/pyconweb2022/Dockerfile b/pyconweb2022/Dockerfile new file mode 100644 index 0000000..b93b409 --- /dev/null +++ b/pyconweb2022/Dockerfile @@ -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"] diff --git a/pyconweb2022/docker-compose.yml b/pyconweb2022/docker-compose.yml new file mode 100644 index 0000000..daa4848 --- /dev/null +++ b/pyconweb2022/docker-compose.yml @@ -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: diff --git a/pyconweb2022/pyconweb2022/settings.py b/pyconweb2022/pyconweb2022/settings.py index 1c2d831..be6f859 100644 --- a/pyconweb2022/pyconweb2022/settings.py +++ b/pyconweb2022/pyconweb2022/settings.py @@ -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'. @@ -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 @@ -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