-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__init__.py
More file actions
41 lines (32 loc) · 1.36 KB
/
Copy path__init__.py
File metadata and controls
41 lines (32 loc) · 1.36 KB
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
from typing import Union
import socketio
from starlette.routing import Mount, Route, Router
from common import AppConfig, application_init
from common.di_container import Container
from common.telemetry import instrument_third_party
from socketio_app.namespaces.chat import ChatNamespace
from socketio_app.web_routes import docs
# These instrumentors patch and wrap libraries, we want
# to execute them ASAP
instrument_third_party()
def create_app(
test_config: Union[AppConfig, None] = None,
test_di_container: Union[Container, None] = None,
) -> Router:
_config = test_config or AppConfig()
ref = application_init(_config, test_di_container)
ref.di_container.wire(packages=["socketio_app"])
# SocketIO App
sio = socketio.AsyncServer(async_mode="asgi")
# Namespaces are the equivalent of Routes.
sio.register_namespace(ChatNamespace("/chat"))
# Render /docs endpoint using starlette, and all the rest handled with Socket.io
routes = [
Route("/docs/asyncapi.json", docs.asyncapi_json, methods=["GET"]),
Route("/docs", docs.get_asyncapi_html, methods=["GET"]),
Mount("", app=socketio.ASGIApp(sio), name="socketio"),
]
# No need for whole starlette, we're rendering a simple couple of endpoints
# https://www.starlette.io/routing/#working-with-router-instances
app = Router(routes=routes)
return app