Export FastAPI app from runner so custom routes can be added

Move the FastAPI instance to module level so other packages can import
it and register routes before main() is called. main() now configures
the existing app with transport-specific routes instead of creating a
new one.
This commit is contained in:
Aleix Conchillo Flaqué
2026-04-02 21:16:17 -07:00
parent 57068f1b38
commit 1ab07d312f

View File

@@ -110,6 +110,22 @@ RUNNER_DOWNLOADS_FOLDER: Optional[str] = None
RUNNER_HOST: str = "localhost"
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():
"""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)
def _create_server_app(args: argparse.Namespace):
"""Create FastAPI app with transport-specific routes."""
app = FastAPI()
def _configure_server_app(args: argparse.Namespace):
"""Configure the module-level FastAPI app with transport-specific routes."""
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
@@ -188,8 +202,6 @@ def _create_server_app(args: argparse.Namespace):
else:
logger.warning(f"Unknown transport type: {args.transport}")
return app
def _setup_webrtc_routes(app: FastAPI, args: argparse.Namespace):
"""Set up WebRTC-specific routes."""
@@ -997,8 +1009,8 @@ def main(parser: Optional[argparse.ArgumentParser] = None):
RUNNER_HOST = args.host
RUNNER_PORT = args.port
# Create the app with transport-specific setup
app = _create_server_app(args)
# Configure the app with transport-specific routes
_configure_server_app(args)
# Run the server
uvicorn.run(app, host=args.host, port=args.port)