|
5 | 5 |
|
6 | 6 | from idom.types import RootComponentConstructor
|
7 | 7 |
|
8 |
| -from .utils import default_implementation |
| 8 | +from .types import ServerImplementation |
| 9 | +from .utils import all_implementations |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def configure(app: Any, component: RootComponentConstructor) -> None:
|
12 | 13 | """Configure the given app instance to display the given component"""
|
13 |
| - return default_implementation().configure(app, component) |
| 14 | + return _default_implementation().configure(app, component) |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def create_development_app() -> Any:
|
17 | 18 | """Create an application instance for development purposes"""
|
18 |
| - return default_implementation().create_development_app() |
| 19 | + return _default_implementation().create_development_app() |
19 | 20 |
|
20 | 21 |
|
21 | 22 | async def serve_development_app(
|
22 | 23 | app: Any, host: str, port: int, started: asyncio.Event
|
23 | 24 | ) -> None:
|
24 | 25 | """Run an application using a development server"""
|
25 |
| - return await default_implementation().serve_development_app( |
| 26 | + return await _default_implementation().serve_development_app( |
26 | 27 | app, host, port, started
|
27 | 28 | )
|
| 29 | + |
| 30 | + |
| 31 | +def _default_implementation() -> ServerImplementation[Any]: |
| 32 | + """Get the first available server implementation""" |
| 33 | + global _DEFAULT_IMPLEMENTATION |
| 34 | + |
| 35 | + if _DEFAULT_IMPLEMENTATION is not None: |
| 36 | + return _DEFAULT_IMPLEMENTATION |
| 37 | + |
| 38 | + try: |
| 39 | + implementation = next(all_implementations()) |
| 40 | + except StopIteration: |
| 41 | + raise RuntimeError("No built-in server implementation installed.") |
| 42 | + else: |
| 43 | + _DEFAULT_IMPLEMENTATION = implementation |
| 44 | + return implementation |
| 45 | + |
| 46 | + |
| 47 | +_DEFAULT_IMPLEMENTATION: ServerImplementation[Any] | None = None |
0 commit comments