-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_server.py
More file actions
40 lines (34 loc) · 1.07 KB
/
api_server.py
File metadata and controls
40 lines (34 loc) · 1.07 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
import os
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
from livekit import api
from datetime import timedelta
import uuid
load_dotenv()
LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")
LIVEKIT_URL = os.getenv("LIVEKIT_URL")
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/connection-details")
async def get_token(identity: str, room: str = "voice-room"):
at = api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)
at.with_grants(api.VideoGrants(room_join=True, room=f"room-{uuid.uuid4()}"))
at.with_identity(identity)
at.with_ttl(timedelta(4))
token = at.to_jwt()
return JSONResponse({
"participantToken": token,
"serverUrl": LIVEKIT_URL
})
if __name__ == "__main__":
import uvicorn
uvicorn.run("api_server:app", host="localhost", port=8003, reload=False)