Merge pull request #4234 from pipecat-ai/aleix/export-runner-app
Export FastAPI app from runner for custom routes
This commit is contained in:
1
changelog/4234.added.md
Normal file
1
changelog/4234.added.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
- The development runner now exports a module-level `app` FastAPI instance (`from pipecat.runner.run import app`) so you can register custom routes before calling `main()`.
|
||||||
@@ -110,6 +110,22 @@ RUNNER_DOWNLOADS_FOLDER: Optional[str] = None
|
|||||||
RUNNER_HOST: str = "localhost"
|
RUNNER_HOST: str = "localhost"
|
||||||
RUNNER_PORT: int = 7860
|
RUNNER_PORT: int = 7860
|
||||||
|
|
||||||
|
app: FastAPI = FastAPI()
|
||||||
|
"""The FastAPI application instance.
|
||||||
|
|
||||||
|
Import this to add custom routes from other packages before calling
|
||||||
|
:func:`main`::
|
||||||
|
|
||||||
|
from pipecat.runner.run import app, main
|
||||||
|
|
||||||
|
@app.get("/my-route")
|
||||||
|
async def my_route():
|
||||||
|
return {"hello": "world"}
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def _get_bot_module():
|
def _get_bot_module():
|
||||||
"""Get the bot module from the calling script."""
|
"""Get the bot module from the calling script."""
|
||||||
@@ -164,10 +180,8 @@ async def _run_telephony_bot(websocket: WebSocket, args: argparse.Namespace):
|
|||||||
await bot_module.bot(runner_args)
|
await bot_module.bot(runner_args)
|
||||||
|
|
||||||
|
|
||||||
def _create_server_app(args: argparse.Namespace):
|
def _configure_server_app(args: argparse.Namespace):
|
||||||
"""Create FastAPI app with transport-specific routes."""
|
"""Configure the module-level FastAPI app with transport-specific routes."""
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=["*"],
|
||||||
@@ -188,8 +202,6 @@ def _create_server_app(args: argparse.Namespace):
|
|||||||
else:
|
else:
|
||||||
logger.warning(f"Unknown transport type: {args.transport}")
|
logger.warning(f"Unknown transport type: {args.transport}")
|
||||||
|
|
||||||
return app
|
|
||||||
|
|
||||||
|
|
||||||
def _setup_webrtc_routes(app: FastAPI, args: argparse.Namespace):
|
def _setup_webrtc_routes(app: FastAPI, args: argparse.Namespace):
|
||||||
"""Set up WebRTC-specific routes."""
|
"""Set up WebRTC-specific routes."""
|
||||||
@@ -997,8 +1009,8 @@ def main(parser: Optional[argparse.ArgumentParser] = None):
|
|||||||
RUNNER_HOST = args.host
|
RUNNER_HOST = args.host
|
||||||
RUNNER_PORT = args.port
|
RUNNER_PORT = args.port
|
||||||
|
|
||||||
# Create the app with transport-specific setup
|
# Configure the app with transport-specific routes
|
||||||
app = _create_server_app(args)
|
_configure_server_app(args)
|
||||||
|
|
||||||
# Run the server
|
# Run the server
|
||||||
uvicorn.run(app, host=args.host, port=args.port)
|
uvicorn.run(app, host=args.host, port=args.port)
|
||||||
|
|||||||
Reference in New Issue
Block a user