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 CORS configuration via env variables #168

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [3.0.1] - 2024-11-14
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: I've created a maint-3.0.x branch because I think this can't wait before the next minor release, set to be done once we merge #155

Copy link
Member Author

Choose a reason for hiding this comment

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

The changes will be back ported to the main branch later


- Enable runtime `CORS` configuration using environment variables (`CORS_ORIGIN="https://...,https://..."`, `CORS_METHODS="PUT,OPTIONS"`)

## [3.0.0] - 2024-08-02

- Enable filter extension for `GET /items` requests and add `Queryables` links in `/collections` and `/collections/{collection_id}` responses ([#89](https://github.com/stac-utils/stac-fastapi-pgstac/pull/89))
Expand Down
12 changes: 12 additions & 0 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import os

from brotli_asgi import BrotliMiddleware
from fastapi.responses import ORJSONResponse
from stac_fastapi.api.app import StacApi
from stac_fastapi.api.middleware import CORSMiddleware, ProxyHeaderMiddleware
from stac_fastapi.api.models import (
ItemCollectionUri,
create_get_request_model,
Expand All @@ -23,6 +25,7 @@
TransactionExtension,
)
from stac_fastapi.extensions.third_party import BulkTransactionExtension
from starlette.middleware import Middleware

from stac_fastapi.pgstac.config import Settings
from stac_fastapi.pgstac.core import CoreCrudClient
Expand Down Expand Up @@ -75,6 +78,15 @@
items_get_request_model=items_get_request_model,
search_get_request_model=get_request_model,
search_post_request_model=post_request_model,
middlewares=[
Middleware(BrotliMiddleware),
Middleware(ProxyHeaderMiddleware),
Middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_methods=settings.cors_methods,
),
],
)
app = api.app

Expand Down
15 changes: 14 additions & 1 deletion stac_fastapi/pgstac/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Type
from urllib.parse import quote_plus as quote

from pydantic import BaseModel
from pydantic import BaseModel, field_validator
from pydantic_settings import SettingsConfigDict
from stac_fastapi.types.config import ApiSettings

Expand Down Expand Up @@ -75,8 +75,21 @@ class Settings(ApiSettings):
base_item_cache: Type[BaseItemCache] = DefaultBaseItemCache
invalid_id_chars: List[str] = DEFAULT_INVALID_ID_CHARS

cors_origins: str = "*"
cors_methods: str = "GET,POST,OPTIONS"

testing: bool = False

@field_validator("cors_origins")
def parse_cors_origin(cls, v):
"""Parse CORS origins."""
return [origin.strip() for origin in v.split(",")]

@field_validator("cors_methods")
def parse_cors_methods(cls, v):
"""Parse CORS methods."""
return [method.strip() for method in v.split(",")]

@property
def reader_connection_string(self):
"""Create reader psql connection string."""
Expand Down