Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/google/adk/cli/adk_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ def get_fast_api_app(
self,
lifespan: Optional[Lifespan[FastAPI]] = None,
allow_origins: Optional[list[str]] = None,
allow_origin_regex: Optional[str] = None,
web_assets_dir: Optional[str] = None,
setup_observer: Callable[
[Observer, "AdkWebServer"], None
Expand Down Expand Up @@ -713,14 +714,20 @@ async def internal_lifespan(app: FastAPI):
# Run the FastAPI server.
app = FastAPI(lifespan=internal_lifespan)

if allow_origins:
app.add_middleware(
CORSMiddleware,
allow_origins=allow_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if allow_origins or allow_origin_regex:
cors_kwargs = {
"allow_credentials": True,
"allow_methods": ["*"],
"allow_headers": ["*"],
}

if allow_origins:
cors_kwargs["allow_origins"] = allow_origins

if allow_origin_regex:
cors_kwargs["allow_origin_regex"] = allow_origin_regex

app.add_middleware(CORSMiddleware, **cors_kwargs)

@app.get("/list-apps")
async def list_apps(
Expand Down
6 changes: 6 additions & 0 deletions src/google/adk/cli/cli_tools_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,10 @@ def decorator(func):
help="Optional. Any additional origins to allow for CORS.",
multiple=True,
)
@click.option(
"--allow_origin_regex",
help="Optional. A regex pattern for additional origins to allow for CORS.",
)
@click.option(
"-v",
"--verbose",
Expand Down Expand Up @@ -1223,6 +1227,7 @@ def cli_api_server(
eval_storage_uri: Optional[str] = None,
log_level: str = "INFO",
allow_origins: Optional[list[str]] = None,
allow_origin_regex: Optional[str] = None,
host: str = "127.0.0.1",
port: int = 8000,
url_prefix: Optional[str] = None,
Expand Down Expand Up @@ -1259,6 +1264,7 @@ def cli_api_server(
memory_service_uri=memory_service_uri,
eval_storage_uri=eval_storage_uri,
allow_origins=allow_origins,
allow_origin_regex=allow_origin_regex,
web=False,
trace_to_cloud=trace_to_cloud,
otel_to_cloud=otel_to_cloud,
Expand Down
2 changes: 2 additions & 0 deletions src/google/adk/cli/fast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def get_fast_api_app(
memory_service_uri: Optional[str] = None,
eval_storage_uri: Optional[str] = None,
allow_origins: Optional[list[str]] = None,
allow_origin_regex: Optional[str] = None,
web: bool,
a2a: bool = False,
host: str = "127.0.0.1",
Expand Down Expand Up @@ -207,6 +208,7 @@ def tear_down_observer(observer: Observer, _: AdkWebServer):
app = adk_web_server.get_fast_api_app(
lifespan=lifespan,
allow_origins=allow_origins,
allow_origin_regex=allow_origin_regex,
otel_to_cloud=otel_to_cloud,
**extra_fast_api_args,
)
Expand Down