Skip to content

Commit

Permalink
get default port optionally form environment var
Browse files Browse the repository at this point in the history
If the default port 8080 is taken, you have to either change the app.run()
or  use --port <PORTNUM> 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.
  • Loading branch information
AvdN committed Aug 7, 2023
1 parent 7f8ba4c commit abed853
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/content/tutorial/01-getting-started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lona/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,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(
Expand Down Expand Up @@ -428,8 +430,8 @@ def run(

# setup arguments
server_args = Namespace(
host='localhost',
port=8080,
host=os.environ.get('LONA_DEFAULT_HOST', 'localhost'),
port=int(os.environ.get('LONA_DEFAULT_PORT', 8080)),
shell_server_url='',
shutdown_timeout=0,
log_level='info',
Expand Down
4 changes: 4 additions & 0 deletions lona/command_line/handle_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
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()
Expand Down

0 comments on commit abed853

Please sign in to comment.