build relative urls for HA ingress

This commit is contained in:
Stefan Allius
2025-04-28 23:06:13 +02:00
parent 8375679777
commit b8a39aac55
6 changed files with 51 additions and 9 deletions

View File

@@ -16,6 +16,8 @@ from cnf.config_read_env import ConfigReadEnv
from cnf.config_read_toml import ConfigReadToml
from cnf.config_read_json import ConfigReadJson
from web import Web
from web.wrapper import url_for
from modbus_tcp import ModbusTcp
@@ -34,8 +36,8 @@ class ProxyState:
app = Quart(__name__,
template_folder='web/templates',
static_folder='web/static')
Web(app, '../translations')
app.secret_key = 'JKLdks.dajlKKKdladkflKwolafallsdfl'
app.jinja_env.globals.update(url_for=url_for)
@app.route('/-/ready')
@@ -130,6 +132,12 @@ def main(): # pragma: no cover
parser.add_argument('-b', '--log_backups', type=int,
default=0,
help='set max number of daily log-files')
parser.add_argument('-tr', '--trans_path', type=str,
default='../translations/',
help='set path for the translations files')
parser.add_argument('-r', '--rel_urls', type=bool,
default=False,
help='use relative dashboard urls')
args = parser.parse_args()
#
# Setup our daily, rotating logger
@@ -148,6 +156,8 @@ def main(): # pragma: no cover
logging.info(f"config_path: {args.config_path}")
logging.info(f"json_config: {args.json_config}")
logging.info(f"toml_config: {args.toml_config}")
logging.info(f"trans_path: {args.trans_path}")
logging.info(f"rel_urls: {args.rel_urls}")
logging.info(f"log_path: {args.log_path}")
if args.log_backups == 0:
logging.info("log_backups: unlimited")
@@ -186,6 +196,7 @@ def main(): # pragma: no cover
Proxy.class_init()
Schedule.start()
ModbusTcp(loop)
Web(app, args.trans_path, args.rel_urls)
#
# Create tasks for our listening servers. These must be tasks! If we call
@@ -202,7 +213,8 @@ def main(): # pragma: no cover
try:
ProxyState.set_up(True)
logging.info("Start Quart")
app.run(host='0.0.0.0', port=8127, use_reloader=False, loop=loop)
app.run(host='0.0.0.0', port=8127, use_reloader=False, loop=loop,
debug=True,)
logging.info("Quart stopped")
except KeyboardInterrupt:

View File

@@ -16,7 +16,11 @@ load_modules(__loader__)
class Web:
'''Helper Class to register the Blueprint at Quart and
initializing Babel'''
def __init__(self, app: Quart, translation_directories: str | list[str]):
def __init__(self,
app: Quart,
translation_directories: str | list[str],
rel_urls: bool):
web.build_relative_urls = rel_urls
app.register_blueprint(web)
from .i18n import get_locale, get_tz

View File

@@ -1,4 +1,4 @@
from quart import request, session, redirect, abort, url_for
from quart import request, session, redirect, abort
from quart_babel.locale import get_locale as babel_get_locale
from . import web
@@ -38,7 +38,5 @@ def utility_processor():
async def set_language(language=None):
if language in LANGUAGES:
session['language'] = language
if request.referrer is not None:
return redirect(request.referrer)
return redirect(url_for('web.index'))
return redirect('../#')
return abort(404)

View File

@@ -1,4 +1,5 @@
from quart import render_template, url_for
from quart import render_template
from .wrapper import url_for
from . import web

27
app/src/web/wrapper.py Normal file
View File

@@ -0,0 +1,27 @@
from quart import url_for as quart_url_for
from . import web
def url_for(*args, **kwargs):
"""Return the url for a specific endpoint.
This wrapper optionally convert into a relative url.
This is most useful in templates and redirects to create a URL
that can be used in the browser.
Arguments:
endpoint: The endpoint to build a url for, if prefixed with
``.`` it targets endpoint's in the current blueprint.
_anchor: Additional anchor text to append (i.e. #text).
_external: Return an absolute url for external (to app) usage.
_method: The method to consider alongside the endpoint.
_scheme: A specific scheme to use.
values: The values to build into the URL, as specified in
the endpoint rule.
"""
url = quart_url_for(*args, **kwargs)
print(f"wrapper url_for: {url}")
if '/' == url[0] and web.build_relative_urls:
url = '.' + url
return url

View File

@@ -30,4 +30,4 @@ cd /home/proxy || exit
export VERSION=$(cat /proxy-version.txt)
echo "Start Proxyserver..."
python3 server.py --json_config=/data/options.json --log_path=/homeassistant/tsun-proxy/logs/ --config_path=/homeassistant/tsun-proxy/ --log_backups=2
python3 server.py --rel_urls=True --json_config=/data/options.json --log_path=/homeassistant/tsun-proxy/logs/ --config_path=/homeassistant/tsun-proxy/ --log_backups=2