From d08f59ae09b713ec85ff821f4110cbce2655ea10 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 6 Aug 2024 18:01:23 -0700 Subject: [PATCH 1/3] add redirect_ui_middleware --- litellm/proxy/proxy_server.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 59efaae10..e686bfa19 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -285,8 +285,6 @@ except Exception as e: server_root_path = os.getenv("SERVER_ROOT_PATH", "") print("server root path: ", server_root_path) # noqa -if server_root_path != "": - setup_admin_ui_on_server_root_path(server_root_path) _license_check = LicenseCheck() premium_user: bool = _license_check.is_premium() ui_link = f"{server_root_path}/ui/" @@ -388,6 +386,16 @@ try: src = os.path.join(ui_path, filename) dst = os.path.join(folder_path, "index.html") os.rename(src, dst) + + if server_root_path != "": + + @app.middleware("http") + async def redirect_ui_middleware(request: Request, call_next): + if request.url.path.startswith("/ui"): + new_path = request.url.path.replace("/ui", f"{server_root_path}/ui", 1) + return RedirectResponse(new_path) + return await call_next(request) + except: pass app.add_middleware( From a1c1ad60d2412b33a37671c1b97f16d20abed382 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 6 Aug 2024 18:10:34 -0700 Subject: [PATCH 2/3] fix forward ui requests when base url set --- litellm/proxy/proxy_server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index e686bfa19..11acc6ff4 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -388,6 +388,7 @@ try: os.rename(src, dst) if server_root_path != "": + os.environ["PROXY_BASE_URL"] = server_root_path @app.middleware("http") async def redirect_ui_middleware(request: Request, call_next): From 7173cef8220970c8bd01d1f7ca013976e80145b6 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 6 Aug 2024 20:58:51 -0700 Subject: [PATCH 3/3] clean up unused func --- litellm/proxy/common_utils/admin_ui_utils.py | 58 -------------------- litellm/proxy/proxy_server.py | 7 ++- 2 files changed, 5 insertions(+), 60 deletions(-) diff --git a/litellm/proxy/common_utils/admin_ui_utils.py b/litellm/proxy/common_utils/admin_ui_utils.py index 3044ba3af..3845c78ce 100644 --- a/litellm/proxy/common_utils/admin_ui_utils.py +++ b/litellm/proxy/common_utils/admin_ui_utils.py @@ -166,61 +166,3 @@ def missing_keys_form(missing_key_names: str): """ return missing_keys_html_form.format(missing_keys=missing_key_names) - - -def setup_admin_ui_on_server_root_path(server_root_path: str): - """ - Helper util to setup Admin UI on Server root path - """ - from litellm._logging import verbose_proxy_logger - - if server_root_path != "": - print("setting proxy base url to server root path") # noqa - if os.getenv("PROXY_BASE_URL") is None: - os.environ["PROXY_BASE_URL"] = server_root_path - - # re-build admin UI on server root path - # Save the original directory - original_dir = os.getcwd() - - current_dir = ( - os.path.dirname(os.path.abspath(__file__)) - + "/../../../ui/litellm-dashboard/" - ) - build_ui_path = os.path.join(current_dir, "build_ui_custom_path.sh") - package_path = os.path.join(current_dir, "package.json") - - print(f"Setting up Admin UI on {server_root_path}/ui .......") # noqa - - try: - # Change the current working directory - os.chdir(current_dir) - - # Make the script executable - subprocess.run(["chmod", "+x", "build_ui_custom_path.sh"], check=True) - - # Run npm install - subprocess.run(["npm", "install"], check=True) - - # Run npm run build - subprocess.run(["npm", "run", "build"], check=True) - - # Run the custom build script with the argument - subprocess.run( - ["./build_ui_custom_path.sh", f"{server_root_path}/ui"], check=True - ) - - print("Admin UI setup completed successfully.") # noqa - - except subprocess.CalledProcessError as e: - print(f"An error occurred during the Admin UI setup: {e}") # noqa - - except Exception as e: - print(f"An unexpected error occurred: {e}") # noqa - - finally: - # Always return to the original directory, even if an error occurred - os.chdir(original_dir) - print(f"Returned to original directory: {original_dir}") # noqa - - pass diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 11acc6ff4..29dc3813c 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -138,7 +138,6 @@ from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.caching_routes import router as caching_router from litellm.proxy.common_utils.admin_ui_utils import ( html_form, - setup_admin_ui_on_server_root_path, show_missing_vars_in_env, ) from litellm.proxy.common_utils.debug_utils import init_verbose_loggers @@ -388,7 +387,11 @@ try: os.rename(src, dst) if server_root_path != "": - os.environ["PROXY_BASE_URL"] = server_root_path + print( # noqa + f"server_root_path is set, forwarding any /ui requests to {server_root_path}/ui" + ) # noqa + if os.getenv("PROXY_BASE_URL") is None: + os.environ["PROXY_BASE_URL"] = server_root_path @app.middleware("http") async def redirect_ui_middleware(request: Request, call_next):