From c69e6d23133ce8cbe50722a3050fbc7aa3775a9d Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Thu, 7 Nov 2024 16:05:11 +0100 Subject: [PATCH] rework the hosts def --- .env.example | 1 - .vscode/launch.json | 10 +++++++++- openIMIS/openIMIS/settings/base.py | 7 ++++--- openIMIS/openIMIS/settings/common.py | 3 +++ openIMIS/openIMIS/settings/prod.py | 9 +++------ 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 46dda2ee..d43fcde8 100644 --- a/.env.example +++ b/.env.example @@ -15,7 +15,6 @@ MSSQL_DB_HOST=127.0.0.1 DB_NAME=test_imis DB_TEST_NAME=test_imis - # Site root that will prefix all exposed endpoints. It's required when working with openIMIS frontend SITE_ROOT=api # Should the debug be on (i.e. debug information will be displayed) diff --git a/.vscode/launch.json b/.vscode/launch.json index 22242022..cacb44b2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -220,7 +220,9 @@ "django": true, "cwd": "${workspaceRoot}/openIMIS", "env": { - "DB_DEFAULT": "${input:dbEngine}" + "DB_DEFAULT": "${input:dbEngine}", + "PROTOS": "http", + "HOSTS": "localhost", }, "justMyCode": false }, @@ -240,6 +242,8 @@ "cwd": "${workspaceRoot}/openIMIS", "env": { "DB_DEFAULT": "${input:dbEngine}", + "PROTOS": "http", + "HOSTS": "localhost", //"ASYNC": "PROD", "CELERY_BROKER_URL": "memory://openIMIS-test//", "CELERY_RESULT_BACKEND": "cache+memory://openIMIS-test//" @@ -261,6 +265,8 @@ "cwd": "${workspaceRoot}/openIMIS", "env": { "DB_DEFAULT": "${input:dbEngine}", + "PROTOS": "http", + "HOSTS": "localhost", //"ASYNC": "PROD", "CELERY_BROKER_URL": "memory://openIMIS-test//", "CELERY_RESULT_BACKEND": "cache+memory://openIMIS-test//" @@ -279,6 +285,8 @@ "args": ["-A", "openIMIS", "worker", "-l", "debug", "-Q", "queueName"], "env": { "DB_DEFAULT": "${input:dbEngine}", + "PROTOS": "http", + "HOSTS": "localhost", "ASYNC": "PROD", "CELERY_BROKER_URL": "amqp://lov", "CELERY_RESULT_BACKEND": "cache+memory://openIMIS-test//" diff --git a/openIMIS/openIMIS/settings/base.py b/openIMIS/openIMIS/settings/base.py index 089320c2..fcfb20b1 100644 --- a/openIMIS/openIMIS/settings/base.py +++ b/openIMIS/openIMIS/settings/base.py @@ -7,7 +7,7 @@ from ..openimisapps import openimis_apps, get_locale_folders from datetime import timedelta -from .common import DEBUG, BASE_DIR, MODE +from .common import DEBUG, BASE_DIR, MODE, protos, hosts from .security import REMOTE_USER_AUTHENTICATION # Makes openimis_apps available to other modules @@ -17,6 +17,8 @@ def SITE_ROOT(): root = os.environ.get("SITE_ROOT", "") if root == "": + if hosts and protos: + return f"{protos[0]}://{hosts[0]}/" return root elif root.endswith("/"): return root @@ -35,7 +37,7 @@ def SITE_URL(): SITE_FRONT = os.environ.get("SITE_FRONT", "front") -FRONTEND_URL = SITE_ROOT() + SITE_FRONT +FRONTEND_URL = os.environ.get("FRONTEND_URL", SITE_URL() + SITE_FRONT) # Application definition @@ -192,4 +194,3 @@ def SITE_URL(): # By default, the maximum upload size is 2.5Mb, which is a bit short for base64 picture upload DATA_UPLOAD_MAX_MEMORY_SIZE = int(os.environ.get('DATA_UPLOAD_MAX_MEMORY_SIZE', 10 * 1024 * 1024)) -FRONTEND_URL = os.environ.get("FRONTEND_URL", "") diff --git a/openIMIS/openIMIS/settings/common.py b/openIMIS/openIMIS/settings/common.py index 402f2625..a79f53a4 100644 --- a/openIMIS/openIMIS/settings/common.py +++ b/openIMIS/openIMIS/settings/common.py @@ -4,6 +4,9 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) MODE = os.environ.get("MODE", 'prod').lower() +# Fetch protocols and hosts from environment variables +protos = os.environ.get('PROTOS', default='https').split(',') +hosts = os.environ.get('HOSTS', default='localhost').split(',') if MODE == "dev": DEBUG = True else: diff --git a/openIMIS/openIMIS/settings/prod.py b/openIMIS/openIMIS/settings/prod.py index 4d8a513a..e90807ae 100644 --- a/openIMIS/openIMIS/settings/prod.py +++ b/openIMIS/openIMIS/settings/prod.py @@ -1,17 +1,14 @@ import os - +from .common import hosts, protos + GRAPHQL_JWT.update({ "JWT_COOKIE_SECURE": True, "JWT_COOKIE_SAMESITE": "Lax", }) -# Fetch protocols and hosts from environment variables -protos = os.environ.get('PROTOS', default='https').split(',') -hosts = os.environ.get('HOSTS', default='') - # Set ALLOWED_HOSTS -ALLOWED_HOSTS = hosts.split(',') if hosts else ['*'] +ALLOWED_HOSTS = hosts if hosts else ['*'] # Create CSRF_TRUSTED_ORIGINS by combining protocols and hosts CSRF_TRUSTED_ORIGINS = [f'{proto}://{host.strip()}' for proto in protos for host in ALLOWED_HOSTS if host != '*']