fastapi-cli icon indicating copy to clipboard operation
fastapi-cli copied to clipboard

feat: Enable configuration via pyproject.toml and environment variables

Open mattmess1221 opened this issue 1 year ago • 2 comments

This change allows configuration using both environment variables and a pyproject.toml file.

Environment variables

This takes advantage of the builtin functionality in click for loading options from environment variables. The prefix is FASTAPI.

Example:

$ export FASTAPI_DEV_PATH="server/app.py" FASTAPI_DEV_PORT="8001"
$ fastapi dev

Environment variables for each option are listed in the --help text.

Project configuration

If a pyproject.toml file exists in the current directory and contains the table tool.fastapi.cli, its keys will be merged into the click parameters. If a subtable matching the command exists, its keys will also be merged into the parameters, allowing for more precise configuration.

Example:

[tool.fastapi.cli]
app = "server/app.py"

[tool.fastapi.cli.run]
workers = 4

mattmess1221 avatar May 08 '24 00:05 mattmess1221

My PR seems to have exposed a bug in the tests when importing the app. I fixed it in 260242eb477c3431a0952096be4ede9c04859e94. Feel free to cherry-pick it.

mattmess1221 avatar May 08 '24 01:05 mattmess1221