|
| 1 | +import asyncio |
| 2 | +from enum import Enum |
1 | 3 | from logging import getLogger |
2 | 4 | from pathlib import Path |
3 | 5 | from typing import Any, Union |
|
16 | 18 |
|
17 | 19 | app = typer.Typer(rich_markup_mode="rich") |
18 | 20 |
|
| 21 | + |
| 22 | +class WSProtocolType(str, Enum): |
| 23 | + auto = "auto" |
| 24 | + none = "none" |
| 25 | + websockets = "websockets" |
| 26 | + wsproto = "wsproto" |
| 27 | + |
| 28 | + |
19 | 29 | setup_logging() |
20 | 30 | logger = getLogger(__name__) |
21 | 31 |
|
@@ -60,6 +70,12 @@ def _run( |
60 | 70 | command: str, |
61 | 71 | app: Union[str, None] = None, |
62 | 72 | proxy_headers: bool = False, |
| 73 | + ws: type[asyncio.Protocol] | WSProtocolType = "auto", |
| 74 | + ws_max_size: int = 16777216, |
| 75 | + ws_max_queue: int = 32, |
| 76 | + ws_ping_interval: float = 20.0, |
| 77 | + ws_ping_timeout: float = 20.0, |
| 78 | + ws_per_message_deflate: bool = True, |
63 | 79 | ) -> None: |
64 | 80 | try: |
65 | 81 | use_uvicorn_app = get_import_string(path=path, app_name=app) |
@@ -97,6 +113,12 @@ def _run( |
97 | 113 | workers=workers, |
98 | 114 | root_path=root_path, |
99 | 115 | proxy_headers=proxy_headers, |
| 116 | + ws=ws, |
| 117 | + ws_max_size=ws_max_size, |
| 118 | + ws_max_queue=ws_max_queue, |
| 119 | + ws_ping_interval=ws_ping_interval, |
| 120 | + ws_ping_timeout=ws_ping_timeout, |
| 121 | + ws_per_message_deflate=ws_per_message_deflate, |
100 | 122 | ) |
101 | 123 |
|
102 | 124 |
|
@@ -145,6 +167,32 @@ def dev( |
145 | 167 | help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info." |
146 | 168 | ), |
147 | 169 | ] = True, |
| 170 | + ws: Annotated[ |
| 171 | + WSProtocolType, |
| 172 | + typer.Option( |
| 173 | + help="The WebSocket protocol.", case_sensitive=False, show_choices=True |
| 174 | + ), |
| 175 | + ] = WSProtocolType.auto, |
| 176 | + ws_max_size: Annotated[ |
| 177 | + int, |
| 178 | + typer.Option(help="WebSocket max size message in bytes."), |
| 179 | + ] = 16777216, |
| 180 | + ws_max_queue: Annotated[ |
| 181 | + int, |
| 182 | + typer.Option(help="The maximum length of the WebSocket message queue."), |
| 183 | + ] = 32, |
| 184 | + ws_ping_interval: Annotated[ |
| 185 | + float, |
| 186 | + typer.Option(help="WebSocket ping interval in seconds."), |
| 187 | + ] = 20.0, |
| 188 | + ws_ping_timeout: Annotated[ |
| 189 | + float, |
| 190 | + typer.Option(help="WebSocket ping timeout in seconds."), |
| 191 | + ] = 20.0, |
| 192 | + ws_per_message_deflate: Annotated[ |
| 193 | + bool, |
| 194 | + typer.Option(help="WebSocket per-message-deflate compression"), |
| 195 | + ] = True, |
148 | 196 | ) -> Any: |
149 | 197 | """ |
150 | 198 | Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪 |
@@ -180,6 +228,12 @@ def dev( |
180 | 228 | app=app, |
181 | 229 | command="dev", |
182 | 230 | proxy_headers=proxy_headers, |
| 231 | + ws=ws, |
| 232 | + ws_max_size=ws_max_size, |
| 233 | + ws_max_queue=ws_max_queue, |
| 234 | + ws_ping_interval=ws_ping_interval, |
| 235 | + ws_ping_timeout=ws_ping_timeout, |
| 236 | + ws_per_message_deflate=ws_per_message_deflate, |
183 | 237 | ) |
184 | 238 |
|
185 | 239 |
|
@@ -234,6 +288,32 @@ def run( |
234 | 288 | help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info." |
235 | 289 | ), |
236 | 290 | ] = True, |
| 291 | + ws: Annotated[ |
| 292 | + WSProtocolType, |
| 293 | + typer.Option( |
| 294 | + help="The WebSocket protocol.", case_sensitive=False, show_choices=True |
| 295 | + ), |
| 296 | + ] = WSProtocolType.auto, |
| 297 | + ws_max_size: Annotated[ |
| 298 | + int, |
| 299 | + typer.Option(help="WebSocket max size message in bytes."), |
| 300 | + ] = 16777216, |
| 301 | + ws_max_queue: Annotated[ |
| 302 | + int, |
| 303 | + typer.Option(help="The maximum length of the WebSocket message queue."), |
| 304 | + ] = 32, |
| 305 | + ws_ping_interval: Annotated[ |
| 306 | + float, |
| 307 | + typer.Option(help="WebSocket ping interval in seconds."), |
| 308 | + ] = 20.0, |
| 309 | + ws_ping_timeout: Annotated[ |
| 310 | + float, |
| 311 | + typer.Option(help="WebSocket ping timeout in seconds."), |
| 312 | + ] = 20.0, |
| 313 | + ws_per_message_deflate: Annotated[ |
| 314 | + bool, |
| 315 | + typer.Option(help="WebSocket per-message-deflate compression"), |
| 316 | + ] = True, |
237 | 317 | ) -> Any: |
238 | 318 | """ |
239 | 319 | Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀 |
@@ -270,6 +350,12 @@ def run( |
270 | 350 | app=app, |
271 | 351 | command="run", |
272 | 352 | proxy_headers=proxy_headers, |
| 353 | + ws=ws, |
| 354 | + ws_max_size=ws_max_size, |
| 355 | + ws_max_queue=ws_max_queue, |
| 356 | + ws_ping_interval=ws_ping_interval, |
| 357 | + ws_ping_timeout=ws_ping_timeout, |
| 358 | + ws_per_message_deflate=ws_per_message_deflate, |
273 | 359 | ) |
274 | 360 |
|
275 | 361 |
|
|
0 commit comments