2025-08-18 20:00:58 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2025-08-18 20:03:51 +02:00
|
|
|
Simple HTTP server for testing the GlitchCraft PWA
|
2025-08-18 20:00:58 +02:00
|
|
|
Run with: python3 server.py
|
|
|
|
|
Then open http://localhost:8000/ in your browser
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import http.server
|
|
|
|
|
import socketserver
|
2025-08-18 20:03:51 +02:00
|
|
|
import socket
|
2025-08-18 20:00:58 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
PORT = 8000
|
|
|
|
|
# Serve from the app directory
|
|
|
|
|
DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app")
|
|
|
|
|
|
|
|
|
|
|
2025-08-18 20:03:51 +02:00
|
|
|
class DualStackServer(socketserver.TCPServer):
|
|
|
|
|
"""TCP server that supports both IPv4 and IPv6."""
|
|
|
|
|
|
|
|
|
|
address_family = socket.AF_INET6
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
# Disable dual stack to make it work on all systems
|
|
|
|
|
# Some systems have it enabled by default, some don't
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
# Enable dual stack - listen on both IPv4 and IPv6
|
|
|
|
|
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
|
|
|
|
|
|
|
|
|
|
|
2025-08-18 20:00:58 +02:00
|
|
|
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
|
2025-08-18 20:03:51 +02:00
|
|
|
"""Custom HTTP request handler for serving the GlitchCraft PWA."""
|
2025-08-18 20:00:58 +02:00
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, directory=DIRECTORY, **kwargs)
|
|
|
|
|
|
|
|
|
|
def end_headers(self):
|
|
|
|
|
# Add headers for PWA and CORS
|
|
|
|
|
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
|
self.send_header("Service-Worker-Allowed", "/")
|
|
|
|
|
self.send_header("X-Content-Type-Options", "nosniff")
|
|
|
|
|
super().end_headers()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-08-18 20:03:51 +02:00
|
|
|
# Listen on all interfaces (:: for IPv6, which also handles IPv4 with dual stack)
|
|
|
|
|
HOST = "::" # Listen on all interfaces (IPv6 and IPv4)
|
|
|
|
|
|
|
|
|
|
with DualStackServer((HOST, PORT), MyHTTPRequestHandler) as httpd:
|
|
|
|
|
print(f"Server running on all interfaces at port {PORT}")
|
|
|
|
|
print(f"Access at:")
|
|
|
|
|
print(f" http://localhost:{PORT}/")
|
|
|
|
|
print(f" http://127.0.0.1:{PORT}/ (IPv4)")
|
|
|
|
|
print(f" http://[::1]:{PORT}/ (IPv6)")
|
|
|
|
|
print(f" http://<your-ip>:{PORT}/")
|
2025-08-18 20:00:58 +02:00
|
|
|
print(f"Serving files from: {DIRECTORY}")
|
|
|
|
|
print("Press Ctrl+C to stop the server")
|
|
|
|
|
try:
|
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("\nServer stopped.")
|