@@ -20,6 +20,7 @@ class StaticWebFrontend(Frontend):
2020
2121 Arguments:
2222 serve_dir: A local directory to serve files from. This directory should at least contain a file `index.html`.
23+ root_path: A path prefix when routing traffic from behind a proxy at `/<root_path>`
2324
2425 Example:
2526
@@ -36,7 +37,7 @@ def __init__(self, serve_dir: str) -> None:
3637 self .serve_dir = serve_dir
3738 self ._process : Optional [mp .Process ] = None
3839
39- def start_server (self , host : str , port : int ) -> None :
40+ def start_server (self , host : str , port : int , root_path : str = "" ) -> None :
4041 log_file = str (get_frontend_logfile ())
4142 self ._process = mp .Process (
4243 target = start_server ,
@@ -46,6 +47,7 @@ def start_server(self, host: str, port: int) -> None:
4647 serve_dir = self .serve_dir ,
4748 path = f"/{ self .flow .name } " ,
4849 log_file = log_file ,
50+ root_path = root_path ,
4951 ),
5052 )
5153 self ._process .start ()
@@ -61,7 +63,9 @@ def healthz():
6163 return {"status" : "ok" }
6264
6365
64- def start_server (serve_dir : str , host : str = "localhost" , port : int = - 1 , path : str = "/" , log_file : str = "" ) -> None :
66+ def start_server (
67+ serve_dir : str , host : str = "localhost" , port : int = - 1 , path : str = "/" , log_file : str = "" , root_path : str = ""
68+ ) -> None :
6569 if port == - 1 :
6670 port = find_free_network_port ()
6771 fastapi_service = FastAPI ()
@@ -76,11 +80,11 @@ def start_server(serve_dir: str, host: str = "localhost", port: int = -1, path:
7680 # trailing / is required for urljoin to properly join the path. In case of
7781 # multiple trailing /, urljoin removes them
7882 fastapi_service .get (urljoin (f"{ path } /" , "healthz" ), status_code = 200 )(healthz )
79- fastapi_service .mount (path , StaticFiles (directory = serve_dir , html = True ), name = "static" )
83+ fastapi_service .mount (urljoin ( path , root_path ) , StaticFiles (directory = serve_dir , html = True ), name = "static" )
8084
8185 log_config = _get_log_config (log_file ) if log_file else uvicorn .config .LOGGING_CONFIG
8286
83- uvicorn .run (app = fastapi_service , host = host , port = port , log_config = log_config )
87+ uvicorn .run (app = fastapi_service , host = host , port = port , log_config = log_config , root_path = root_path )
8488
8589
8690def _get_log_config (log_file : str ) -> dict :
@@ -115,7 +119,8 @@ def _get_log_config(log_file: str) -> dict:
115119if __name__ == "__main__" : # pragma: no-cover
116120 parser = ArgumentParser ()
117121 parser .add_argument ("serve_dir" , type = str )
122+ parser .add_argument ("root_path" , type = str , default = "" )
118123 parser .add_argument ("--host" , type = str , default = "localhost" )
119124 parser .add_argument ("--port" , type = int , default = - 1 )
120125 args = parser .parse_args ()
121- start_server (serve_dir = args .serve_dir , host = args .host , port = args .port )
126+ start_server (serve_dir = args .serve_dir , host = args .host , port = args .port , root_path = args . root_path )
0 commit comments