From de3bf25519e74a312c83cd74f43c1adc7fd45b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Sz=C3=A1sz?= Date: Wed, 15 Oct 2025 21:00:41 +0100 Subject: [PATCH 1/2] Base path support for server --- scripts/server_with_rewrites.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/scripts/server_with_rewrites.py b/scripts/server_with_rewrites.py index 5a2f5070..8915b452 100644 --- a/scripts/server_with_rewrites.py +++ b/scripts/server_with_rewrites.py @@ -1,16 +1,28 @@ from http.server import HTTPServer, SimpleHTTPRequestHandler import argparse +class MyHTTPServer(HTTPServer): + def __init__(self, server_address, RequestHandlerClass, + base_path='', + bind_and_activate=True): + HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=bind_and_activate) + self.base_path = base_path -class RequestHandler(SimpleHTTPRequestHandler): +class RequestHandler(SimpleHTTPRequestHandler): # mimic firebase hosting's rewrite rules - def translate_path(self, path): - if path.startswith('/simplified') or path.startswith('/traditional') or path.startswith('/cantonese') or path.startswith('/hsk'): + def translate_path(self, request_path): + base_path = self.server.base_path if isinstance(self.server, MyHTTPServer) else '' + slashed_path = request_path.removeprefix(base_path) + crit_blank = slashed_path in ('/', '') + crit_specials = any([slashed_path.startswith(f"{candidate}") for candidate in ( + '/simplified', '/traditional', '/cantonese', '/hsk' + )]) + if crit_blank or crit_specials: return 'index.html' - # prepend . such that the frontend's use of /whatever results in ./whatever - # should maybe just strip the first character, tbh - return f".{path}" + # now turn the slashed path /foo/bar into ./foo/bar on disk + path = f".{slashed_path}" + return path if __name__ == '__main__': @@ -20,9 +32,16 @@ def translate_path(self, path): description='Enable URL rewrites without relying on firebase hosting. Intended to be run from the public/ directory.') parser.add_argument( '--port', help='your choice of port; default 8000', nargs='?', default=8000, type=int) + parser.add_argument( + '--base-path', help='custom base path, e.g. /app/hanzi; useful for reverse proxy', default='') args = parser.parse_args() port = int(args.port) - myServer = HTTPServer(('0.0.0.0', port), RequestHandler) + base_path = args.base_path + if base_path: + assert base_path[0] == '/', '--base-path must start with /' + if base_path[-1] == '/': + base_path = base_path[:-1] + myServer = MyHTTPServer(('0.0.0.0', port), RequestHandler, base_path=base_path) print("HanziGraph started") try: myServer.serve_forever() From 1c675f5ab5d170b76c788cc4beab836cf3f7b4b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Sz=C3=A1sz?= Date: Sat, 25 Oct 2025 17:58:49 +0100 Subject: [PATCH 2/2] Global BASE_PATH for Python server --- scripts/server_with_rewrites.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/scripts/server_with_rewrites.py b/scripts/server_with_rewrites.py index 8915b452..5e249328 100644 --- a/scripts/server_with_rewrites.py +++ b/scripts/server_with_rewrites.py @@ -1,19 +1,13 @@ from http.server import HTTPServer, SimpleHTTPRequestHandler import argparse -class MyHTTPServer(HTTPServer): - def __init__(self, server_address, RequestHandlerClass, - base_path='', - bind_and_activate=True): - HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=bind_and_activate) - self.base_path = base_path - +BASE_PATH = '' class RequestHandler(SimpleHTTPRequestHandler): # mimic firebase hosting's rewrite rules def translate_path(self, request_path): - base_path = self.server.base_path if isinstance(self.server, MyHTTPServer) else '' - slashed_path = request_path.removeprefix(base_path) + global BASE_PATH + slashed_path = request_path.removeprefix(BASE_PATH) crit_blank = slashed_path in ('/', '') crit_specials = any([slashed_path.startswith(f"{candidate}") for candidate in ( '/simplified', '/traditional', '/cantonese', '/hsk' @@ -33,15 +27,15 @@ def translate_path(self, request_path): parser.add_argument( '--port', help='your choice of port; default 8000', nargs='?', default=8000, type=int) parser.add_argument( - '--base-path', help='custom base path, e.g. /app/hanzi; useful for reverse proxy', default='') + '--base-path', help='custom base path, e.g. /app/hanzigraph; useful for reverse proxy', default='') args = parser.parse_args() port = int(args.port) - base_path = args.base_path - if base_path: - assert base_path[0] == '/', '--base-path must start with /' - if base_path[-1] == '/': - base_path = base_path[:-1] - myServer = MyHTTPServer(('0.0.0.0', port), RequestHandler, base_path=base_path) + if args.base_path: + BASE_PATH = args.base_path + assert BASE_PATH[0] == '/', '--base-path must start with /' + if BASE_PATH[-1] == '/': + BASE_PATH = BASE_PATH[:-1] + myServer = HTTPServer(('0.0.0.0', port), RequestHandler) print("HanziGraph started") try: myServer.serve_forever()