From 7c5fec3a2606b7a40f4cdc60f2553ba55fc8df99 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Tue, 11 Jun 2024 02:25:03 -0700 Subject: [PATCH] Use root url for monitoring url (#8506) * use root url for monitoring url * add changeset * format * add unit test --------- Co-authored-by: gradio-pr-bot --- .changeset/hot-cases-sneeze.md | 5 +++++ gradio/routes.py | 9 +++++---- test/test_routes.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 .changeset/hot-cases-sneeze.md diff --git a/.changeset/hot-cases-sneeze.md b/.changeset/hot-cases-sneeze.md new file mode 100644 index 0000000000000..c114c5f5e8016 --- /dev/null +++ b/.changeset/hot-cases-sneeze.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Use root url for monitoring url diff --git a/gradio/routes.py b/gradio/routes.py index 7c8af3e264aae..b92054df5b5b3 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -1167,11 +1167,12 @@ def robots_txt(): else: return "User-agent: *\nDisallow: " - @app.get("/monitoring") - async def analytics_login(): - print( - f"Monitoring URL: {app.get_blocks().local_url}monitoring/{app.analytics_key}" + @app.get("/monitoring", dependencies=[Depends(login_check)]) + async def analytics_login(request: fastapi.Request): + root = route_utils.get_root_url( + request=request, route_path="/monitoring", root_path=app.root_path ) + print(f"Monitoring URL: {root}/monitoring/{app.analytics_key}") return HTMLResponse("See console for monitoring URL.") @app.get("/monitoring/{key}") diff --git a/test/test_routes.py b/test/test_routes.py index ee1865d54e25e..e6219dcf981b8 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -634,6 +634,30 @@ def test_logout(self): ) assert response.status_code == 401 + def test_monitoring_route(self): + io = Interface(lambda x: x, "text", "text") + app, _, _ = io.launch( + auth=("test", "correct_password"), + prevent_thread_lock=True, + ) + client = TestClient(app) + client.post( + "/login", + data={"username": "test", "password": "correct_password"}, + ) + + response = client.get( + "/monitoring", + ) + assert response.status_code == 200 + + response = client.get("/logout") + + response = client.get( + "/monitoring", + ) + assert response.status_code == 401 + class TestQueueRoutes: @pytest.mark.asyncio