Skip to content

Commit

Permalink
Make Production Ready
Browse files Browse the repository at this point in the history
  • Loading branch information
vCra committed Apr 28, 2019
1 parent 3e17d4c commit 6d55ad9
Show file tree
Hide file tree
Showing 46 changed files with 937 additions and 108 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo programdom readme
22 changes: 17 additions & 5 deletions compose/local/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
FROM python:3.7-alpine
FROM node:alpine as static
COPY package.json /package.json
RUN npm install -g gulp
RUN npm install
WORKDIR /static
COPY ./programdom/static /programdom/static
COPY ./gulpfile.prod.js /gulpfile.js
RUN gulp build


FROM python:3.7-alpine
COPY --from=static /static /app/programdom/static
ENV PYTHONUNBUFFERED 1

RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
# Pillow dependencies
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add libffi-dev py-cffi \
# Translations dependencies
&& apk add gettext \
# https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell
&& apk add postgresql-client
&& apk add postgresql-client \
&& apk add libxml2-dev libxslt-dev


# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install -r /requirements/local.txt
RUN pip install -r /requirements/production.txt

COPY ./compose/local/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
Expand All @@ -27,6 +37,8 @@ COPY ./compose/local/django/start /start
RUN sed -i 's/\r//' /start
RUN chmod +x /start

COPY . /app

WORKDIR /app

ENTRYPOINT ["/entrypoint"]
5 changes: 1 addition & 4 deletions compose/local/django/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ set -o errexit
set -o pipefail
set -o nounset


# N.B. If only .env files supported variable expansion...
export CELERY_BROKER_URL="${REDIS_URL}"

if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
export REDIS_URL="redis://${REDIS_HOST}/"

postgres_ready() {
python << END
Expand Down
4 changes: 3 additions & 1 deletion compose/local/django/start
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ set -o nounset


python manage.py migrate
python manage.py runserver_plus 0.0.0.0:8000
python manage.py collectstatic --no-input
daphne -b 0.0.0.0 -p 8000 config.asgi:application

File renamed without changes.
Empty file added config/asgi.py
Empty file.
8 changes: 8 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
'default': env.db('DATABASE_URL', default='postgresql://postgres:postgrespassword@localhost:5432/postgres'),
}

# URLS
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -159,6 +162,9 @@
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# Clear the session when the user closes the browser, so that they are not part of another workshop next time
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

# MEDIA
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#media-root
Expand Down Expand Up @@ -237,3 +243,5 @@
# ------------------------------------------------------------------------------
#

# Time to wait before repolling Judge0 for a new status
JUDGE0_POLL_WAIT = 1
3 changes: 0 additions & 3 deletions config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
}
}

DATABASES = {
'default': env.db('DATABASE_URL', 'postgresql://postgres@localhost:5432/postgres'),
}

# Sessions
# ------------------------------------------------------------------------------
Expand Down
106 changes: 60 additions & 46 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,57 @@
}
}

INSTALLED_APPS += ['minio_storage'] # noqa F405

DEFAULT_FILE_STORAGE = "minio_storage.storage.MinioMediaStorage"
STATICFILES_STORAGE = "minio_storage.storage.MinioStaticStorage"
MINIO_STORAGE_ENDPOINT = f"{env('MINIO_SERVER')}:9000"
MINIO_STORAGE_ACCESS_KEY = env('MINIO_ACCESS_KEY')
MINIO_STORAGE_SECRET_KEY = env('MINIO_SECRET_KEY')
MINIO_STORAGE_USE_HTTPS = False
MINIO_STORAGE_MEDIA_BUCKET_NAME = 'media'
MINIO_STORAGE_AUTO_CREATE_MEDIA_BUCKET = True
MINIO_STORAGE_STATIC_BUCKET_NAME = 'static'
MINIO_STORAGE_AUTO_CREATE_STATIC_BUCKET = True
MINIO_STORAGE_MEDIA_URL = "/media"
MINIO_STORAGE_STATIC_URL = "/static"

