diff --git a/Dockerfile b/Dockerfile index fcd288c..1060ffb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,9 +8,11 @@ COPY requirements.txt $APPS_DIR/ WORKDIR $APPS_DIR +# uWSGI requires linux-headers RUN set -ex \ && buildDeps=' \ build-base \ + linux-headers \ python3-dev \ ' \ && apk --no-cache --update add \ @@ -22,9 +24,13 @@ RUN set -ex \ && apk del $buildDeps \ && rm -rf /var/cache/apk/* -COPY ./collection $APPS_DIR +COPY ./collection . VOLUME $DB_DIR +COPY ./docker-entrypoint.sh . +COPY ./uwsgi.ini . +ENTRYPOINT ["./docker-entrypoint.sh"] + EXPOSE 8000 -CMD ["/bin/sh"] +CMD ["prod"] diff --git a/TODO b/TODO index eebac9c..1d72d7d 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ * Tests sur Console * Tests sur Game * Données initiales des consoles connues - * Dockerfile avec UWSGI * API django rest framework * documentation API * travis.yml to launch test diff --git a/collection/collection/production.py b/collection/collection/production.py new file mode 100644 index 0000000..cc27de8 --- /dev/null +++ b/collection/collection/production.py @@ -0,0 +1,18 @@ +""" +Django production settings +""" +import os +from collection.settings import * # NOQA + +DEBUG = os.getenv('DEBUG', False) + +SECRET_KEY = os.getenv('SECRET_KEY') + +ALLOWED_HOSTS = [os.getenv('ALLOWED_HOSTS', '*')] + +# Domain configuration: +# - if you have a specific domain to secure, change ALLOWED_HOSTS like this: +# ALLOWED_HOSTS = ['domain.tld', 'domain.tld.'] +# - if you use a proxy like Nginx, you need to add this line into config: +# `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;` +USE_X_FORWARDED_HOST = True diff --git a/collection/collection/settings.py b/collection/collection/settings.py index 29fac16..58a7ab2 100644 --- a/collection/collection/settings.py +++ b/collection/collection/settings.py @@ -111,12 +111,15 @@ TIME_ZONE = 'UTC' USE_I18N = True -USE_L10N = True +USE_L10N = False -USE_TZ = True +USE_TZ = False # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(os.path.abspath(os.path.curdir), 'static') +if not os.getenv('STATIC_ROOT', None): + STATIC_ROOT = os.path.abspath(os.getenv('STATIC_ROOT')) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..063b041 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env sh +set -e + +# Make database writable +chown -R guest "$DB_DIR" + +if [ "$1" = 'dev' ]; then + export DJANGO_SETTINGS_MODULE=collection.settings + exec python3 manage.py runserver 0.0.0.0:8000 +elif [ "$1" = 'prod' ]; then + export DJANGO_SETTINGS_MODULE=collection.production + # Collect static files + python3 manage.py collectstatic --noinput --clear -v 0 + exec uwsgi --ini uwsgi.ini --pythonpath "$APPS_DIR" --static-map=/static/="$STATIC_ROOT" +fi + +exec "$@" diff --git a/requirements.txt b/requirements.txt index f3000f6..db6b2d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django==1.11 PyYAML==3.12 +uWSGI==2.0.15 diff --git a/uwsgi.ini b/uwsgi.ini new file mode 100644 index 0000000..2e54877 --- /dev/null +++ b/uwsgi.ini @@ -0,0 +1,8 @@ +[uwsgi] +http-socket = :8000 +module = collection.wsgi:application +master = true +processes = 4 +reload-on-as = 512 +enable-threads = true +uid = guest