diff --git a/.gitignore b/.gitignore index f99362a..dd04602 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ static/perks +.env diff --git a/Dockerfile-backend b/Dockerfile-backend new file mode 100644 index 0000000..14c4e9e --- /dev/null +++ b/Dockerfile-backend @@ -0,0 +1,21 @@ +# vim: set ft=dockerfile : +FROM python:3.7-alpine + +COPY backend /app +WORKDIR /app +EXPOSE 80 + +RUN set -ex \ + && apk add --no-cache --virtual .build-deps \ + gcc \ + make \ + libc-dev \ + musl-dev \ + libffi-dev \ + && pip install --no-cache-dir pipenv \ + && pipenv install --system --clear \ + && apk del .build-deps \ + && rm db.sqlite3 + +CMD ["./entrypoint.sh"] + diff --git a/Dockerfile-frontend b/Dockerfile-frontend new file mode 100644 index 0000000..0f95786 --- /dev/null +++ b/Dockerfile-frontend @@ -0,0 +1,20 @@ +# vim: set ft=dockerfile : +# Stage 1 (Bundling) +FROM node:alpine as builder + +COPY frontend /app +WORKDIR /app +RUN set -ex \ + && apk add --no-cache yarn \ + && yarn install \ + && yarn run build + +# Stage 2 (Packaging) +FROM nginx:alpine + +COPY --from=builder /app/dist /app +COPY nginx.conf /etc/nginx/conf.d +RUN rm /etc/nginx/conf.d/default.conf + +EXPOSE 80 + diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh new file mode 100755 index 0000000..9aa9c02 --- /dev/null +++ b/backend/entrypoint.sh @@ -0,0 +1,11 @@ +#! /bin/sh +# +# entrypoint.sh +# Copyright (C) 2019 pavle +# +# Distributed under terms of the BSD-3-Clause license. +# + +python manage.py migrate +gunicorn -w 4 --bind 0.0.0.0:80 perktree.wsgi:application + diff --git a/backend/perktree/settings.py b/backend/perktree/settings.py index c050455..351b0fd 100644 --- a/backend/perktree/settings.py +++ b/backend/perktree/settings.py @@ -20,16 +20,16 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '^j_x7a7u_#9ruh3p^h=*my_k+asoqob&xq@5n^2n2f(7$#dk(#' +SECRET_KEY = '^j_x7a7u_#9ruh3p^h=*my_k+asoqob&xq@5n^2n2f(7$#dk(#' if os.environ.get('DJANGO_ENV') == 'dev' else os.environ.get('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.environ.get('DJANGO_ENV') == 'dev' ALLOWED_HOSTS = [ - 'perktree.localhost' + 'perktree.localhost', + os.environ.get('APP_HOST'), ] - # Application definition INSTALLED_APPS = [ @@ -78,12 +78,15 @@ WSGI_APPLICATION = 'perktree.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': os.environ.get('DB_ENGINE', ''), + 'NAME': os.path.join(BASE_DIR, os.environ.get('DB_NAME', '')) if os.environ.get('DJANGO_ENV') == 'dev' else os.environ.get('DB_NAME', ''), + 'USER': os.environ.get('DB_USER', ''), + 'PASSWORD': os.environ.get('DB_PASSWORD', ''), + 'HOST': os.environ.get('DB_HOST', ''), + 'PORT': os.environ.get('DB_PORT', ''), } } - # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6e15233 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: "3" +services: + db: + image: mariadb + container_name: perktree-db + restart: unless-stopped + environment: + MYSQL_ROOT_PASSWORD: perktree-root-password + MYSQL_DATABASE: perktree + + frontend: + build: + context: . + dockerfile: Dockerfile-frontend + container_name: perktree-frontend + restart: unless-stopped + ports: + - 8019:80 + links: + - backend + + backend: + build: + context: . + dockerfile: Dockerfile-backend + container_name: perktree-backend + restart: unless-stopped + environment: + SECRET_KEY: ${SECRET_KEY} + DJANGO_ENV: prod + APP_HOST: perktree.theedgeofrage.com + DB_ENGINE: django.db.backends.mysql + DB_NAME: perktree + DB_USER: root + DB_PASSWORD: perktree-root-password + DB_HOST: db + DB_PORT: 3306 + links: + - db + diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..0b0b78b --- /dev/null +++ b/nginx.conf @@ -0,0 +1,18 @@ +server { + listen 80; + server_name _; + client_max_body_size 5M; + + location /api/ { + proxy_pass http://perktree-backend; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location / { + root /app; + try_files $uri $uri/ /index.html; + } +} +