#!/usr/bin/env python3 """ Start the Discord Fishbowl Admin Interface Launches both the FastAPI backend and React frontend """ import asyncio import subprocess import sys import os import signal import time from pathlib import Path def run_command(cmd, cwd=None, env=None): """Run a command and return the process""" print(f"Starting: {' '.join(cmd)}") return subprocess.Popen( cmd, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True ) def main(): """Main function to start the admin interface""" project_root = Path(__file__).parent.parent admin_frontend_path = project_root / "admin-frontend" processes = [] try: print("šŸš€ Starting Discord Fishbowl Admin Interface...") print("=" * 50) # Start FastAPI backend print("\nšŸ“” Starting FastAPI backend server...") backend_env = os.environ.copy() backend_env["PYTHONPATH"] = str(project_root) backend_process = run_command( [sys.executable, "-m", "uvicorn", "src.admin.app:app", "--host", "0.0.0.0", "--port", "8000", "--reload"], cwd=project_root, env=backend_env ) processes.append(("Backend", backend_process)) # Wait a bit for backend to start time.sleep(3) # Check if npm is available and admin-frontend exists if not admin_frontend_path.exists(): print("āŒ Admin frontend directory not found!") print("Please ensure admin-frontend directory exists") return 1 # Check if node_modules exists, if not run npm install node_modules_path = admin_frontend_path / "node_modules" if not node_modules_path.exists(): print("\nšŸ“¦ Installing frontend dependencies...") npm_install = run_command( ["npm", "install"], cwd=admin_frontend_path ) npm_install.wait() if npm_install.returncode != 0: print("āŒ Failed to install npm dependencies") return 1 # Start React frontend print("\n🌐 Starting React frontend server...") frontend_process = run_command( ["npm", "start"], cwd=admin_frontend_path ) processes.append(("Frontend", frontend_process)) print("\nāœ… Admin interface is starting up!") print("šŸ“” Backend API: http://localhost:8000") print("🌐 Frontend UI: http://localhost:3000/admin") print("\nšŸ”‘ Default login credentials:") print(" Username: admin") print(" Password: admin123") print("\nšŸ›‘ Press Ctrl+C to stop all services") print("=" * 50) # Monitor processes while True: time.sleep(1) # Check if any process has died for name, process in processes: if process.poll() is not None: print(f"\nāŒ {name} process has stopped unexpectedly!") return 1 except KeyboardInterrupt: print("\nšŸ›‘ Shutting down admin interface...") # Terminate all processes for name, process in processes: print(f" Stopping {name}...") try: process.terminate() process.wait(timeout=5) except subprocess.TimeoutExpired: print(f" Force killing {name}...") process.kill() process.wait() print("āœ… Admin interface stopped successfully") return 0 except Exception as e: print(f"\nāŒ Error starting admin interface: {e}") # Clean up processes for name, process in processes: try: process.terminate() process.wait(timeout=2) except: process.kill() return 1 if __name__ == "__main__": sys.exit(main())