MINIO_STORAGE_AUTO_CREATE_STATIC_POLICY = True
MINIO_STORAGE_AUTO_CREATE_MEDIA_POLICY = True

# SECURITY
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect
SECURE_SSL_REDIRECT = env.bool('DJANGO_SECURE_SSL_REDIRECT', default=True)
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect
# SECURE_SSL_REDIRECT = env.bool('DJANGO_SECURE_SSL_REDIRECT', default=True)
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
SESSION_COOKIE_SECURE = True
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-httponly
SESSION_COOKIE_HTTPONLY = True
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
CSRF_COOKIE_SECURE = True
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-httponly
CSRF_COOKIE_HTTPONLY = True
# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-seconds
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
SECURE_HSTS_SECONDS = 60
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool('DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS', default=True)
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
SECURE_HSTS_PRELOAD = env.bool('DJANGO_SECURE_HSTS_PRELOAD', default=True)
# https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
SECURE_CONTENT_TYPE_NOSNIFF = env.bool('DJANGO_SECURE_CONTENT_TYPE_NOSNIFF', default=True)
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-browser-xss-filter
SECURE_BROWSER_XSS_FILTER = True
# https://docs.djangoproject.com/en/dev/ref/settings/#x-frame-options
X_FRAME_OPTIONS = 'DENY'


# SESSION_COOKIE_SECURE = True
# # https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-httponly
# SESSION_COOKIE_HTTPONLY = True
# # https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
# CSRF_COOKIE_SECURE = True
# # https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-httponly
# CSRF_COOKIE_HTTPONLY = True
# # https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-seconds
# # TODO: set this to 60 seconds first and then to 518400 once you prove the former works
# SECURE_HSTS_SECONDS = 60
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
# SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool('DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS', default=True)
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
# SECURE_HSTS_PRELOAD = env.bool('DJANGO_SECURE_HSTS_PRELOAD', default=True)
# # https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
# SECURE_CONTENT_TYPE_NOSNIFF = env.bool('DJANGO_SECURE_CONTENT_TYPE_NOSNIFF', default=True)
# # https://docs.djangoproject.com/en/dev/ref/settings/#secure-browser-xss-filter
# SECURE_BROWSER_XSS_FILTER = True
# # https://docs.djangoproject.com/en/dev/ref/settings/#x-frame-options
# X_FRAME_OPTIONS = 'DENY'

SECRET_KEY = env('DJANGO_SECRET_KEY')

#TODO: change this to the hostname that you want to listen on
ALLOWED_HOSTS = "*"

# TEMPLATES
# ------------------------------------------------------------------------------
Expand All @@ -65,25 +86,7 @@
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
# https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
EMAIL_SUBJECT_PREFIX = env('DJANGO_EMAIL_SUBJECT_PREFIX', default='[AberCompSoc]')

MAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = env('DJANGO_SMTP_HOSTNAME', default='smtp.office365.com')
EMAIL_PORT = env('DJANGO_SMTP_POST', default=587)
EMAIL_HOST_USER = env('DJANGO_SMTP_USER')
EMAIL_HOST_PASSWORD = env('DJANGO_SMTP_PASSWORD')

EMAIL_USE_TLS = True


# https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
DEFAULT_FROM_EMAIL = env(
'DJANGO_DEFAULT_FROM_EMAIL',
default=EMAIL_HOST_USER
)

SERVER_EMAIL = env('DJANGO_SERVER_EMAIL', default=DEFAULT_FROM_EMAIL)
MAIL_BACKEND = 'django.core.mail.backends.console'

# Static Files
#
Expand Down Expand Up @@ -143,3 +146,14 @@

# Your stuff...
# ------------------------------------------------------------------------------

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(env("REDIS_HOST"), 6379)],
},
},
}

JUDGE0_ENDPOINT = "http://judgezero:3000"
2 changes: 1 addition & 1 deletion config/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
WSGI config for AberCompSoc project.
WSGI config for Programdom project.
This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
Expand Down
19 changes: 19 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Loading

0 comments on commit 6d55ad9

Please sign in to comment.