#!/usr/bin/env python3
"""Minimal HTTP server for the WebGPU POC.

WebGPU requires a secure context. http://localhost is treated as a secure
context by Chromium, so plain HTTP on 127.0.0.1 is sufficient — no TLS
needed and no extra headers required for navigator.gpu.

Serves files from the directory it lives in. Binds to localhost only.
"""

import http.server
import os
import socketserver
import sys

PORT = 8080
HERE = os.path.dirname(os.path.abspath(__file__))


class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=HERE, **kwargs)

    def end_headers(self):
        # Be explicit about no caching while iterating on the POC.
        self.send_header("Cache-Control", "no-store")
        super().end_headers()

    def log_message(self, fmt, *args):
        sys.stderr.write("[server] %s\n" % (fmt % args))


def main():
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(("127.0.0.1", PORT), Handler) as httpd:
        sys.stderr.write(f"[server] serving {HERE} on http://127.0.0.1:{PORT}/poc.html\n")
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass


if __name__ == "__main__":
    main()
