-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathfastapi.py
54 lines (43 loc) · 1.71 KB
/
fastapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import Optional
from fastapi import FastAPI
from idom.core.types import ComponentConstructor
from .starlette import (
Config,
StarletteServer,
_setup_common_routes,
_setup_config_and_app,
_setup_shared_view_dispatcher_route,
_setup_single_view_dispatcher_route,
)
def PerClientStateServer(
constructor: ComponentConstructor,
config: Optional[Config] = None,
app: Optional[FastAPI] = None,
) -> StarletteServer:
"""Return a :class:`StarletteServer` where each client has its own state.
Implements the :class:`~idom.server.proto.ServerFactory` protocol
Parameters:
constructor: A component constructor
config: Options for configuring server behavior
app: An application instance (otherwise a default instance is created)
"""
config, app = _setup_config_and_app(config, app, FastAPI)
_setup_common_routes(config, app)
_setup_single_view_dispatcher_route(config["url_prefix"], app, constructor)
return StarletteServer(app)
def SharedClientStateServer(
constructor: ComponentConstructor,
config: Optional[Config] = None,
app: Optional[FastAPI] = None,
) -> StarletteServer:
"""Return a :class:`StarletteServer` where each client shares state.
Implements the :class:`~idom.server.proto.ServerFactory` protocol
Parameters:
constructor: A component constructor
config: Options for configuring server behavior
app: An application instance (otherwise a default instance is created)
"""
config, app = _setup_config_and_app(config, app, FastAPI)
_setup_common_routes(config, app)
_setup_shared_view_dispatcher_route(config["url_prefix"], app, constructor)
return StarletteServer(app)