#!/usr/bin/env python3 """ Simple HTTP server to serve the Yoto Up Covers web app. Run this script and open http://localhost:8000 in your browser. """ import http.server import socketserver import os import sys from pathlib import Path # Change to the current directory (where the files are) web_app_dir = Path(__file__).parent os.chdir(web_app_dir) # Set up the server PORT = 8000 class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): def end_headers(self): # Add CORS headers to allow cross-origin requests self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS') self.send_header('Access-Control-Allow-Headers', 'Content-Type') super().end_headers() def log_message(self, format, *args): # Suppress server logs for cleaner output return def main(): print("Yoto Up Covers Web App Server") print("=" * 40) print(f"Serving from: {web_app_dir.absolute()}") print(f"Open your browser to: http://localhost:{PORT}") print("Press Ctrl+C to stop the server") print() try: with socketserver.TCPServer(("", PORT), CustomHTTPRequestHandler) as httpd: httpd.serve_forever() except KeyboardInterrupt: print("\nServer stopped.") except OSError as e: if e.errno == 48: # Address already in use print(f"Error: Port {PORT} is already in use. Try a different port or stop the other server.") else: print(f"Error starting server: {e}") if __name__ == "__main__": main()