Skip to content

Commit

Permalink
Merge pull request #105 from ricardogsilva/101-allow-configuring-CORS…
Browse files Browse the repository at this point in the history
…-on-the-backend-server

Added CORS middleware to API v2 app
  • Loading branch information
francbartoli authored Jun 3, 2024
2 parents 165ffaa + 3c9a24a commit 17074b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions arpav_ppcv/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class ArpavPpcvSettings(BaseSettings): # noqa
log_config_file: Path | None = None
session_secret_key: str = "changeme"
admin_user: AdminUserSettings = AdminUserSettings()
cors_origins: list[str] = []
cors_methods: list[str] = []
allow_cors_credentials: bool = False

@pydantic.model_validator(mode="after")
def ensure_test_db_dsn(self):
Expand Down
8 changes: 8 additions & 0 deletions arpav_ppcv/webapp/api_v2/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fastapi
from fastapi.middleware.cors import CORSMiddleware

from ... import config
from .routers.coverages import router as coverages_router
Expand All @@ -21,6 +22,13 @@ def create_app(settings: config.ArpavPpcvSettings) -> fastapi.FastAPI:
"email": settings.contact.email,
},
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=settings.allow_cors_credentials,
allow_methods=settings.cors_methods,
allow_headers=["*"],
)
app.include_router(
base_router,
prefix="/base",
Expand Down
5 changes: 4 additions & 1 deletion docker/compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ services:
webapp:
image: *webapp-image
environment:
*common-env
<<: *common-env
ARPAV_PPCV__CORS_ORIGINS: '["*"]'
ARPAV_PPCV__CORS_METHODS: '["*"]'
ARPAV_PPCV__ALLOW_CORS_CREDENTIALS: true
ports:
- target: 5001
published: 5001
Expand Down

0 comments on commit 17074b3

Please sign in to comment.