-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_app.py
More file actions
43 lines (28 loc) · 1.04 KB
/
Copy pathfastapi_app.py
File metadata and controls
43 lines (28 loc) · 1.04 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
42
43
"""Run the PeerJS broker alongside a FastAPI app, sharing its event loop.
Requires fastapi and uvicorn (not project dependencies):
pip install fastapi uvicorn
uvicorn examples.fastapi_app:app --port 8888
"""
import asyncio
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from python_peerjs_server.config import Config
from python_peerjs_server.main import peerjs_serve
logging.basicConfig(level=logging.INFO)
peerjs_config = Config(host="127.0.0.1", port=9000, key="peerjs")
@asynccontextmanager
async def lifespan(app: FastAPI):
task = asyncio.create_task(peerjs_serve(peerjs_config, handle_signals=False))
yield
task.cancel()
await asyncio.gather(task, return_exceptions=True)
app = FastAPI(lifespan=lifespan)
@app.get("/")
def index() -> str:
return "hello from fastapi"
if __name__ == "__main__":
raise SystemExit(
"This example defines an ASGI app and must be run with uvicorn, not plain python:\n\n"
" uvicorn examples.fastapi_app:app --port 8888\n"
)