#!/usr/bin/env python3 import http.server, socketserver, os, pathlib ROOT = pathlib.Path(__file__).parent / 'public' PORT = int(os.environ.get('PORT', '8008')) class SPAHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): path_only = self.path.split('?',1)[0].split('#',1)[0] fs_path = self.translate_path(path_only) if os.path.exists(fs_path) and os.path.isfile(fs_path): return super().do_GET() self.path = '/index.html' return super().do_GET() if __name__ == '__main__': os.chdir(ROOT) # serve from /public with socketserver.TCPServer(('127.0.0.1', PORT), SPAHandler) as httpd: print(f"Serving {ROOT} at http://127.0.0.1:{PORT}") try: httpd.serve_forever() except KeyboardInterrupt: pass