#!/usr/bin/env python3
"""Tiny static server for the try-on local test harness.

Forces charset=utf-8 on text responses so the harness page renders correctly
(python's default http.server omits the charset, which mojibakes non-ASCII text).
"""
import functools
import http.server
import socketserver

PORT = 8123


_CHARSET_TYPES = {
    ".html": "text/html; charset=utf-8",
    ".js": "text/javascript; charset=utf-8",
    ".css": "text/css; charset=utf-8",
    ".json": "application/json; charset=utf-8",
}


class Handler(http.server.SimpleHTTPRequestHandler):
    def guess_type(self, path):
        for ext, ctype in _CHARSET_TYPES.items():
            if path.endswith(ext):
                return ctype
        return super().guess_type(path)

    def end_headers(self):
        self.send_header("Cache-Control", "no-store")
        super().end_headers()


if __name__ == "__main__":
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(("127.0.0.1", PORT), Handler) as httpd:
        print(f"serving http://127.0.0.1:{PORT}/local_test.html")
        httpd.serve_forever()
