Add support for setting a base url for the server endpoint

This commit is contained in:
okRjofdqw716 2025-12-29 17:57:29 -08:00
parent 0bd1cab9e2
commit f4fd1245dc
5 changed files with 341 additions and 296 deletions

View file

@ -63,6 +63,7 @@ environment:
- FLASK_ENV=production # Flask environment - FLASK_ENV=production # Flask environment
- PYTHONPATH=/app # Python path - PYTHONPATH=/app # Python path
- SOULSYNC_CONFIG_PATH=/app/config/config.json # Config file location - SOULSYNC_CONFIG_PATH=/app/config/config.json # Config file location
- SOULSYNC_BASE_URL=/ # Configure the endpoint base_url, useful for reverse proxies
- TZ=America/New_York # Timezone - TZ=America/New_York # Timezone
``` ```

Binary file not shown.

View file

@ -65,6 +65,7 @@ if os.path.exists(config_path):
print(f"🔴 FAILED to re-initialize config_manager: {e}") print(f"🔴 FAILED to re-initialize config_manager: {e}")
else: else:
print(f"🔴 WARNING: config.json not found at {config_path}. Using default settings.") print(f"🔴 WARNING: config.json not found at {config_path}. Using default settings.")
# Correctly point to the 'webui' directory for templates and static files # Correctly point to the 'webui' directory for templates and static files
app = Flask( app = Flask(
__name__, __name__,
@ -72,6 +73,41 @@ app = Flask(
static_folder=os.path.join(base_dir, 'webui', 'static') static_folder=os.path.join(base_dir, 'webui', 'static')
) )
#
# Hack-ish support for custom base urls
#
# This is generally supported out of the box by proper werkzeug configuration or more production ready WSGI servers (gunicorn)
# where the Flask app needs to be configured and returned as function or attributem but not 'run'.
# (Run is handled by the WSGI server)
#
class PrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ["This url does not belong to the app.".encode()]
base_url = '/'
if 'SOULSYNC_BASE_URL' in os.environ:
base_url = os.environ['SOULSYNC_BASE_URL']
if base_url != '/': # the middleware seems to misbehave if the base_url is the default path
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=base_url)
# Inject the base_url into the context so it can be used in the template rendering
@app.context_processor
def inject_base_url():
return dict(base_url=base_url)
# --- Docker Helper Functions --- # --- Docker Helper Functions ---
def docker_resolve_path(path_str): def docker_resolve_path(path_str):
""" """

View file

@ -4,6 +4,8 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SoulSync - Music Sync & Manager</title> <title>SoulSync - Music Sync & Manager</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<!-- APP_BASE_URL obtained from server, used in script.js to know the base server path -->
<script>var APP_BASE_URL = "{{ base_url }}"; console.log('Soulsync base url:', APP_BASE_URL);</script>
</head> </head>
<body> <body>
<div class="main-container"> <div class="main-container">

File diff suppressed because it is too large Load diff