From 89396ebac904ec649e6d9bff58c282ee0571f961 Mon Sep 17 00:00:00 2001 From: rmanach Date: Fri, 22 Sep 2023 22:17:21 +0200 Subject: [PATCH] set environment settings --- .env.example | 12 ++++++++++++ .gitignore | 3 ++- docker-compose.yml | 8 ++++---- mumui/settings.py | 16 ++++++++++------ startup.sh | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7e46f26 --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# 'dev' or whatever for dev environment, otherwise 'prod' to enable production environment +ENV=dev + +# used only on production environment +HOST= + +POSTGRES_DB=mumui +POSTGRES_USER=mumui +POSTGRES_PASSWORD=admin + +# used at installation +ADMIN_PASSWORD=admin \ No newline at end of file diff --git a/.gitignore b/.gitignore index c9ac037..0daef0e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ venv /static *.log *.pid -docker-compose.override.yml \ No newline at end of file +docker-compose.override.yml +.env \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 9c4d9ca..f13d77c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,10 +19,8 @@ services: postgres: image: postgres:latest container_name: postgres - environment: - POSTGRES_DB: mumui - POSTGRES_USER: test - POSTGRES_PASSWORD: test + env_file: + - .env networks: - mumui_network volumes: @@ -31,6 +29,8 @@ services: mumui: image: mumui:local container_name: mumui + env_file: + - .env networks: - mumui_network volumes: diff --git a/mumui/settings.py b/mumui/settings.py index 765744d..a53c6f3 100644 --- a/mumui/settings.py +++ b/mumui/settings.py @@ -9,6 +9,7 @@ https://docs.djangoproject.com/en/4.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ +import os from pathlib import Path @@ -22,10 +23,13 @@ BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = "django-insecure-_c56%%c8%g%@5(3&thxi7ku2a&wst8lik*8@l0=#)ar)s86g36" -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +IS_PROD = os.getenv("ENV") == "prod" +HOST = "*" if not IS_PROD else os.getenv("HOST", "*") -ALLOWED_HOSTS = ["dev.thegux.fr"] +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = not IS_PROD + +ALLOWED_HOSTS = ["*"] if not IS_PROD else [HOST.lstrip("https://")] # Application definition @@ -80,8 +84,8 @@ DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "mumui", - "USER": "test", - "PASSWORD": "test", + "USER": os.getenv("POSTGRES_USER", "test"), + "PASSWORD": os.getenv("POSTGRES_PASSWORD", "test"), "HOST": "postgres", "PORT": "5432", } @@ -135,4 +139,4 @@ LOGOUT_REDIRECT_URL = "home" GRIP_URL = "http://pushpin:5561" EVENTSTREAM_CHANNELMANAGER_CLASS = "deployment.channel.DeploymentChannelManager" -CSRF_TRUSTED_ORIGINS = ["https://dev.thegux.fr"] +CSRF_TRUSTED_ORIGINS = [HOST] if IS_PROD else ["http://localhost:8080"] diff --git a/startup.sh b/startup.sh index cc01d89..48b7a31 100755 --- a/startup.sh +++ b/startup.sh @@ -5,6 +5,6 @@ python manage.py makemigrations python manage.py migrate python manage.py collectstatic --no-input -DJANGO_SUPERUSER_PASSWORD=admin python manage.py createsuperuser --noinput --username admin --email admin@admin.fr +DJANGO_SUPERUSER_PASSWORD=${ADMIN_PASSWORD} python manage.py createsuperuser --noinput --username admin --email admin@admin.fr supervisord -c /app/supervisord.conf \ No newline at end of file