Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI --allow-origin (CORS) #343

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
8 changes: 8 additions & 0 deletions jupyverse_api/jupyverse_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
default=8000,
help="The host port.",
)
@click.option(
"--allow-origin",
multiple=True,
type=str,
help="The origin to allow.",
)
@click.option(
"--set",
"set_",
Expand All @@ -44,13 +50,15 @@ def main(
port: int,
set_: List[str],
disable: List[str],
allow_origin: List[str],
) -> None:
set_ = list(set_)
for i, s in enumerate(set_):
set_[i] = f"component.components.{s}"
set_.append(f"component.open_browser={open_browser}")
set_.append(f"component.host={host}")
set_.append(f"component.port={port}")
set_.append(f"component.allow_origin={allow_origin}")
config = get_config(disable)
run.callback(
unsafe=False,
Expand Down
13 changes: 12 additions & 1 deletion jupyverse_api/jupyverse_api/main/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import webbrowser
from typing import Any, Callable, Dict, Sequence
from typing import Any, Callable, Dict, Sequence, Tuple

from asgiref.typing import ASGI3Application
from asphalt.core import Component, Context
from asphalt.web.fastapi import FastAPIComponent
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

from ..app import App
Expand Down Expand Up @@ -39,11 +40,21 @@ def __init__(
app: FastAPI | str | None = None,
host: str = "127.0.0.1",
port: int = 8000,
allow_origin: Tuple = (),
open_browser: bool = False,
query_params: Dict[str, Any] | None = None,
debug: bool | None = None,
middlewares: Sequence[Callable[..., ASGI3Application] | dict[str, Any]] = (),
) -> None:
if allow_origin:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, allow_origin is a string, not a tuple. Should it be cast to a tuple before configuring the middleware?

middleware = {
"type": CORSMiddleware,
"allow_origins": allow_origin,
"allow_credentials": True,
"allow_methods": ["*"],
"allow_headers": ["*"],
}
middlewares = list(middlewares) + [middleware]
super().__init__(
components, # type: ignore
app=app,
Expand Down
Loading