/
keorlov
/
collsim
Обзор
Документация
Войти
/
keorlov
/
collsim
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
run.py
131 строка
4 KB
Konstantin Orlov
wip
13 июл 2026, 09:01
13 июл 2026, 09:01
15b66bc
Код
Авторство
О чём код?
#!/usr/bin/env python3 """ Run script for CollSim - starts both backend (Flask+Waitress) and frontend (Vite). """ import argparse import os import signal import subprocess import sys import time from pathlib import Path def run_backend(port: int, host: str = "0.0.0.0") -> subprocess.Popen: """Start the Flask+Waitress backend.""" env = os.environ.copy() env["PYTHONPATH"] = str(Path(__file__).parent) return subprocess.Popen( [sys.executable, "-m", "app", "--host", host, "--port", str(port)], env=env, ) def run_frontend(port: int = 5173) -> subprocess.Popen | None: """Start the Vite frontend dev server.""" frontend_dir = Path(__file__).parent / "frontend" if not frontend_dir.exists(): print("Frontend directory not found, skipping frontend...") return None # Check if node_modules exists if not (frontend_dir / "node_modules").exists(): print("Installing frontend dependencies...") try: subprocess.run(["npm", "install"], cwd=frontend_dir, check=True) except (subprocess.CalledProcessError, FileNotFoundError) as e: print(f"Failed to install frontend deps: {e}") print("Please run 'npm install' in frontend/ manually") return None return subprocess.Popen( ["npm", "run", "dev", "--", "--port", str(port)], cwd=frontend_dir, ) def wait_for_health(url: str, timeout: int = 30) -> bool: """Wait for backend health endpoint.""" import urllib.request import urllib.error start = time.time() while time.time() - start < timeout: try: with urllib.request.urlopen(url, timeout=2) as resp: if resp.status == 200: return True except (urllib.error.URLError, ConnectionRefusedError, TimeoutError): time.sleep(0.5) return False def main(): parser = argparse.ArgumentParser(description="Run CollSim backend and frontend") parser.add_argument("--backend-port", type=int, default=5000, help="Backend port") parser.add_argument("--frontend-port", type=int, default=5173, help="Frontend port") parser.add_argument("--host", default="0.0.0.0", help="Backend host") parser.add_argument("--no-frontend", action="store_true", help="Run backend only") args = parser.parse_args() processes = [] def cleanup(signum=None, frame=None): print("\nShutting down...") for p in processes: if p: p.terminate() for p in processes: if p: try: p.wait(timeout=5) except subprocess.TimeoutExpired: p.kill() sys.exit(0) signal.signal(signal.SIGINT, cleanup) signal.signal(signal.SIGTERM, cleanup) print(f"Starting backend on {args.host}:{args.backend_port}...") backend = run_backend(args.backend_port, args.host) processes.append(backend) # Wait for backend to be ready health_url = f"http://localhost:{args.backend_port}/health" if wait_for_health(health_url): print("Backend ready!") else: print("Backend failed to start") cleanup() return 1 if not args.no_frontend: print(f"Starting frontend on port {args.frontend_port}...") frontend = run_frontend(args.frontend_port) if frontend: processes.append(frontend) print("Frontend started!") else: print("Frontend not available") print("\n=== CollSim Running ===") print(f"Backend API: http://localhost:{args.backend_port}") print(f" POST /simulate") print(f" GET /health") if not args.no_frontend: print(f"Frontend UI: http://localhost:{args.frontend_port}") print("Press Ctrl+C to stop\n") # Wait for any process to exit while True: for p in processes: if p and p.poll() is not None: print(f"Process exited with code {p.returncode}") cleanup() return p.returncode time.sleep(1) if __name__ == "__main__": sys.exit(main())