Spaces:
Sleeping
Sleeping
File size: 3,911 Bytes
1e8ff3b ad5d266 a3e20c4 bd1c43c ad5d266 bd1c43c a3e20c4 4d3af55 bd1c43c ad5d266 1e8ff3b a3e20c4 1e8ff3b a3e20c4 1e8ff3b a3e20c4 1e8ff3b a3e20c4 1e8ff3b b80985f 4d3af55 a3e20c4 1e8ff3b fc610e2 a3e20c4 1e8ff3b a3e20c4 4d3af55 a3e20c4 4d3af55 1e8ff3b a3e20c4 1e8ff3b 71f583a 1e8ff3b a3e20c4 82d5975 ad5d266 82d5975 ad5d266 82d5975 a3e20c4 ad5d266 bd1c43c ad5d266 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
from dotenv import load_dotenv
from pathlib import Path
import os
import requests
import subprocess
import time
from app.auth import get_room_name
load_dotenv()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount the static directory
app.mount("/static", StaticFiles(directory="frontend/out", html=True), name="static")
def _create_room():
api_path = os.getenv("DAILY_API_PATH") or "https://api.daily.co/v1"
expiry_time = os.getenv("BOT_MAX_DURATION", 300)
daily_api_key = os.getenv("DAILY_API_KEY", None)
try:
res = requests.post(
f"{api_path}/rooms",
headers={"Authorization": f"Bearer {daily_api_key}"},
json={
"properties": {
"exp": time.time() + int(expiry_time),
"eject_at_room_exp": True,
"enable_prejoin_ui": False,
"start_audio_off": True,
"permissions": {
"canSend": ["video"]
}
}
}
)
if res.status_code != 200:
return JSONResponse({
"error": "Unable to create room",
"status_code": res.status_code,
"text": res.text
}, 500)
data = res.json()
print(data)
return JSONResponse({"room_url": data["url"], "privacy": data["privacy"]})
except Exception:
return JSONResponse({"error": Exception})
def _start_bot(bot_path, room_url, args=None):
daily_room_name = get_room_name(room_url)
daily_api_path = os.getenv("DAILY_API_PATH")
daily_api_key = os.getenv("DAILY_API_KEY")
#@TODO error handling here
if args:
extra_args = " ".join([f'-{x[0]} "{x[1]}"' for x in args])
else:
extra_args = ""
proc = subprocess.Popen(
[
f"python3 {bot_path} -u {room_url} -k {daily_api_key} {extra_args}"
],
shell=True,
bufsize=1,
)
# Don't return until the bot has joined the room, but wait for at most 2 seconds.
attempts = 0
while attempts < 20:
time.sleep(0.1)
attempts += 1
res = requests.get(
f"{daily_api_path}/rooms/{daily_room_name}/get-session-data",
headers={"Authorization": f"Bearer {daily_api_key}"},
)
if res.status_code == 200:
break
print(f"Took {attempts} attempts to join room {daily_room_name}")
return JSONResponse({"started": True})
@app.post("/start")
async def start(request: Request):
data = await request.json()
return _start_bot("./app/bot.py", data['room_url'])
@app.post("/create")
async def create():
return _create_room()
@app.get("/{path_name:path}", response_class=FileResponse)
async def catch_all(path_name: str):
if path_name == "":
return FileResponse("frontend/out/index.html")
file_path = Path("frontend/out") / path_name
if file_path.is_file():
return file_path
html_file_path = file_path.with_suffix(".html")
if html_file_path.is_file():
return FileResponse(html_file_path)
raise HTTPException(status_code=450, detail="Incorrect API call")
# Run the application using Uvicorn
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"server:app",
host="0.0.0.0",
port=int(os.getenv('FAST_API_PORT', 8000)),
reload=os.getenv('FAST_API_RELOAD', 'false').lower() == 'true',
#ssl_certfile=args.ssl_certfile,
#ssl_keyfile=args.ssl_keyfile,
)
|