Skip to content

Commit

Permalink
Enable read of uvicorn settings from environment variables (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 6, 2021
1 parent 825aeba commit 9c1b88c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ equivalent keyword arguments, eg. `uvicorn.run("example:app", port=5000, reload=
Please note that in this case, if you use `reload=True` or `workers=NUM`,
you should put `uvicorn.run` into `if __name__ == '__main__'` clause in the main module.

You can also configure Uvicorn using environment variables with the prefix `UVICORN_`.
For example, in case you want to run the app on port `5000`, just set the environment variable `UVICORN_PORT` to `5000`.

!!! note
CLI options and the arguments for `uvicorn.run()` take precedence over environment variables.


## Application

* `APP` - The ASGI application to run, in the format `"<module>:<attribute>"`.
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nav:
- Contributing: "contributing.md"

markdown_extensions:
- admonition
- codehilite:
css_class: highlight
- toc:
Expand Down
30 changes: 28 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
import os
import platform
import sys
from pathlib import Path
Expand All @@ -17,6 +18,10 @@
main = importlib.import_module("uvicorn.main")


class App:
pass


def test_cli_print_version() -> None:
runner = CliRunner()

Expand Down Expand Up @@ -131,5 +136,26 @@ def test_cli_reloader_incomplete_app_parameter(
) in captured.err


class App:
pass
@pytest.fixture()
def load_env_h11_protocol():
old_environ = dict(os.environ)
os.environ["UVICORN_HTTP"] = "h11"
yield
os.environ.clear()
os.environ.update(old_environ)


def test_env_variables(load_env_h11_protocol: None):
runner = CliRunner(env=os.environ)
with mock.patch.object(main, "run") as mock_run:
runner.invoke(cli, ["tests.test_cli:App"])
_, kwargs = mock_run.call_args
assert kwargs["http"] == "h11"


def test_mistmatch_env_variables(load_env_h11_protocol: None):
runner = CliRunner(env=os.environ)
with mock.patch.object(main, "run") as mock_run:
runner.invoke(cli, ["tests.test_cli:App", "--http=httptools"])
_, kwargs = mock_run.call_args
assert kwargs["http"] == "httptools"
2 changes: 1 addition & 1 deletion uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No
ctx.exit()


@click.command()
@click.command(context_settings={"auto_envvar_prefix": "UVICORN"})
@click.argument("app")
@click.option(
"--host",
Expand Down

0 comments on commit 9c1b88c

Please sign in to comment.