-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_webapp.py
More file actions
213 lines (180 loc) · 6.76 KB
/
Copy pathstart_webapp.py
File metadata and controls
213 lines (180 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
MetaBBX WebApp Starter Script
This script starts both the backend API server and the frontend React app.
"""
import subprocess
import sys
import os
import time
import signal
import threading
from pathlib import Path
def print_banner():
"""Print the MetaBBX banner"""
banner = """
╔══════════════════════════════════════════════════════════════╗
║ ║
║ 🚀 MetaBBX WebApp ║
║ ║
║ AI-Powered Code Generator & Editor ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
print(banner)
def check_dependencies():
"""Check if required dependencies are installed"""
print("🔍 Checking dependencies...")
# Check Python dependencies
try:
import flask
import flask_cors
print("✅ Python dependencies: OK")
except ImportError as e:
print(f"❌ Python dependencies missing: {e}")
print("Please run: pip install -r requirements.txt")
return False
# Check if Node.js is installed
try:
subprocess.run(['node', '--version'], check=True, capture_output=True)
print("✅ Node.js: OK")
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ Node.js not found. Please install Node.js first.")
return False
# Check if npm is installed
try:
subprocess.run(['npm', '--version'], check=True, capture_output=True)
print("✅ npm: OK")
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ npm not found. Please install npm first.")
return False
return True
def install_frontend_dependencies():
"""Install frontend dependencies if needed"""
frontend_dir = Path("frontend")
if not frontend_dir.exists():
print("❌ Frontend directory not found: frontend/")
return False
node_modules = frontend_dir / "node_modules"
if not node_modules.exists():
print("📦 Installing frontend dependencies...")
try:
subprocess.run(['npm', 'install'], cwd=frontend_dir, check=True)
print("✅ Frontend dependencies installed")
except subprocess.CalledProcessError as e:
print(f"❌ Failed to install frontend dependencies: {e}")
return False
return True
def start_backend():
"""Start the Flask backend server"""
print("🚀 Starting backend server...")
backend_dir = Path("backend")
if not backend_dir.exists():
print("❌ Backend directory not found: backend/")
return None
try:
# Start the Flask server
process = subprocess.Popen(
[sys.executable, "api_server.py"],
cwd=backend_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Wait a bit for the server to start
time.sleep(3)
if process.poll() is None:
print("✅ Backend server started on http://localhost:5000")
return process
else:
stdout, stderr = process.communicate()
print(f"❌ Backend server failed to start:")
print(f"STDOUT: {stdout}")
print(f"STDERR: {stderr}")
return None
except Exception as e:
print(f"❌ Error starting backend server: {e}")
return None
def start_frontend():
"""Start the React frontend"""
print("🚀 Starting frontend...")
frontend_dir = Path("frontend")
try:
# Start the React development server
process = subprocess.Popen(
['npm', 'start'],
cwd=frontend_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Wait a bit for the server to start
time.sleep(5)
if process.poll() is None:
print("✅ Frontend started on http://localhost:3000")
return process
else:
stdout, stderr = process.communicate()
print(f"❌ Frontend failed to start:")
print(f"STDOUT: {stdout}")
print(f"STDERR: {stderr}")
return None
except Exception as e:
print(f"❌ Error starting frontend: {e}")
return None
def main():
"""Main function to start the webapp"""
print_banner()
# Check dependencies
if not check_dependencies():
sys.exit(1)
# Install frontend dependencies if needed
if not install_frontend_dependencies():
sys.exit(1)
# Start backend
backend_process = start_backend()
if not backend_process:
print("❌ Failed to start backend. Exiting.")
sys.exit(1)
# Start frontend
frontend_process = start_frontend()
if not frontend_process:
print("❌ Failed to start frontend. Stopping backend...")
backend_process.terminate()
sys.exit(1)
print("\n" + "="*60)
print("🎉 MetaBBX WebApp is now running!")
print("="*60)
print("📱 Frontend: http://localhost:3000")
print("🔧 Backend API: http://localhost:5000")
print("📊 Health Check: http://localhost:5000/api/health")
print("="*60)
print("Press Ctrl+C to stop both servers")
print("="*60 + "\n")
# Function to handle cleanup on exit
def cleanup(signum, frame):
print("\n🛑 Shutting down servers...")
if backend_process:
backend_process.terminate()
if frontend_process:
frontend_process.terminate()
print("✅ Servers stopped. Goodbye!")
sys.exit(0)
# Register signal handlers
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
try:
# Keep the main thread alive
while True:
time.sleep(1)
# Check if processes are still running
if backend_process.poll() is not None:
print("❌ Backend server stopped unexpectedly")
break
if frontend_process.poll() is not None:
print("❌ Frontend server stopped unexpectedly")
break
except KeyboardInterrupt:
cleanup(signal.SIGINT, None)
if __name__ == "__main__":
main()