Skip to content

Commit a0df75b

Browse files
wyf7107copybara-github
authored andcommitted
chore: Add support for reversed proxy in adk web, users can use an optional param --root_path for proxy's path
Co-authored-by: Yifan Wang <wanyif@google.com> PiperOrigin-RevId: 826632932
1 parent 0487eea commit a0df75b

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def __init__(
436436
extra_plugins: Optional[list[str]] = None,
437437
logo_text: Optional[str] = None,
438438
logo_image_url: Optional[str] = None,
439+
url_prefix: Optional[str] = None,
439440
):
440441
self.agent_loader = agent_loader
441442
self.session_service = session_service
@@ -452,6 +453,7 @@ def __init__(
452453
self.runners_to_clean: set[str] = set()
453454
self.current_app_name_ref: SharedValue[str] = SharedValue(value="")
454455
self.runner_dict = {}
456+
self.url_prefix = url_prefix
455457

456458
async def get_runner_async(self, app_name: str) -> Runner:
457459
"""Returns the cached runner for the given app."""
@@ -559,6 +561,7 @@ def _setup_runtime_config(self, web_assets_dir: str):
559561
" overwritten.",
560562
runtime_config_path,
561563
)
564+
runtime_config["backendUrl"] = self.url_prefix if self.url_prefix else ""
562565

563566
# Set custom logo config.
564567
if self.logo_text or self.logo_image_url:
@@ -1562,6 +1565,10 @@ async def process_messages():
15621565
mimetypes.add_type("application/javascript", ".js", True)
15631566
mimetypes.add_type("text/javascript", ".js", True)
15641567

1568+
redirect_dev_ui_url = (
1569+
self.url_prefix + "/dev-ui/" if self.url_prefix else "/dev-ui/"
1570+
)
1571+
15651572
@app.get("/dev-ui/config")
15661573
async def get_ui_config():
15671574
return {
@@ -1571,11 +1578,11 @@ async def get_ui_config():
15711578

15721579
@app.get("/")
15731580
async def redirect_root_to_dev_ui():
1574-
return RedirectResponse("/dev-ui/")
1581+
return RedirectResponse(redirect_dev_ui_url)
15751582

15761583
@app.get("/dev-ui")
15771584
async def redirect_dev_ui_add_slash():
1578-
return RedirectResponse("/dev-ui/")
1585+
return RedirectResponse(redirect_dev_ui_url)
15791586

15801587
app.mount(
15811588
"/dev-ui/",

src/google/adk/cli/cli_tools_click.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,17 @@ def decorator(func):
10281028
),
10291029
multiple=True,
10301030
)
1031+
@click.option(
1032+
"--url_prefix",
1033+
type=str,
1034+
help=(
1035+
"Optional. URL path prefix when the application is mounted behind a"
1036+
" reverse proxy or API gateway (e.g., '/api/v1', '/adk'). This"
1037+
" ensures generated URLs and redirects work correctly when the app"
1038+
" is not served at the root path. Must start with '/' if provided."
1039+
),
1040+
default=None,
1041+
)
10311042
@functools.wraps(func)
10321043
@click.pass_context
10331044
def wrapper(ctx, *args, **kwargs):
@@ -1065,6 +1076,7 @@ def cli_web(
10651076
allow_origins: Optional[list[str]] = None,
10661077
host: str = "127.0.0.1",
10671078
port: int = 8000,
1079+
url_prefix: Optional[str] = None,
10681080
trace_to_cloud: bool = False,
10691081
otel_to_cloud: bool = False,
10701082
reload: bool = True,
@@ -1128,6 +1140,7 @@ async def _lifespan(app: FastAPI):
11281140
a2a=a2a,
11291141
host=host,
11301142
port=port,
1143+
url_prefix=url_prefix,
11311144
reload_agents=reload_agents,
11321145
extra_plugins=extra_plugins,
11331146
logo_text=logo_text,
@@ -1164,6 +1177,7 @@ def cli_api_server(
11641177
allow_origins: Optional[list[str]] = None,
11651178
host: str = "127.0.0.1",
11661179
port: int = 8000,
1180+
url_prefix: Optional[str] = None,
11671181
trace_to_cloud: bool = False,
11681182
otel_to_cloud: bool = False,
11691183
reload: bool = True,
@@ -1203,6 +1217,7 @@ def cli_api_server(
12031217
a2a=a2a,
12041218
host=host,
12051219
port=port,
1220+
url_prefix=url_prefix,
12061221
reload_agents=reload_agents,
12071222
extra_plugins=extra_plugins,
12081223
),

src/google/adk/cli/fast_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def get_fast_api_app(
6464
a2a: bool = False,
6565
host: str = "127.0.0.1",
6666
port: int = 8000,
67+
url_prefix: Optional[str] = None,
6768
trace_to_cloud: bool = False,
6869
otel_to_cloud: bool = False,
6970
reload_agents: bool = False,
@@ -144,6 +145,7 @@ def get_fast_api_app(
144145
extra_plugins=extra_plugins,
145146
logo_text=logo_text,
146147
logo_image_url=logo_image_url,
148+
url_prefix=url_prefix,
147149
)
148150

149151
# Callbacks & other optional args for when constructing the FastAPI instance

0 commit comments

Comments
 (0)