Skip to content

Commit

Permalink
server: 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 14c8565
Show file tree
Hide file tree
Showing 3 changed files with 24 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
18 changes: 16 additions & 2 deletions lona/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from os import PathLike
import contextlib
import logging
import sys
import os

from typing_extensions import Literal
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 5 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,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()
Expand Down

0 comments on commit 14c8565

Please sign in to comment.