diff --git a/cid/config.py b/cid/config.py index dbf1bc1..fe61421 100644 --- a/cid/config.py +++ b/cid/config.py @@ -23,3 +23,8 @@ # Supported cloud providers. CLOUD_PROVIDERS = ["aws", "azure", "google"] + +# Allowed origins for CORS. +CORS_ORIGINS = [ + "https://cloudx-cidv2.fly.dev/", +] diff --git a/cid/main.py b/cid/main.py index 1822b2a..bafa56a 100644 --- a/cid/main.py +++ b/cid/main.py @@ -5,11 +5,12 @@ from fastapi import Depends, FastAPI, HTTPException, Request from fastapi.encoders import jsonable_encoder +from fastapi.middleware.cors import CORSMiddleware from schedule import every, repeat, run_pending from sqlalchemy.orm import Session from cid import crud -from cid.config import ENVIRONMENT +from cid.config import CORS_ORIGINS, ENVIRONMENT from cid.database import SessionLocal log = logging.getLogger(__name__) @@ -18,6 +19,15 @@ app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=CORS_ORIGINS, + allow_credentials=True, + allow_methods=["GET"], + allow_headers=["*"], +) + + def get_db() -> Generator: db = SessionLocal() try: diff --git a/poetry.lock b/poetry.lock index 4529c35..a313baf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -234,6 +234,26 @@ files = [ dnspython = ">=2.0.0" idna = ">=2.0.0" +[[package]] +name = "environs" +version = "11.0.0" +description = "simplified environment variable parsing" +optional = false +python-versions = ">=3.8" +files = [ + {file = "environs-11.0.0-py3-none-any.whl", hash = "sha256:e0bcfd41c718c07a7db422f9109e490746450da38793fe4ee197f397b9343435"}, + {file = "environs-11.0.0.tar.gz", hash = "sha256:069727a8f73d8ba8d033d3cd95c0da231d44f38f1da773bf076cef168d312ee8"}, +] + +[package.dependencies] +marshmallow = ">=3.13.0" +python-dotenv = "*" + +[package.extras] +dev = ["environs[tests]", "pre-commit (>=3.5,<4.0)", "tox"] +django = ["dj-database-url", "dj-email-url", "django-cache-url"] +tests = ["environs[django]", "pytest"] + [[package]] name = "fastapi" version = "0.111.1" @@ -276,6 +296,21 @@ typer = ">=0.12.3" [package.extras] standard = ["fastapi", "uvicorn[standard] (>=0.15.0)"] +[[package]] +name = "fastapi-cors" +version = "0.0.6" +description = "Simple env support of CORS settings for Fastapi applications" +optional = false +python-versions = ">=3.8" +files = [ + {file = "fastapi-cors-0.0.6.tar.gz", hash = "sha256:17eeb92f2b3dd8f0deb8ca69f031760e35edd5a0c811aaf80f9743e5e6ae50a1"}, + {file = "fastapi_cors-0.0.6-py3-none-any.whl", hash = "sha256:d116b482c682f9c5330f04b1c49a9d504f3a9df6373bc43dd6c31f3b9d0b8b15"}, +] + +[package.dependencies] +environs = ">=9.5.0" +fastapi = ">=0.99.1" + [[package]] name = "filelock" version = "3.15.4" @@ -613,6 +648,25 @@ files = [ {file = "MarkupSafe-2.1.5.tar.gz", hash = "sha256:d283d37a890ba4c1ae73ffadf8046435c76e7bc2247bbb63c00bd1a709c6544b"}, ] +[[package]] +name = "marshmallow" +version = "3.21.3" +description = "A lightweight library for converting complex datatypes to and from native Python datatypes." +optional = false +python-versions = ">=3.8" +files = [ + {file = "marshmallow-3.21.3-py3-none-any.whl", hash = "sha256:86ce7fb914aa865001a4b2092c4c2872d13bc347f3d42673272cabfdbad386f1"}, + {file = "marshmallow-3.21.3.tar.gz", hash = "sha256:4f57c5e050a54d66361e826f94fba213eb10b67b2fdb02c3e0343ce207ba1662"}, +] + +[package.dependencies] +packaging = ">=17.0" + +[package.extras] +dev = ["marshmallow[tests]", "pre-commit (>=3.5,<4.0)", "tox"] +docs = ["alabaster (==0.7.16)", "autodocsumm (==0.2.12)", "sphinx (==7.3.7)", "sphinx-issues (==4.1.0)", "sphinx-version-warning (==1.1.2)"] +tests = ["pytest", "pytz", "simplejson"] + [[package]] name = "mdurl" version = "0.1.2" @@ -1727,4 +1781,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = ">=3.12,<4.0" -content-hash = "1bd4a912ddbad70acdc32b9d7c0f522197872a53b058c2c24f0c3560add2d978" +content-hash = "5e1bf6bca66b2e2a12e9c58752554900b6e214ba796431b09809d277efe09cbc" diff --git a/pyproject.toml b/pyproject.toml index 476c0a5..6e300af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ httpx = "^0.27.0" schedule = "^1.2.2" python-dateutil = "^2.9.0-post.0" packaging = "^24.1" +fastapi-cors = "^0.0.6" [tool.poetry.group.dev.dependencies] pytest = "^8.2.2"