|
23 | 23 | """
|
24 | 24 | import os
|
25 | 25 | import argparse
|
| 26 | +import inspect |
26 | 27 | from typing import List, Literal, Union
|
27 | 28 |
|
28 | 29 | import uvicorn
|
@@ -91,10 +92,34 @@ def parse_bool_arg(arg):
|
91 | 92 | help=f"{description}",
|
92 | 93 | )
|
93 | 94 |
|
| 95 | + # Extract uvicorn.run function arguments to dynamically add them to argparse |
| 96 | + uvicorn_run_signature = inspect.signature(uvicorn.run) |
| 97 | + for param_name, param in uvicorn_run_signature.parameters.items(): |
| 98 | + # Skip the "app" argument since it will be provided internally |
| 99 | + if param_name == "app": |
| 100 | + continue |
| 101 | + |
| 102 | + # Handle special types and defaults |
| 103 | + param_type = param.annotation if param.annotation != inspect.Parameter.empty else str |
| 104 | + param_default = param.default if param.default != inspect.Parameter.empty else None |
| 105 | + |
| 106 | + # Add argument to argparse |
| 107 | + parser.add_argument( |
| 108 | + f"--uvicorn-{param_name}", |
| 109 | + type=param_type, |
| 110 | + default=param_default, |
| 111 | + help=f"Uvicorn setting for {param_name} (default: {repr(param_default).replace('%', '%%')})" |
| 112 | + ) |
| 113 | + |
94 | 114 | args = parser.parse_args()
|
95 |
| - settings = Settings(**{k: v for k, v in vars(args).items() if v is not None}) |
| 115 | + settings = Settings(**{k: v for k, v in vars(args).items() if v is not None and not k.startswith("uvicorn_")}) |
96 | 116 | app = create_app(settings=settings)
|
97 | 117 |
|
98 |
| - uvicorn.run( |
99 |
| - app, host=os.getenv("HOST", settings.host), port=int(os.getenv("PORT", settings.port)) |
100 |
| - ) |
| 118 | + # Separate out uvicorn arguments |
| 119 | + uvicorn_args = {k[8:]: v for k, v in vars(args).items() if k.startswith("uvicorn_") and v is not None} |
| 120 | + |
| 121 | + # Merge with other environment-based settings if needed |
| 122 | + uvicorn_args['host'] = os.getenv("HOST", uvicorn_args.get('host', "127.0.0.1")) |
| 123 | + uvicorn_args['port'] = int(os.getenv("PORT", uvicorn_args.get('port', 8000))) |
| 124 | + |
| 125 | + uvicorn.run(app, **uvicorn_args) |
0 commit comments