#!/usr/bin/env python3
"""
Einfacher HTTP-Server für den Minecraft Chat Viewer
Startet einen lokalen Server auf Port 8000
"""

import http.server
import socketserver
import os
import sys

# Change to the directory where this script is located
os.chdir(os.path.dirname(os.path.abspath(__file__)))

PORT = 8000

class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        # Add CORS headers to allow local file access
        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()

    def log_message(self, format, *args):
        # Customize log format
        print(f"[{self.log_date_time_string()}] {format % args}")

def main():
    try:
        with socketserver.TCPServer(("", PORT), CustomHTTPRequestHandler) as httpd:
            print(f"🎮 Minecraft Chat Viewer Server")
            print(f"📡 Server läuft auf: http://localhost:{PORT}")
            print(f"📁 Serving directory: {os.getcwd()}")
            print(f"🚀 Öffne http://localhost:{PORT} in deinem Browser")
            print(f"⏹️  Drücke Ctrl+C zum Beenden")
            print("-" * 50)
            
            httpd.serve_forever()
            
    except KeyboardInterrupt:
        print(f"\n👋 Server beendet.")
        sys.exit(0)
    except OSError as e:
        if e.errno == 48:  # Address already in use
            print(f"❌ Port {PORT} ist bereits belegt!")
            print(f"💡 Versuche einen anderen Port oder beende andere Server.")
        else:
            print(f"❌ Fehler beim Starten des Servers: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()
