"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
"""CLI server startup kept separate from the API route assembler."""
from __future__ import annotations
from pathlib import Path
from fastapi import FastAPI
from src.api.security import (
_is_loopback_bind_host, _configured_api_key, install_access_log_redaction_filter,
)
def serve(app: FastAPI, argv: list[str] | None, *, repo_root: Path) -> int:
"""Start the API server from CLI-style arguments."""
import argparse
import subprocess
import uvicorn
from src.api.spa import SPAStaticFiles
parser = argparse.ArgumentParser(description="Vibe-Trading Server")
parser.add_argument("--port", type=int, default=8000, help="Listen port (default 8000)")
parser.add_argument("--host", default="127.0.0.1", help="Bind address")
parser.add_argument("--dev", action="store_true", help="Dev mode: spawn Vite on :5173")
try:
args = parser.parse_args(argv)
except SystemExit as exc:
return int(exc.code) if isinstance(exc.code, int) else 2
if not _is_loopback_bind_host(args.host) and not _configured_api_key():
print(
f"[warn] Binding to {args.host} without API_AUTH_KEY set. "
f"Remote requests are rejected by the loopback peer-IP check, "
f"but consider using --host 127.0.0.1 for local-only access."
)
frontend_dist = repo_root / "frontend" / "dist"
frontend_root = repo_root / "frontend"
vite_proc = None
if args.dev and frontend_root.exists():
print("[dev] Starting Vite dev server on :5173 ...")
vite_proc = subprocess.Popen(
["npx", "vite", "--host", "0.0.0.0"],
cwd=str(frontend_root),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
print(f"[dev] Vite PID={vite_proc.pid}")
print("[dev] Frontend: http://localhost:5173")
print(f"[dev] API: http://localhost:{args.port}")
elif frontend_dist.exists():
if not any(getattr(route, "path", None) == "/" for route in app.routes):
app.mount("/", SPAStaticFiles(directory=str(frontend_dist), html=True), name="frontend")
print(f"[prod] Frontend served from {frontend_dist}")
else:
print(f"[warn] No frontend build found at {frontend_dist}")
print("[warn] Run: cd frontend && npm run build")
print("=" * 50)
print(" Vibe-Trading Server")
print(f" http://127.0.0.1:{args.port}")
print("=" * 50)
# Redact api_key=/ticket= values from Uvicorn's access log (it logs the full
# request line including the query string). Installed before run() so the
# filter is attached when Uvicorn configures its loggers.
install_access_log_redaction_filter()
try:
uvicorn.run(app, host=args.host, port=args.port, log_level="info")
finally:
if vite_proc:
vite_proc.terminate()
print("[dev] Vite stopped")
return 0