Skip to content

Commit 8a28583

Browse files
committed
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.
1 parent 9a47418 commit 8a28583

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/fastapi_cli/cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def _run(
8686
command: str,
8787
app: Union[str, None] = None,
8888
proxy_headers: bool = False,
89+
server_header: bool = False,
8990
) -> None:
9091
with get_rich_toolkit() as toolkit:
9192
server_type = "development" if command == "dev" else "production"
@@ -168,6 +169,7 @@ def _run(
168169
root_path=root_path,
169170
proxy_headers=proxy_headers,
170171
log_config=get_uvicorn_log_config(),
172+
server_header=server_header,
171173
)
172174

173175

@@ -216,6 +218,10 @@ def dev(
216218
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
217219
),
218220
] = True,
221+
server_header: Annotated[
222+
bool,
223+
typer.Option(help="Enable/Disable Server header."),
224+
] = False,
219225
) -> Any:
220226
"""
221227
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -251,6 +257,7 @@ def dev(
251257
app=app,
252258
command="dev",
253259
proxy_headers=proxy_headers,
260+
server_header=server_header,
254261
)
255262

256263

@@ -305,6 +312,10 @@ def run(
305312
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
306313
),
307314
] = True,
315+
server_header: Annotated[
316+
bool,
317+
typer.Option(help="Enable/Disable Server header."),
318+
] = False,
308319
) -> Any:
309320
"""
310321
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -341,6 +352,7 @@ def run(
341352
app=app,
342353
command="run",
343354
proxy_headers=proxy_headers,
355+
server_header=server_header,
344356
)
345357

346358

tests/test_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_dev() -> None:
3131
"root_path": "",
3232
"proxy_headers": True,
3333
"log_config": get_uvicorn_log_config(),
34+
"server_header": False,
3435
}
3536
assert "Using import string: single_file_app:app" in result.output
3637
assert "Starting development server 🚀" in result.output
@@ -60,6 +61,7 @@ def test_dev_package() -> None:
6061
"root_path": "",
6162
"proxy_headers": True,
6263
"log_config": get_uvicorn_log_config(),
64+
"server_header": False,
6365
}
6466
assert "Using import string: nested_package.package:app" in result.output
6567
assert "Starting development server 🚀" in result.output
@@ -94,6 +96,7 @@ def test_dev_args() -> None:
9496
"--app",
9597
"api",
9698
"--no-proxy-headers",
99+
"--server-header",
97100
],
98101
)
99102
assert result.exit_code == 0, result.output
@@ -108,6 +111,7 @@ def test_dev_args() -> None:
108111
"root_path": "/api",
109112
"proxy_headers": False,
110113
"log_config": get_uvicorn_log_config(),
114+
"server_header": True,
111115
}
112116
assert "Using import string: single_file_app:api" in result.output
113117
assert "Starting development server 🚀" in result.output
@@ -135,6 +139,7 @@ def test_run() -> None:
135139
"root_path": "",
136140
"proxy_headers": True,
137141
"log_config": get_uvicorn_log_config(),
142+
"server_header": False,
138143
}
139144
assert "Using import string: single_file_app:app" in result.output
140145
assert "Starting production server 🚀" in result.output
@@ -166,6 +171,7 @@ def test_run_args() -> None:
166171
"--app",
167172
"api",
168173
"--no-proxy-headers",
174+
"--server-header",
169175
],
170176
)
171177
assert result.exit_code == 0, result.output
@@ -180,6 +186,7 @@ def test_run_args() -> None:
180186
"root_path": "/api",
181187
"proxy_headers": False,
182188
"log_config": get_uvicorn_log_config(),
189+
"server_header": True,
183190
}
184191

185192
assert "Using import string: single_file_app:api" in result.output

0 commit comments

Comments
 (0)