- eveai_app adapted to handle removal of complex rewrite rules in nginx.conf, which cannot be achieved in Ingress

This commit is contained in:
Josako
2025-09-06 16:53:51 +02:00
parent b812aedb81
commit 5c20e6c1f9
28 changed files with 341 additions and 336 deletions

View File

@@ -1,27 +1,61 @@
from flask import request, current_app, url_for
from flask import request, url_for
from urllib.parse import urlsplit, urlunsplit
import re
VISIBLE_PREFIXES = ('/admin', '/api', '/chat-client')
def _derive_visible_prefix():
# 1) Edge-provided header (beste en meest expliciete bron)
xfp = request.headers.get('X-Forwarded-Prefix')
if xfp and any(xfp.startswith(p) for p in VISIBLE_PREFIXES):
return xfp.rstrip('/')
# 2) Referer fallback: haal het top-level segment uit de Referer path
ref = request.headers.get('Referer') or ''
try:
ref_path = urlsplit(ref).path or ''
m = re.match(r'^/(admin|api|chat-client)(?:\b|/)', ref_path)
if m:
return f"/{m.group(1)}"
except Exception:
pass
# 3) Geen prefix bekend
return ''
def prefixed_url_for(endpoint, **values):
prefix = request.headers.get('X-Forwarded-Prefix', '')
scheme = request.headers.get('X-Forwarded-Proto', request.scheme)
host = request.headers.get('Host', request.host)
"""
Gedrag:
- Default (_external=False, for_redirect=False): retourneer relatief pad (zonder leading '/')
voor templates/JS. De dynamische <base> zorgt voor correcte resolutie onder het zichtbare prefix.
- _external=True: bouw absolute URL (schema/host). Als X-Forwarded-Prefix aanwezig is,
prefixeer de path daarmee (handig voor e-mails/deeplinks).
- for_redirect=True: geef root-absoluut pad inclusief zichtbaar top-prefix, geschikt
voor HTTP Location headers. Backwards compat: _as_location=True wordt behandeld als for_redirect.
"""
external = values.pop('_external', False)
generated_url = url_for(endpoint, **values)
# Backwards compatibility met oudere paramnaam
if values.pop('_as_location', False):
values['for_redirect'] = True
for_redirect = values.pop('for_redirect', False)
generated_url = url_for(endpoint, **values) # bv. "/user/tenant_overview"
path, query, fragment = urlsplit(generated_url)[2:5]
if external:
path, query, fragment = urlsplit(generated_url)[2:5]
# Check if the prefix is already present in the path
if prefix and not path.startswith(prefix):
new_path = prefix + path
else:
new_path = path
scheme = request.headers.get('X-Forwarded-Proto', request.scheme)
host = request.headers.get('Host', request.host)
xfp = request.headers.get('X-Forwarded-Prefix', '') or ''
new_path = (xfp.rstrip('/') + path) if (xfp and not path.startswith(xfp)) else path
return urlunsplit((scheme, host, new_path, query, fragment))
else:
# Check if the prefix is already present in the generated URL
if prefix and not generated_url.startswith(prefix):
return prefix + generated_url
else:
return generated_url
if for_redirect:
visible_prefix = _derive_visible_prefix()
if visible_prefix and not path.startswith(visible_prefix):
return f"{visible_prefix}{path}"
# root-absoluut pad, zonder prefix als onbekend
return path
# Default: relatief pad
return path[1:] if path.startswith('/') else path