@@ -20,6 +20,7 @@ class StaticWebFrontend(Frontend):
20
20
21
21
Arguments:
22
22
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>`
23
24
24
25
Example:
25
26
@@ -36,7 +37,7 @@ def __init__(self, serve_dir: str) -> None:
36
37
self .serve_dir = serve_dir
37
38
self ._process : Optional [mp .Process ] = None
38
39
39
- def start_server (self , host : str , port : int ) -> None :
40
+ def start_server (self , host : str , port : int , root_path : str = "" ) -> None :
40
41
log_file = str (get_frontend_logfile ())
41
42
self ._process = mp .Process (
42
43
target = start_server ,
@@ -46,6 +47,7 @@ def start_server(self, host: str, port: int) -> None:
46
47
serve_dir = self .serve_dir ,
47
48
path = f"/{ self .flow .name } " ,
48
49
log_file = log_file ,
50
+ root_path = root_path ,
49
51
),
50
52
)
51
53
self ._process .start ()
@@ -61,7 +63,9 @@ def healthz():
61
63
return {"status" : "ok" }
62
64
63
65
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 :
65
69
if port == - 1 :
66
70
port = find_free_network_port ()
67
71
fastapi_service = FastAPI ()
@@ -76,11 +80,11 @@ def start_server(serve_dir: str, host: str = "localhost", port: int = -1, path:
76
80
# trailing / is required for urljoin to properly join the path. In case of
77
81
# multiple trailing /, urljoin removes them
78
82
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" )
80
84
81
85
log_config = _get_log_config (log_file ) if log_file else uvicorn .config .LOGGING_CONFIG
82
86
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 )
84
88
85
89
86
90
def _get_log_config (log_file : str ) -> dict :
@@ -115,7 +119,8 @@ def _get_log_config(log_file: str) -> dict:
115
119
if __name__ == "__main__" : # pragma: no-cover
116
120
parser = ArgumentParser ()
117
121
parser .add_argument ("serve_dir" , type = str )
122
+ parser .add_argument ("root_path" , type = str , default = "" )
118
123
parser .add_argument ("--host" , type = str , default = "localhost" )
119
124
parser .add_argument ("--port" , type = int , default = - 1 )
120
125
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