From a2e923d15baecb83a3d86f060fe5a3af40b63f4e Mon Sep 17 00:00:00 2001 From: Michal Cyprian <michal@platform.sh> Date: Fri, 7 Sep 2018 10:47:23 +0200 Subject: [PATCH] Add Platform.sh settings --- myapp/settings.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/myapp/settings.py b/myapp/settings.py index a426e20..accdd9d 100644 --- a/myapp/settings.py +++ b/myapp/settings.py @@ -11,6 +11,9 @@ https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os +import json +import base64 +from urllib.parse import urlparse # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -118,3 +121,40 @@ USE_TZ = True # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' + +# Import some Platform.sh settings from the environment. + +STATIC_ROOT = os.path.join(os.getenv('PLATFORM_APP_DIR'), 'static') + +entropy = os.getenv('PLATFORM_PROJECT_ENTROPY') +if entropy: + SECRET_KEY = entropy + +routes = os.getenv('PLATFORM_ROUTES') +if routes: + routes = json.loads(base64.b64decode(routes).decode('utf-8')) + app_name = os.getenv('PLATFORM_APPLICATION_NAME') + for url, route in routes.items(): + host = urlparse(url).netloc + if (host not in ALLOWED_HOSTS and route['type'] == 'upstream' + and route['upstream'] == app_name): + ALLOWED_HOSTS.append(host) + +relationships = os.getenv('PLATFORM_RELATIONSHIPS') +if relationships: + relationships = json.loads(base64.b64decode(relationships).decode('utf-8')) + db_settings = relationships['database'][0] + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': db_settings['path'], + 'USER': db_settings['username'], + 'PASSWORD': db_settings['password'], + 'HOST': db_settings['host'], + 'PORT': db_settings['port'], + }, + 'sqlite': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } + } -- GitLab