diff --git a/scripts/server_with_rewrites.py b/scripts/server_with_rewrites.py index 5a2f507..5e24932 100644 --- a/scripts/server_with_rewrites.py +++ b/scripts/server_with_rewrites.py @@ -1,16 +1,22 @@ from http.server import HTTPServer, SimpleHTTPRequestHandler import argparse +BASE_PATH = '' 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): + 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' + )]) + 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,8 +26,15 @@ 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/hanzigraph; useful for reverse proxy', default='') args = parser.parse_args() port = int(args.port) + 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: