Skip to content

Commit ee1f9dc

Browse files
committed
feat: handle both FASTAPI_PORT and PORT, with FASTAPI_PORT taking precedence
Signed-off-by: Damien Degois <damien@degois.info>
1 parent 5539f29 commit ee1f9dc

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/fastapi_cli/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def dev(
211211
int,
212212
typer.Option(
213213
help="The port to serve on. You would normally have a termination proxy on top (another program) handling HTTPS on port [blue]443[/blue] and HTTP on port [blue]80[/blue], transferring the communication to your app.",
214-
envvar="FASTAPI_PORT",
214+
envvar=["FASTAPI_PORT", "PORT"],
215215
),
216216
] = 8000,
217217
reload: Annotated[
@@ -319,7 +319,7 @@ def run(
319319
int,
320320
typer.Option(
321321
help="The port to serve on. You would normally have a termination proxy on top (another program) handling HTTPS on port [blue]443[/blue] and HTTP on port [blue]80[/blue], transferring the communication to your app.",
322-
envvar="FASTAPI_PORT",
322+
envvar=["FASTAPI_PORT", "PORT"],
323323
),
324324
] = 8000,
325325
reload: Annotated[

tests/test_cli.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ def test_dev_env_vars() -> None:
162162
)
163163

164164

165+
def test_dev_env_vars_port() -> None:
166+
with changing_dir(assets_path):
167+
with patch.object(uvicorn, "run") as mock_run:
168+
result = runner.invoke(
169+
app,
170+
["dev", "single_file_app.py"],
171+
env={"PORT": "8111"},
172+
)
173+
assert result.exit_code == 0, result.output
174+
assert mock_run.call_args.kwargs["port"] == 8111
175+
176+
177+
def test_dev_env_vars_port_precedence() -> None:
178+
with changing_dir(assets_path):
179+
with patch.object(uvicorn, "run") as mock_run:
180+
result = runner.invoke(
181+
app,
182+
["dev", "single_file_app.py"],
183+
env={
184+
"PORT": "8111",
185+
"FASTAPI_PORT": "8112",
186+
},
187+
)
188+
assert result.exit_code == 0, result.output
189+
assert mock_run.call_args.kwargs["port"] == 8112
190+
191+
165192
def test_dev_env_vars_and_args() -> None:
166193
with changing_dir(assets_path):
167194
with patch.object(uvicorn, "run") as mock_run:
@@ -337,6 +364,33 @@ def test_run_env_vars() -> None:
337364
assert "Documentation at http://192.168.1.1:8111/docs" in result.output
338365

339366

367+
def test_run_env_vars_port() -> None:
368+
with changing_dir(assets_path):
369+
with patch.object(uvicorn, "run") as mock_run:
370+
result = runner.invoke(
371+
app,
372+
["run", "single_file_app.py"],
373+
env={"PORT": "8111"},
374+
)
375+
assert result.exit_code == 0, result.output
376+
assert mock_run.call_args.kwargs["port"] == 8111
377+
378+
379+
def test_run_env_vars_port_precedence() -> None:
380+
with changing_dir(assets_path):
381+
with patch.object(uvicorn, "run") as mock_run:
382+
result = runner.invoke(
383+
app,
384+
["run", "single_file_app.py"],
385+
env={
386+
"PORT": "8111",
387+
"FASTAPI_PORT": "8112",
388+
},
389+
)
390+
assert result.exit_code == 0, result.output
391+
assert mock_run.call_args.kwargs["port"] == 8112
392+
393+
340394
def test_run_env_vars_and_args() -> None:
341395
with changing_dir(assets_path):
342396
with patch.object(uvicorn, "run") as mock_run:

0 commit comments

Comments
 (0)