|
19 | 19 | SSL_PROTOCOL_VERSION,
|
20 | 20 | WS_PROTOCOLS,
|
21 | 21 | Config,
|
| 22 | + HTTPProtocolType, |
| 23 | + InterfaceType, |
| 24 | + LifespanType, |
| 25 | + LoopSetupType, |
| 26 | + WSProtocolType, |
22 | 27 | )
|
23 | 28 | from uvicorn.server import Server, ServerState # noqa: F401 # Used to be defined here.
|
24 | 29 | from uvicorn.supervisors import ChangeReload, Multiprocess
|
@@ -233,7 +238,7 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No
|
233 | 238 | "--forwarded-allow-ips",
|
234 | 239 | type=str,
|
235 | 240 | default=None,
|
236 |
| - help="Comma seperated list of IPs to trust with proxy headers. Defaults to" |
| 241 | + help="Comma separated list of IPs to trust with proxy headers. Defaults to" |
237 | 242 | " the $FORWARDED_ALLOW_IPS environment variable if available, or '127.0.0.1'.",
|
238 | 243 | )
|
239 | 244 | @click.option(
|
@@ -347,15 +352,15 @@ def main(
|
347 | 352 | port: int,
|
348 | 353 | uds: str,
|
349 | 354 | fd: int,
|
350 |
| - loop: str, |
351 |
| - http: str, |
352 |
| - ws: str, |
| 355 | + loop: LoopSetupType, |
| 356 | + http: HTTPProtocolType, |
| 357 | + ws: WSProtocolType, |
353 | 358 | ws_max_size: int,
|
354 | 359 | ws_ping_interval: float,
|
355 | 360 | ws_ping_timeout: float,
|
356 | 361 | ws_per_message_deflate: bool,
|
357 |
| - lifespan: str, |
358 |
| - interface: str, |
| 362 | + lifespan: LifespanType, |
| 363 | + interface: InterfaceType, |
359 | 364 | debug: bool,
|
360 | 365 | reload: bool,
|
361 | 366 | reload_dirs: typing.List[str],
|
@@ -388,61 +393,152 @@ def main(
|
388 | 393 | app_dir: str,
|
389 | 394 | factory: bool,
|
390 | 395 | ) -> None:
|
391 |
| - kwargs = { |
392 |
| - "host": host, |
393 |
| - "port": port, |
394 |
| - "uds": uds, |
395 |
| - "fd": fd, |
396 |
| - "loop": loop, |
397 |
| - "http": http, |
398 |
| - "ws": ws, |
399 |
| - "ws_max_size": ws_max_size, |
400 |
| - "ws_ping_interval": ws_ping_interval, |
401 |
| - "ws_ping_timeout": ws_ping_timeout, |
402 |
| - "ws_per_message_deflate": ws_per_message_deflate, |
403 |
| - "lifespan": lifespan, |
404 |
| - "env_file": env_file, |
405 |
| - "log_config": LOGGING_CONFIG if log_config is None else log_config, |
406 |
| - "log_level": log_level, |
407 |
| - "access_log": access_log, |
408 |
| - "interface": interface, |
409 |
| - "debug": debug, |
410 |
| - "reload": reload, |
411 |
| - "reload_dirs": reload_dirs if reload_dirs else None, |
412 |
| - "reload_includes": reload_includes if reload_includes else None, |
413 |
| - "reload_excludes": reload_excludes if reload_excludes else None, |
414 |
| - "reload_delay": reload_delay, |
415 |
| - "workers": workers, |
416 |
| - "proxy_headers": proxy_headers, |
417 |
| - "server_header": server_header, |
418 |
| - "date_header": date_header, |
419 |
| - "forwarded_allow_ips": forwarded_allow_ips, |
420 |
| - "root_path": root_path, |
421 |
| - "limit_concurrency": limit_concurrency, |
422 |
| - "backlog": backlog, |
423 |
| - "limit_max_requests": limit_max_requests, |
424 |
| - "timeout_keep_alive": timeout_keep_alive, |
425 |
| - "ssl_keyfile": ssl_keyfile, |
426 |
| - "ssl_certfile": ssl_certfile, |
427 |
| - "ssl_keyfile_password": ssl_keyfile_password, |
428 |
| - "ssl_version": ssl_version, |
429 |
| - "ssl_cert_reqs": ssl_cert_reqs, |
430 |
| - "ssl_ca_certs": ssl_ca_certs, |
431 |
| - "ssl_ciphers": ssl_ciphers, |
432 |
| - "headers": [header.split(":", 1) for header in headers], |
433 |
| - "use_colors": use_colors, |
434 |
| - "factory": factory, |
435 |
| - "app_dir": app_dir, |
436 |
| - } |
437 |
| - run(app, **kwargs) |
| 396 | + run( |
| 397 | + app, |
| 398 | + host=host, |
| 399 | + port=port, |
| 400 | + uds=uds, |
| 401 | + fd=fd, |
| 402 | + loop=loop, |
| 403 | + http=http, |
| 404 | + ws=ws, |
| 405 | + ws_max_size=ws_max_size, |
| 406 | + ws_ping_interval=ws_ping_interval, |
| 407 | + ws_ping_timeout=ws_ping_timeout, |
| 408 | + ws_per_message_deflate=ws_per_message_deflate, |
| 409 | + lifespan=lifespan, |
| 410 | + env_file=env_file, |
| 411 | + log_config=LOGGING_CONFIG if log_config is None else log_config, |
| 412 | + log_level=log_level, |
| 413 | + access_log=access_log, |
| 414 | + interface=interface, |
| 415 | + debug=debug, |
| 416 | + reload=reload, |
| 417 | + reload_dirs=reload_dirs if reload_dirs else None, |
| 418 | + reload_includes=reload_includes if reload_includes else None, |
| 419 | + reload_excludes=reload_excludes if reload_excludes else None, |
| 420 | + reload_delay=reload_delay, |
| 421 | + workers=workers, |
| 422 | + proxy_headers=proxy_headers, |
| 423 | + server_header=server_header, |
| 424 | + date_header=date_header, |
| 425 | + forwarded_allow_ips=forwarded_allow_ips, |
| 426 | + root_path=root_path, |
| 427 | + limit_concurrency=limit_concurrency, |
| 428 | + backlog=backlog, |
| 429 | + limit_max_requests=limit_max_requests, |
| 430 | + timeout_keep_alive=timeout_keep_alive, |
| 431 | + ssl_keyfile=ssl_keyfile, |
| 432 | + ssl_certfile=ssl_certfile, |
| 433 | + ssl_keyfile_password=ssl_keyfile_password, |
| 434 | + ssl_version=ssl_version, |
| 435 | + ssl_cert_reqs=ssl_cert_reqs, |
| 436 | + ssl_ca_certs=ssl_ca_certs, |
| 437 | + ssl_ciphers=ssl_ciphers, |
| 438 | + headers=[header.split(":", 1) for header in headers], # type: ignore[misc] |
| 439 | + use_colors=use_colors, |
| 440 | + factory=factory, |
| 441 | + app_dir=app_dir, |
| 442 | + ) |
438 | 443 |
|
439 | 444 |
|
440 |
| -def run(app: typing.Union[ASGIApplication, str], **kwargs: typing.Any) -> None: |
441 |
| - app_dir = kwargs.pop("app_dir", None) |
| 445 | +def run( |
| 446 | + app: typing.Union[ASGIApplication, str], |
| 447 | + *, |
| 448 | + host: str = "127.0.0.1", |
| 449 | + port: int = 8000, |
| 450 | + uds: typing.Optional[str] = None, |
| 451 | + fd: typing.Optional[int] = None, |
| 452 | + loop: LoopSetupType = "auto", |
| 453 | + http: HTTPProtocolType = "auto", |
| 454 | + ws: WSProtocolType = "auto", |
| 455 | + ws_max_size: int = 16777216, |
| 456 | + ws_ping_interval: float = 20.0, |
| 457 | + ws_ping_timeout: float = 20.0, |
| 458 | + ws_per_message_deflate: bool = True, |
| 459 | + lifespan: LifespanType = "auto", |
| 460 | + interface: InterfaceType = "auto", |
| 461 | + debug: bool = False, |
| 462 | + reload: bool = False, |
| 463 | + reload_dirs: typing.Optional[typing.List[str]] = None, |
| 464 | + reload_includes: typing.Optional[typing.List[str]] = None, |
| 465 | + reload_excludes: typing.Optional[typing.List[str]] = None, |
| 466 | + reload_delay: float = 0.25, |
| 467 | + workers: typing.Optional[int] = None, |
| 468 | + env_file: typing.Optional[str] = None, |
| 469 | + log_config: typing.Optional[typing.Union[dict, str]] = None, |
| 470 | + log_level: typing.Optional[str] = None, |
| 471 | + access_log: bool = True, |
| 472 | + proxy_headers: bool = True, |
| 473 | + server_header: bool = True, |
| 474 | + date_header: bool = True, |
| 475 | + forwarded_allow_ips: typing.Optional[str] = None, |
| 476 | + root_path: str = "", |
| 477 | + limit_concurrency: typing.Optional[int] = None, |
| 478 | + backlog: int = 2048, |
| 479 | + limit_max_requests: typing.Optional[int] = None, |
| 480 | + timeout_keep_alive: int = 5, |
| 481 | + ssl_keyfile: typing.Optional[str] = None, |
| 482 | + ssl_certfile: typing.Optional[str] = None, |
| 483 | + ssl_keyfile_password: typing.Optional[str] = None, |
| 484 | + ssl_version: int = int(SSL_PROTOCOL_VERSION), |
| 485 | + ssl_cert_reqs: int = int(ssl.CERT_NONE), |
| 486 | + ssl_ca_certs: typing.Optional[str] = None, |
| 487 | + ssl_ciphers: str = "TLSv1", |
| 488 | + headers: typing.Optional[typing.List[typing.Tuple[str, str]]] = None, |
| 489 | + use_colors: typing.Optional[bool] = None, |
| 490 | + app_dir: typing.Optional[str] = None, |
| 491 | + factory: bool = False, |
| 492 | +) -> None: |
442 | 493 | if app_dir is not None:
|
443 | 494 | sys.path.insert(0, app_dir)
|
444 | 495 |
|
445 |
| - config = Config(app, **kwargs) |
| 496 | + config = Config( |
| 497 | + app, |
| 498 | + host=host, |
| 499 | + port=port, |
| 500 | + uds=uds, |
| 501 | + fd=fd, |
| 502 | + loop=loop, |
| 503 | + http=http, |
| 504 | + ws=ws, |
| 505 | + ws_max_size=ws_max_size, |
| 506 | + ws_ping_interval=ws_ping_interval, |
| 507 | + ws_ping_timeout=ws_ping_timeout, |
| 508 | + ws_per_message_deflate=ws_per_message_deflate, |
| 509 | + lifespan=lifespan, |
| 510 | + interface=interface, |
| 511 | + debug=debug, |
| 512 | + reload=reload, |
| 513 | + reload_dirs=reload_dirs, |
| 514 | + reload_includes=reload_includes, |
| 515 | + reload_excludes=reload_excludes, |
| 516 | + reload_delay=reload_delay, |
| 517 | + workers=workers, |
| 518 | + env_file=env_file, |
| 519 | + log_config=log_config, |
| 520 | + log_level=log_level, |
| 521 | + access_log=access_log, |
| 522 | + proxy_headers=proxy_headers, |
| 523 | + server_header=server_header, |
| 524 | + date_header=date_header, |
| 525 | + forwarded_allow_ips=forwarded_allow_ips, |
| 526 | + root_path=root_path, |
| 527 | + limit_concurrency=limit_concurrency, |
| 528 | + backlog=backlog, |
| 529 | + limit_max_requests=limit_max_requests, |
| 530 | + timeout_keep_alive=timeout_keep_alive, |
| 531 | + ssl_keyfile=ssl_keyfile, |
| 532 | + ssl_certfile=ssl_certfile, |
| 533 | + ssl_keyfile_password=ssl_keyfile_password, |
| 534 | + ssl_version=ssl_version, |
| 535 | + ssl_cert_reqs=ssl_cert_reqs, |
| 536 | + ssl_ca_certs=ssl_ca_certs, |
| 537 | + ssl_ciphers=ssl_ciphers, |
| 538 | + headers=headers, |
| 539 | + use_colors=use_colors, |
| 540 | + factory=factory, |
| 541 | + ) |
446 | 542 | server = Server(config=config)
|
447 | 543 |
|
448 | 544 | if (config.reload or config.workers > 1) and not isinstance(app, str):
|
|
0 commit comments