-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
64 lines (53 loc) · 1.94 KB
/
server.py
File metadata and controls
64 lines (53 loc) · 1.94 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
#!/usr/bin/env python3
"""
Simple HTTP server to serve the HTML frontend
"""
import http.server
import socketserver
import webbrowser
import os
import sys
from pathlib import Path
def main():
"""Main function to start the HTTP server"""
# Change to the directory containing this script
script_dir = Path(__file__).parent
os.chdir(script_dir)
PORT = 8080
# Check if the HTML file exists
html_file = "frontend.html"
if not os.path.exists(html_file):
print(f"❌ Error: {html_file} not found!")
return 1
# Create a custom handler that serves the HTML file
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/' or self.path == '/index.html':
self.path = '/frontend.html'
return super().do_GET()
try:
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"🚀 Simple frontend server starting...")
print(f"📊 Frontend available at: http://localhost:{PORT}")
print(f"⚠️ Make sure the API server is running on port 8000")
print(f" Start API with: python main.py api")
print(f" Or: uv run -- python main.py api")
print()
print("Press Ctrl+C to stop the server")
# Try to open the browser
try:
webbrowser.open(f'http://localhost:{PORT}')
except:
pass
httpd.serve_forever()
except KeyboardInterrupt:
print("\n👋 Server stopped")
return 0
except OSError as e:
if e.errno == 10048: # Port already in use on Windows
print(f"❌ Port {PORT} is already in use. Try a different port or stop the other server.")
else:
print(f"❌ Error starting server: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())