From 7832282545d6136d1d7fc5064f477f8e904ad1ba Mon Sep 17 00:00:00 2001 From: meeb Date: Mon, 25 Jul 2022 13:36:12 +1000 Subject: [PATCH] patch the wsgi application environ to support sub-URLs, add a master ENV var to set a sub-URL, tweak SASS and README to match, actually resolves #255 --- README.md | 3 +-- tubesync/common/static/styles/_fonts.scss | 6 +++--- tubesync/tubesync/local_settings.py.container | 6 +++++- tubesync/tubesync/wsgi.py | 21 ++++++++++++++++++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ea2160a..6155e9e 100644 --- a/README.md +++ b/README.md @@ -358,8 +358,7 @@ useful if you are manually installing TubeSync in some other environment. These | Name | What | Example | | ------------------------ | ------------------------------------------------------------ | ------------------------------------ | | DJANGO_SECRET_KEY | Django's SECRET_KEY | YJySXnQLB7UVZw2dXKDWxI5lEZaImK6l | -| DJANGO_FORCE_SCRIPT_NAME | Django's FORCE_SCRIPT_NAME | /somepath/ | -| DJANGO_STATIC_URL | Django's STATIC_URL (set with FORCE_SCRIPT_NAME) | /somepath/static/ | +| DJANGO_URL_PREFIX | Run TubeSync in a sub-URL on the web server | /somepath/ | | TUBESYNC_DEBUG | Enable debugging | True | | TUBESYNC_WORKERS | Number of background workers, default is 2, max allowed is 8 | 2 | | TUBESYNC_HOSTS | Django's ALLOWED_HOSTS, defaults to `*` | tubesync.example.com,otherhost.com | diff --git a/tubesync/common/static/styles/_fonts.scss b/tubesync/common/static/styles/_fonts.scss index e088e3d..66fb854 100644 --- a/tubesync/common/static/styles/_fonts.scss +++ b/tubesync/common/static/styles/_fonts.scss @@ -1,20 +1,20 @@ @font-face { font-family: 'roboto-light'; - src: url('/static/fonts/roboto/roboto-light.woff') format('woff'); + src: url('../fonts/roboto/roboto-light.woff') format('woff'); font-weight: normal; font-style: normal; } @font-face { font-family: 'roboto-regular'; - src: url('/static/fonts/roboto/roboto-regular.woff') format('woff'); + src: url('../fonts/roboto/roboto-regular.woff') format('woff'); font-weight: normal; font-style: normal; } @font-face { font-family: 'roboto-bold'; - src: url('/static/fonts/roboto/roboto-bold.woff') format('woff'); + src: url('../fonts/roboto/roboto-bold.woff') format('woff'); font-weight: bold; font-style: normal; } diff --git a/tubesync/tubesync/local_settings.py.container b/tubesync/tubesync/local_settings.py.container index c008ef2..a105058 100644 --- a/tubesync/tubesync/local_settings.py.container +++ b/tubesync/tubesync/local_settings.py.container @@ -1,5 +1,6 @@ import os from pathlib import Path +from urllib.parse import urljoin from common.logger import log from common.utils import parse_database_connection_string @@ -8,7 +9,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent ROOT_DIR = Path('/') CONFIG_BASE_DIR = ROOT_DIR / 'config' DOWNLOADS_BASE_DIR = ROOT_DIR / 'downloads' +DJANGO_URL_PREFIX = os.getenv('DJANGO_URL_PREFIX', None) STATIC_URL = str(os.getenv('DJANGO_STATIC_URL', '/static/')) +if DJANGO_URL_PREFIX and STATIC_URL: + STATIC_URL = urljoin(DJANGO_URL_PREFIX, STATIC_URL[1:]) # This is not ever meant to be a public web interface so this isn't too critical @@ -18,7 +22,7 @@ SECRET_KEY = str(os.getenv('DJANGO_SECRET_KEY', 'tubesync-django-secret')) ALLOWED_HOSTS_STR = str(os.getenv('TUBESYNC_HOSTS', '*')) ALLOWED_HOSTS = ALLOWED_HOSTS_STR.split(',') DEBUG = True if os.getenv('TUBESYNC_DEBUG', False) else False -FORCE_SCRIPT_NAME = os.getenv('DJANGO_FORCE_SCRIPT_NAME', None) +FORCE_SCRIPT_NAME = os.getenv('DJANGO_FORCE_SCRIPT_NAME', DJANGO_URL_PREFIX) TIME_ZONE = os.getenv('TZ', 'UTC') diff --git a/tubesync/tubesync/wsgi.py b/tubesync/tubesync/wsgi.py index 8f7c1b6..f397227 100644 --- a/tubesync/tubesync/wsgi.py +++ b/tubesync/tubesync/wsgi.py @@ -1,6 +1,25 @@ import os +from urllib.parse import urljoin from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tubesync.settings') -application = get_wsgi_application() +DJANGO_URL_PREFIX = os.getenv('DJANGO_URL_PREFIX', None) +_application = get_wsgi_application() + + +def application(environ, start_response): + script_name = None + if DJANGO_URL_PREFIX: + if DJANGO_URL_PREFIX.endswith('/'): + script_name = DJANGO_URL_PREFIX + else: + raise Exception(f'DJANGO_URL_PREFIX must end with a /, ' + f'got: {DJANGO_URL_PREFIX}') + if script_name: + static_url = urljoin(script_name, 'static/') + environ['SCRIPT_NAME'] = script_name + path_info = environ['PATH_INFO'] + if path_info.startswith(script_name) and not path_info.startswith(static_url): + environ['PATH_INFO'] = path_info[len(script_name):] + return _application(environ, start_response)