2025-05-24 15:36:14 +02:00

75 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Simple HTTP server for serving the SeReact frontend during development.
"""
import http.server
import socketserver
import webbrowser
import os
import sys
from pathlib import Path
# Configuration
PORT = 8081
HOST = 'localhost'
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
"""Custom handler to serve index.html for SPA routing"""
def end_headers(self):
# Add CORS headers for development
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-API-Key')
super().end_headers()
def do_OPTIONS(self):
# Handle preflight requests
self.send_response(200)
self.end_headers()
def main():
# Change to the directory containing this script
script_dir = Path(__file__).parent
os.chdir(script_dir)
# Check if index.html exists
if not Path('index.html').exists():
print("Error: index.html not found in current directory")
print(f"Current directory: {os.getcwd()}")
sys.exit(1)
# Create server
with socketserver.TCPServer((HOST, PORT), CustomHTTPRequestHandler) as httpd:
server_url = f"http://{HOST}:{PORT}"
print(f"🚀 SeReact Frontend Development Server")
print(f"📁 Serving files from: {os.getcwd()}")
print(f"🌐 Server running at: {server_url}")
print(f"📱 Open in browser: {server_url}")
print(f"⏹️ Press Ctrl+C to stop the server")
print()
# Try to open browser automatically
try:
webbrowser.open(server_url)
print("✅ Browser opened automatically")
except Exception as e:
print(f"⚠️ Could not open browser automatically: {e}")
print()
print("🔧 Development Tips:")
print(" - Edit files and refresh browser to see changes")
print(" - Check browser console (F12) for errors")
print(" - Configure API settings in the app")
print()
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n🛑 Server stopped by user")
print("👋 Thanks for using SeReact!")
if __name__ == "__main__":
main()