taler-merchant-demos

Python-based Frontends for the Demonstration Web site
Log | Files | Refs | Submodules | README | LICENSE

commit 0ec9dfd14ea1ecf22666eb6b6a12ca748b0591c1
parent efa382e984ee05aabd42d9c5830ccb4699aa7925
Author: Florian Dold <florian@dold.me>
Date:   Tue,  3 Sep 2024 14:49:41 +0200

separate backend config sections for each demo

Diffstat:
Mdebian/changelog | 6++++++
Mtalermerchantdemos/cli.py | 121++++++++++++++++++++++---------------------------------------------------------
2 files changed, 40 insertions(+), 87 deletions(-)

diff --git a/debian/changelog b/debian/changelog @@ -1,3 +1,9 @@ +taler-merchant-demos (0.13.0) unstable; urgency=medium + + * Release 0.13.0 + + -- Florian Dold <dold@taler.net> Tue, 03 Sep 2024 14:48:42 +0200 + taler-merchant-demos (0.10.1) unstable; urgency=medium * Release 0.10.1. diff --git a/talermerchantdemos/cli.py b/talermerchantdemos/cli.py @@ -38,59 +38,6 @@ arg_venvpath = ["-H", sys.prefix] # loading it causes an error. arg_load_python = "--if-not-plugin python --plugins python --endif".split(" ") -## -# This function interprets the 'serve-uwsgi' subcommand. -# The effect is to launch the blog UWSGI service. This -# type of service is usually used when the HTTP blog interface -# is accessed via a reverse proxy (like Nginx, for example). -# -# @param command line options. -def handle_serve_uwsgi(config, which_shop): - serve_uwsgi = config[which_shop]["uwsgi_serve"].value_string(required=True).lower() - params = [ - "uwsgi", - "uwsgi", - *arg_venvpath, - *arg_load_python, - "--master", - "--die-on-term", - "--log-format", - UWSGI_LOGFMT, - "--module", - "talermerchantdemos.{}:app".format(which_shop), - "--need-app", - "--set-ph", "backend_url={}".format( - config["frontends"]["backend"].value_string(required=True) - ), - "--set-ph", "currency={}".format( - config["taler"]["currency"].value_string(required=True) - ), - "--set-ph", "apikey={}".format( - config["frontends"]["backend_apikey"].value_string(required=True) - ), - "--module", - "--cache2", - "name=paid_articles,items=500" - ] - if serve_uwsgi == "tcp": - port = config[which_shop]["uwsgi_port"].value_int(required=True) - spec = ":%d" % (port,) - params.extend(["--socket", spec]) - elif serve_uwsgi == "unix": - spec = config[which_shop]["uwsgi_unixpath"].value_filename(required=True) - mode = config[which_shop]["uwsgi_unixpath_mode"].value_filename(required=True) - params.extend(["--socket", spec]) - params.extend(["--chmod-socket=" + mode]) - os.makedirs(os.path.dirname(spec), exist_ok=True) - logging.info("launching uwsgi with argv %s", params[1:]) - try: - os.execlp(*params) - except: - sys.stderr.write( - "Failed to start uwsgi. Please make sure to install uwsgi for Python3." - ) - sys.exit(1) - ## # This function interprets the 'serve-http' subcommand. @@ -98,6 +45,8 @@ def handle_serve_uwsgi(config, which_shop): # # @param args command line options. def handle_serve_http(config, which_shop, port=None): + confsection = f"frontend-demo-{which_shop}" + currency = config["taler"]["currency"].value_string(required=True) params = [ "uwsgi", "uwsgi", @@ -107,38 +56,48 @@ def handle_serve_http(config, which_shop, port=None): "--die-on-term", "--log-format", UWSGI_LOGFMT, - "--set-ph", "backend_url={}".format( - config["frontends"]["backend"].value_string(required=True) - ), - "--set-ph", "currency={}".format( - config["taler"]["currency"].value_string(required=True) - ), - "--set-ph", "apikey={}".format( - config["frontends"]["backend_apikey"].value_string(required=True) - ), + "--set-ph", + f"currency={currency}", "--module", - "talermerchantdemos.{}:app".format(which_shop), + f"talermerchantdemos.{which_shop}:app", ] - + + # Only read backend config if necessary + has_backend = which_shop in ("blog", "donations") + if has_backend: + backend_url = config[confsection]["backend"].value_string(required=True) + apikey = config[confsection]["backend_apikey"].value_string(required=True) + extra = [ + "--set-ph", + f"backend_url={backend_url}" "--set-ph", + f"apikey={apikey}", + ] + params.extend(extra) + # Takes precedence. if port: http_serve = "tcp" else: - http_serve = config[which_shop]["http_serve"].value_string( - required=False, - default="tcp" - ).lower() + http_serve = ( + config[confsection]["http_serve"] + .value_string(required=False, default="tcp") + .lower() + ) if http_serve == "tcp": - port_launch = config[which_shop]["http_port"].value_int(required=False) if not port else port + port_launch = ( + config[confsection]["http_port"].value_int(required=False) + if not port + else port + ) if not port_launch: sys.stderr.write("Port number wasn't found in config and in arguments.") exit(1) params.extend(["--http", f":{port_launch}"]) if http_serve == "unix": - path = config[which_shop]["http_unixpath"].value_filename(required=True) - mode = config[which_shop]["http_unixpath_mode"].value_filename(required=True) + path = config[confsection]["http_unixpath"].value_filename(required=True) + mode = config[confsection]["http_unixpath_mode"].value_filename(required=True) params.extend(["--http-socket", path]) params.extend(["--chmod-socket=" + mode]) os.makedirs(os.path.dirname(path), exist_ok=True) @@ -146,21 +105,11 @@ def handle_serve_http(config, which_shop, port=None): try: os.execlp(*params) except: - sys.stderr.write("Failed to start uwsgi. Please make sure to install uwsgi for Python3.") + sys.stderr.write( + "Failed to start uwsgi. Please make sure to install uwsgi for Python3." + ) sys.exit(1) -def handle_serve_from_config(config_obj, which_shop): - try: - if ( - config_obj.value_string(which_shop, "serve", required=True).lower() - == "http" - ): - return handle_serve_http(config_obj, which_shop) - handle_serve_uwsgi(config_obj, which_shop) - except ConfigurationError as ce: - print(ce) - exit(1) - @click.command("Global shop launcher") @click.option("-c", "--config", help="Configuration file", required=False) @@ -178,9 +127,7 @@ def demos(config, http_port, which_shop): print("Please use a valid shop name: blog, donations, landing.") sys.exit(1) config_obj = TalerConfig.from_file(config) - if http_port: - return handle_serve_http(config_obj, which_shop, http_port) - handle_serve_from_config(config_obj, which_shop) + handle_serve_http(config_obj, which_shop, http_port) demos()