Skip to content

Commit

Permalink
Add CLI --allow-origin (CORS)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Aug 30, 2023
1 parent 1824512 commit 72a90cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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:
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

0 comments on commit 72a90cb

Please sign in to comment.