From 8a28583e5c09959d6403e341721ba2ffe3ccd37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcello=20Silv=C3=A9rio?= Date: Sun, 4 May 2025 11:27:31 -0300 Subject: [PATCH] feat(cli): add server header option to dev and run commands Introduce a new `server_header` option to enable or disable the Server header in the FastAPI CLI for both development and production modes. This provides more control over HTTP response headers for security and customization purposes. --- src/fastapi_cli/cli.py | 12 ++++++++++++ tests/test_cli.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/src/fastapi_cli/cli.py b/src/fastapi_cli/cli.py index c983b5c..784929f 100644 --- a/src/fastapi_cli/cli.py +++ b/src/fastapi_cli/cli.py @@ -86,6 +86,7 @@ def _run( command: str, app: Union[str, None] = None, proxy_headers: bool = False, + server_header: bool = False, ) -> None: with get_rich_toolkit() as toolkit: server_type = "development" if command == "dev" else "production" @@ -168,6 +169,7 @@ def _run( root_path=root_path, proxy_headers=proxy_headers, log_config=get_uvicorn_log_config(), + server_header=server_header, ) @@ -216,6 +218,10 @@ def dev( help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info." ), ] = True, + server_header: Annotated[ + bool, + typer.Option(help="Enable/Disable Server header."), + ] = False, ) -> Any: """ Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. ๐Ÿงช @@ -251,6 +257,7 @@ def dev( app=app, command="dev", proxy_headers=proxy_headers, + server_header=server_header, ) @@ -305,6 +312,10 @@ def run( help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info." ), ] = True, + server_header: Annotated[ + bool, + typer.Option(help="Enable/Disable Server header."), + ] = False, ) -> Any: """ Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. ๐Ÿš€ @@ -341,6 +352,7 @@ def run( app=app, command="run", proxy_headers=proxy_headers, + server_header=server_header, ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8bdba1c..b58ffac 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -31,6 +31,7 @@ def test_dev() -> None: "root_path": "", "proxy_headers": True, "log_config": get_uvicorn_log_config(), + "server_header": False, } assert "Using import string: single_file_app:app" in result.output assert "Starting development server ๐Ÿš€" in result.output @@ -60,6 +61,7 @@ def test_dev_package() -> None: "root_path": "", "proxy_headers": True, "log_config": get_uvicorn_log_config(), + "server_header": False, } assert "Using import string: nested_package.package:app" in result.output assert "Starting development server ๐Ÿš€" in result.output @@ -94,6 +96,7 @@ def test_dev_args() -> None: "--app", "api", "--no-proxy-headers", + "--server-header", ], ) assert result.exit_code == 0, result.output @@ -108,6 +111,7 @@ def test_dev_args() -> None: "root_path": "/api", "proxy_headers": False, "log_config": get_uvicorn_log_config(), + "server_header": True, } assert "Using import string: single_file_app:api" in result.output assert "Starting development server ๐Ÿš€" in result.output @@ -135,6 +139,7 @@ def test_run() -> None: "root_path": "", "proxy_headers": True, "log_config": get_uvicorn_log_config(), + "server_header": False, } assert "Using import string: single_file_app:app" in result.output assert "Starting production server ๐Ÿš€" in result.output @@ -166,6 +171,7 @@ def test_run_args() -> None: "--app", "api", "--no-proxy-headers", + "--server-header", ], ) assert result.exit_code == 0, result.output @@ -180,6 +186,7 @@ def test_run_args() -> None: "root_path": "/api", "proxy_headers": False, "log_config": get_uvicorn_log_config(), + "server_header": True, } assert "Using import string: single_file_app:api" in result.output