Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
from pathlib import Path
from typing import Annotated, Any

Expand Down Expand Up @@ -136,6 +137,7 @@ def _run(
entrypoint: str | None = None,
proxy_headers: bool = False,
forwarded_allow_ips: str | None = None,
public_url: str | None = None,
) -> None:
with get_rich_toolkit() as toolkit:
server_type = "development" if command == "dev" else "production"
Expand Down Expand Up @@ -243,7 +245,7 @@ def _run(
)
)

url = f"http://{host}:{port}"
url = public_url.rstrip("/") if public_url else f"http://{host}:{port}"
url_docs = f"{url}/docs"

Comment on lines +248 to 250

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this

toolkit.print_line()
Expand Down Expand Up @@ -391,6 +393,7 @@ def dev(
command="dev",
proxy_headers=proxy_headers,
forwarded_allow_ips=forwarded_allow_ips,
public_url=os.getenv("FASTAPI_PUBLIC_URL"),
)


Expand Down Expand Up @@ -498,6 +501,7 @@ def run(
command="run",
proxy_headers=proxy_headers,
forwarded_allow_ips=forwarded_allow_ips,
public_url=os.getenv("FASTAPI_PUBLIC_URL"),
)


Expand Down
49 changes: 49 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from unittest.mock import patch

import pytest
import uvicorn
from typer.testing import CliRunner

Expand Down Expand Up @@ -424,6 +425,54 @@ def test_run_env_vars_and_args() -> None:
assert "Documentation at http://0.0.0.0:8080/docs" in result.output


@pytest.mark.parametrize("command", ["dev", "run"])
@pytest.mark.parametrize(
"public_url",
[
"https://myapp.example.com",
"https://myapp.example.com/",
"https://myapp.example.com/subpath/",
],
)
Comment thread
YuriiMotov marked this conversation as resolved.
def test_public_url_env_var(command: str, public_url: str) -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app,
[
command,
"single_file_app.py",
"--host",
"0.0.0.0",
],
env={"FASTAPI_PUBLIC_URL": public_url},
)
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "0.0.0.0",
"port": 8000,
"reload": True if command == "dev" else False,
"reload_dirs": None,
"workers": None,
"root_path": "",
"proxy_headers": True,
"forwarded_allow_ips": None,
"log_config": get_uvicorn_log_config(),
}

assert "Using import string: single_file_app:app" in result.output
assert (
f"Starting {'development' if command == 'dev' else 'production'} server 🚀"
in result.output
)
expected_url_base = public_url.rstrip("/")
assert f"Server started at {expected_url_base}" in result.output
assert f"Documentation at {expected_url_base}/docs" in result.output


def test_run_error() -> None:
with changing_dir(assets_path):
result = runner.invoke(app, ["run", "non_existing_file.py"])
Expand Down