MacBook pro commited on
Commit
1a5a976
·
1 Parent(s): c49ab72

Add compatibility /webrtc/* wrapper endpoints to resolve 404s during prefix transition

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -38,6 +38,70 @@ try:
38
  # endpoints resolve at /webrtc/ping, /webrtc/offer, etc.
39
  app.include_router(webrtc_router, prefix="/webrtc")
40
  WEBRTC_ROUTER_LOADED = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  except Exception as e: # pragma: no cover
42
  WEBRTC_IMPORT_ERROR = str(e)
43
  from fastapi import HTTPException as _HTTPException
 
38
  # endpoints resolve at /webrtc/ping, /webrtc/offer, etc.
39
  app.include_router(webrtc_router, prefix="/webrtc")
40
  WEBRTC_ROUTER_LOADED = True
41
+
42
+ # --- Compatibility layer ---
43
+ # In some deployed revisions the underlying router still carried an internal
44
+ # '/webrtc' prefix, yielding effective paths like /webrtc/webrtc/ping.
45
+ # The production JS client calls /webrtc/ping, /webrtc/token, /webrtc/ice_config, /webrtc/offer.
46
+ # To avoid breaking older images (or double-prefix drift during hot reload),
47
+ # expose lightweight pass-through wrappers at the expected single-prefix paths
48
+ # ONLY if those paths are currently missing (best-effort). Since FastAPI does
49
+ # not provide a simple public API to query registered routes before definition
50
+ # without introspection, we always register wrappers; duplicates are avoided
51
+ # because underlying double-prefixed versions have different paths.
52
+ from typing import Optional as _Opt
53
+ from fastapi import Body as _Body, Header as _Header
54
+ import inspect as _inspect
55
+
56
+ try: # pragma: no cover - defensive
57
+ # Import underlying handlers for direct reuse
58
+ from webrtc_server import webrtc_ping as _rt_webrtc_ping, _ice_configuration as _rt_ice_conf, webrtc_offer as _rt_webrtc_offer, _mint_token as _rt_mint_token # type: ignore
59
+ except Exception: # noqa: BLE001
60
+ _rt_webrtc_ping = None # type: ignore
61
+ _rt_ice_conf = None # type: ignore
62
+ _rt_webrtc_offer = None # type: ignore
63
+ _rt_mint_token = None # type: ignore
64
+
65
+ @app.get("/webrtc/ping")
66
+ async def _compat_webrtc_ping(): # type: ignore
67
+ if _rt_webrtc_ping:
68
+ return await _rt_webrtc_ping() # type: ignore
69
+ return {"router": False, "error": "webrtc_ping unavailable"}
70
+
71
+ @app.get("/webrtc/ice_config")
72
+ async def _compat_webrtc_ice_config(): # type: ignore
73
+ if _rt_ice_conf:
74
+ cfg = _rt_ice_conf() # returns RTCConfiguration
75
+ # Convert to serializable form similar to original endpoint
76
+ servers = []
77
+ for s in getattr(cfg, 'iceServers', []) or []:
78
+ entry = {"urls": s.urls}
79
+ if getattr(s, 'username', None):
80
+ entry["username"] = s.username
81
+ if getattr(s, 'credential', None):
82
+ entry["credential"] = s.credential
83
+ servers.append(entry)
84
+ return {"iceServers": servers}
85
+ return {"iceServers": []}
86
+
87
+ @app.get("/webrtc/token")
88
+ async def _compat_webrtc_token(): # type: ignore
89
+ if _rt_mint_token and WEBRTC_ROUTER_LOADED:
90
+ try:
91
+ return {"token": _rt_mint_token()}
92
+ except Exception as e: # noqa: BLE001
93
+ return {"error": str(e)}
94
+ return {"error": "token_unavailable"}
95
+
96
+ @app.post("/webrtc/offer")
97
+ async def _compat_webrtc_offer(
98
+ offer: dict = _Body(...),
99
+ x_api_key: _Opt[str] = _Header(default=None, alias="x-api-key"),
100
+ x_auth_token: _Opt[str] = _Header(default=None, alias="x-auth-token"),
101
+ ): # type: ignore
102
+ if _rt_webrtc_offer:
103
+ return await _rt_webrtc_offer(offer=offer, x_api_key=x_api_key, x_auth_token=x_auth_token) # type: ignore
104
+ raise HTTPException(status_code=503, detail="WebRTC offer handler unavailable")
105
  except Exception as e: # pragma: no cover
106
  WEBRTC_IMPORT_ERROR = str(e)
107
  from fastapi import HTTPException as _HTTPException