Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebSocket Configuration Options #61

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
85 changes: 85 additions & 0 deletions src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from logging import getLogger
from pathlib import Path
from typing import Any, Union
Expand All @@ -16,6 +17,14 @@

app = typer.Typer(rich_markup_mode="rich")


class WSProtocolType(str, Enum):
auto = "auto"
none = "none"
websockets = "websockets"
wsproto = "wsproto"


setup_logging()
logger = getLogger(__name__)

Expand Down Expand Up @@ -60,6 +69,12 @@ def _run(
command: str,
app: Union[str, None] = None,
proxy_headers: bool = False,
ws: WSProtocolType = WSProtocolType.auto,
ws_max_size: int = 16777216,
ws_max_queue: int = 32,
ws_ping_interval: float = 20.0,
ws_ping_timeout: float = 20.0,
ws_per_message_deflate: bool = True,
) -> None:
try:
use_uvicorn_app = get_import_string(path=path, app_name=app)
Expand Down Expand Up @@ -97,6 +112,12 @@ def _run(
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
ws=ws.value,
ws_max_size=ws_max_size,
ws_max_queue=ws_max_queue,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
ws_per_message_deflate=ws_per_message_deflate,
)


Expand Down Expand Up @@ -145,6 +166,32 @@ def dev(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
ws: Annotated[
WSProtocolType,
typer.Option(
help="The WebSocket protocol.", case_sensitive=False, show_choices=True
),
] = WSProtocolType.auto,
ws_max_size: Annotated[
int,
typer.Option(help="WebSocket max size message in bytes."),
] = 16777216,
ws_max_queue: Annotated[
int,
typer.Option(help="The maximum length of the WebSocket message queue."),
] = 32,
ws_ping_interval: Annotated[
float,
typer.Option(help="WebSocket ping interval in seconds."),
] = 20.0,
ws_ping_timeout: Annotated[
float,
typer.Option(help="WebSocket ping timeout in seconds."),
] = 20.0,
ws_per_message_deflate: Annotated[
bool,
typer.Option(help="WebSocket per-message-deflate compression"),
] = True,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
Expand Down Expand Up @@ -180,6 +227,12 @@ def dev(
app=app,
command="dev",
proxy_headers=proxy_headers,
ws=ws,
ws_max_size=ws_max_size,
ws_max_queue=ws_max_queue,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
ws_per_message_deflate=ws_per_message_deflate,
)


Expand Down Expand Up @@ -234,6 +287,32 @@ def run(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
ws: Annotated[
WSProtocolType,
typer.Option(
help="The WebSocket protocol.", case_sensitive=False, show_choices=True
),
] = WSProtocolType.auto,
ws_max_size: Annotated[
int,
typer.Option(help="WebSocket max size message in bytes."),
] = 16777216,
ws_max_queue: Annotated[
int,
typer.Option(help="The maximum length of the WebSocket message queue."),
] = 32,
ws_ping_interval: Annotated[
float,
typer.Option(help="WebSocket ping interval in seconds."),
] = 20.0,
ws_ping_timeout: Annotated[
float,
typer.Option(help="WebSocket ping timeout in seconds."),
] = 20.0,
ws_per_message_deflate: Annotated[
bool,
typer.Option(help="WebSocket per-message-deflate compression"),
] = True,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
Expand Down Expand Up @@ -270,6 +349,12 @@ def run(
app=app,
command="run",
proxy_headers=proxy_headers,
ws=ws,
ws_max_size=ws_max_size,
ws_max_queue=ws_max_queue,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
ws_per_message_deflate=ws_per_message_deflate,
)


Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def test_dev() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"ws": "auto",
"ws_max_size": 16777216,
"ws_max_queue": 32,
"ws_ping_interval": 20.0,
"ws_ping_timeout": 20.0,
"ws_per_message_deflate": True,
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -58,6 +64,8 @@ def test_dev_args() -> None:
"--app",
"api",
"--no-proxy-headers",
"--ws",
"auto",
],
)
assert result.exit_code == 0, result.output
Expand All @@ -71,6 +79,12 @@ def test_dev_args() -> None:
"workers": None,
"root_path": "/api",
"proxy_headers": False,
"ws": "auto",
"ws_max_size": 16777216,
"ws_max_queue": 32,
"ws_ping_interval": 20.0,
"ws_ping_timeout": 20.0,
"ws_per_message_deflate": True,
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand All @@ -97,6 +111,12 @@ def test_run() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"ws": "auto",
"ws_max_size": 16777216,
"ws_max_queue": 32,
"ws_ping_interval": 20.0,
"ws_ping_timeout": 20.0,
"ws_per_message_deflate": True,
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -128,6 +148,8 @@ def test_run_args() -> None:
"--app",
"api",
"--no-proxy-headers",
"--ws",
"websockets",
],
)
assert result.exit_code == 0, result.output
Expand All @@ -141,6 +163,12 @@ def test_run_args() -> None:
"workers": 2,
"root_path": "/api",
"proxy_headers": False,
"ws": "websockets",
"ws_max_size": 16777216,
"ws_max_queue": 32,
"ws_ping_interval": 20.0,
"ws_ping_timeout": 20.0,
"ws_per_message_deflate": True,
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand Down
Loading