| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import warnings |
| 4 |
from typing import Awaitable, Callable, Literal, Optional |
| 5 |
|
| 6 |
import trio |
| 7 |
|
| 8 |
from .run import worker_serve |
| 9 |
from ..config import Config |
| 10 |
from ..typing import Framework |
| 11 |
from ..utils import wrap_app |
| 12 |
|
| 13 |
|
| 14 |
async def serve( |
| 15 |
app: Framework, |
| 16 |
config: Config, |
| 17 |
*, |
| 18 |
shutdown_trigger: Optional[Callable[..., Awaitable[None]]] = None, |
| 19 |
task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED, |
| 20 |
mode: Optional[Literal["asgi", "wsgi"]] = None, |
| 21 |
) -> None: |
| 22 |
"""Serve an ASGI framework app given the config. |
| 23 |
|
| 24 |
This allows for a programmatic way to serve an ASGI framework, it |
| 25 |
can be used via, |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
trio.run(serve, app, config) |
| 30 |
|
| 31 |
It is assumed that the event-loop is configured before calling |
| 32 |
this function, therefore configuration values that relate to loop |
| 33 |
setup or process setup are ignored. |
| 34 |
|
| 35 |
Arguments: |
| 36 |
app: The ASGI application to serve. |
| 37 |
config: A Hypercorn configuration object. |
| 38 |
shutdown_trigger: This should return to trigger a graceful |
| 39 |
shutdown. |
| 40 |
mode: Specify if the app is WSGI or ASGI. |
| 41 |
""" |
| 42 |
if config.debug: |
| 43 |
warnings.warn("The config `debug` has no affect when using serve", Warning) |
| 44 |
if config.workers != 1: |
| 45 |
warnings.warn("The config `workers` has no affect when using serve", Warning) |
| 46 |
|
| 47 |
await worker_serve( |
| 48 |
wrap_app(app, config.wsgi_max_body_size, mode), |
| 49 |
config, |
| 50 |
shutdown_trigger=shutdown_trigger, |
| 51 |
task_status=task_status, |
| 52 |
) |