build relative urls for HA ingress
This commit is contained in:
@@ -16,6 +16,8 @@ from cnf.config_read_env import ConfigReadEnv
|
|||||||
from cnf.config_read_toml import ConfigReadToml
|
from cnf.config_read_toml import ConfigReadToml
|
||||||
from cnf.config_read_json import ConfigReadJson
|
from cnf.config_read_json import ConfigReadJson
|
||||||
from web import Web
|
from web import Web
|
||||||
|
from web.wrapper import url_for
|
||||||
|
|
||||||
from modbus_tcp import ModbusTcp
|
from modbus_tcp import ModbusTcp
|
||||||
|
|
||||||
|
|
||||||
@@ -34,8 +36,8 @@ class ProxyState:
|
|||||||
app = Quart(__name__,
|
app = Quart(__name__,
|
||||||
template_folder='web/templates',
|
template_folder='web/templates',
|
||||||
static_folder='web/static')
|
static_folder='web/static')
|
||||||
Web(app, '../translations')
|
|
||||||
app.secret_key = 'JKLdks.dajlKKKdladkflKwolafallsdfl'
|
app.secret_key = 'JKLdks.dajlKKKdladkflKwolafallsdfl'
|
||||||
|
app.jinja_env.globals.update(url_for=url_for)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/-/ready')
|
@app.route('/-/ready')
|
||||||
@@ -130,6 +132,12 @@ def main(): # pragma: no cover
|
|||||||
parser.add_argument('-b', '--log_backups', type=int,
|
parser.add_argument('-b', '--log_backups', type=int,
|
||||||
default=0,
|
default=0,
|
||||||
help='set max number of daily log-files')
|
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()
|
args = parser.parse_args()
|
||||||
#
|
#
|
||||||
# Setup our daily, rotating logger
|
# 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"config_path: {args.config_path}")
|
||||||
logging.info(f"json_config: {args.json_config}")
|
logging.info(f"json_config: {args.json_config}")
|
||||||
logging.info(f"toml_config: {args.toml_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}")
|
logging.info(f"log_path: {args.log_path}")
|
||||||
if args.log_backups == 0:
|
if args.log_backups == 0:
|
||||||
logging.info("log_backups: unlimited")
|
logging.info("log_backups: unlimited")
|
||||||
@@ -186,6 +196,7 @@ def main(): # pragma: no cover
|
|||||||
Proxy.class_init()
|
Proxy.class_init()
|
||||||
Schedule.start()
|
Schedule.start()
|
||||||
ModbusTcp(loop)
|
ModbusTcp(loop)
|
||||||
|
Web(app, args.trans_path, args.rel_urls)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Create tasks for our listening servers. These must be tasks! If we call
|
# Create tasks for our listening servers. These must be tasks! If we call
|
||||||
@@ -202,7 +213,8 @@ def main(): # pragma: no cover
|
|||||||
try:
|
try:
|
||||||
ProxyState.set_up(True)
|
ProxyState.set_up(True)
|
||||||
logging.info("Start Quart")
|
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")
|
logging.info("Quart stopped")
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ load_modules(__loader__)
|
|||||||
class Web:
|
class Web:
|
||||||
'''Helper Class to register the Blueprint at Quart and
|
'''Helper Class to register the Blueprint at Quart and
|
||||||
initializing Babel'''
|
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)
|
app.register_blueprint(web)
|
||||||
|
|
||||||
from .i18n import get_locale, get_tz
|
from .i18n import get_locale, get_tz
|
||||||
|
|||||||
@@ -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 quart_babel.locale import get_locale as babel_get_locale
|
||||||
|
|
||||||
from . import web
|
from . import web
|
||||||
@@ -38,7 +38,5 @@ def utility_processor():
|
|||||||
async def set_language(language=None):
|
async def set_language(language=None):
|
||||||
if language in LANGUAGES:
|
if language in LANGUAGES:
|
||||||
session['language'] = language
|
session['language'] = language
|
||||||
if request.referrer is not None:
|
return redirect('../#')
|
||||||
return redirect(request.referrer)
|
|
||||||
return redirect(url_for('web.index'))
|
|
||||||
return abort(404)
|
return abort(404)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from quart import render_template, url_for
|
from quart import render_template
|
||||||
|
from .wrapper import url_for
|
||||||
|
|
||||||
from . import web
|
from . import web
|
||||||
|
|
||||||
|
|||||||
27
app/src/web/wrapper.py
Normal file
27
app/src/web/wrapper.py
Normal 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
|
||||||
@@ -30,4 +30,4 @@ cd /home/proxy || exit
|
|||||||
export VERSION=$(cat /proxy-version.txt)
|
export VERSION=$(cat /proxy-version.txt)
|
||||||
|
|
||||||
echo "Start Proxyserver..."
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user