#!/usr/bin/env python3
"""
Simple HTTP server for serving web_client.html on remote server
"""

import http.server
import socketserver
import os

# Configuration
PORT = 8080
HOST = "0.0.0.0"  # Listen on all interfaces
DIRECTORY = os.path.dirname(os.path.abspath(__file__))

class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)
    
    def end_headers(self):
        # Add CORS headers for WebSocket
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', '*')
        super().end_headers()

# Allow socket reuse
socketserver.TCPServer.allow_reuse_address = True

with socketserver.TCPServer((HOST, PORT), MyHTTPRequestHandler) as httpd:
    print(f"✅ HTTP Server running on http://{HOST}:{PORT}")
    print(f"   Access from remote: http://10.2.56.217:{PORT}/web_client.html")
    print(f"   Press Ctrl+C to stop")
    
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n🛑 Server stopped")
