From 14c856520e59fa55686514c22fbee5094c877e7b Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Mon, 7 Aug 2023 09:16:21 +0200 Subject: [PATCH] server: get default port optionally form environment var If the default port 8080 is taken, you have to either change the app.run() or use --port on every invocation of the program. When trying out examples I don't want to do the first and forget the second. This patch allows you set the environment variables LONA_DEFAULT_HOST and LONA_DEFAULT_PORT in your shell. These override the built-in defaults ('localhost' resp. 8080), and are in turn overridden by arguments to ``app.run()`` and startup commandline parameters. --- .../tutorial/01-getting-started/index.rst | 3 +++ lona/app.py | 18 ++++++++++++++++-- lona/command_line/handle_command_line.py | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/doc/content/tutorial/01-getting-started/index.rst b/doc/content/tutorial/01-getting-started/index.rst index 41e1ff65..73c2b162 100644 --- a/doc/content/tutorial/01-getting-started/index.rst +++ b/doc/content/tutorial/01-getting-started/index.rst @@ -101,6 +101,9 @@ Run the script using: ``app.run()`` takes keywords like ``host`` or ``port`` for configuration, and also parses the command line. Run ``python example.py -h`` to print the help. +If port 8080 is taken by another application, you can set environment +variable ``LONA_DEFAULT_PORT`` to some other port, affecting all examples that +do not set the port explicitly. The script should print that it opened a webserver on ``http://localhost:8080``. If you navigate your browser there, you should see diff --git a/lona/app.py b/lona/app.py index a7c20996..d5009a50 100644 --- a/lona/app.py +++ b/lona/app.py @@ -7,6 +7,7 @@ from os import PathLike import contextlib import logging +import sys import os from typing_extensions import Literal @@ -299,12 +300,14 @@ def parse_command_line(self) -> dict: from lona.command_line.handle_command_line import ( parse_overrides, DESCRIPTION, + EPILOG, ) parser = ArgumentParser( prog=str(self.script_path), formatter_class=RawTextHelpFormatter, description=DESCRIPTION, + epilog=EPILOG, ) parser.add_argument( @@ -427,9 +430,20 @@ def run( ) -> None: # setup arguments + port_env_var = 'LONA_DEFAULT_PORT' + try: + port = int(os.environ.get(port_env_var, 8080)) + except ValueError: + print( + 'value "{}" for environment variable "{}" cannot be converted to an integer port number'.format( # NOQA: LN001,FS002 + os.environ.get(port_env_var), port_env_var, + ), + ) + sys.exit(2) # same exit value as when specifying "--port abc" + server_args = Namespace( - host='localhost', - port=8080, + host=os.environ.get('LONA_DEFAULT_HOST', 'localhost'), + port=port, shell_server_url='', shutdown_timeout=0, log_level='info', diff --git a/lona/command_line/handle_command_line.py b/lona/command_line/handle_command_line.py index 66220fd0..a6b8d599 100644 --- a/lona/command_line/handle_command_line.py +++ b/lona/command_line/handle_command_line.py @@ -16,6 +16,11 @@ Code: https://github.com/lona-web-org/lona """.strip() +EPILOG = """\ +You can set environment variables LONA_DEFAULT_HOST and LONA_DEFAULT_PORT, +to override built-in defaults ('localhost' resp. '8080'). +""".rstrip() + def parse_overrides(raw_overrides): environment = Environment()