Skip to content

allow user to pass middleware options #442

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

Merged
merged 9 commits into from
Apr 26, 2024
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

## [Unreleased]

### Added

* Add enhanced middleware configuration to the StacApi class, enabling specific middleware options and dynamic addition post-application initialization. ([#442](https://github.com/stac-utils/stac-fastapi/pull/442))
* Add Response Model to OpenAPI, even if model validation is turned off ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))

## Changes

* Update to pydantic v2 and stac_pydantic v3 ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
* Removed internal Search and Operator Types in favor of stac_pydantic Types ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
* Fix response model validation ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
* Add Response Model to OpenAPI, even if model validation is turned off ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
* Use status code 201 for Item/Collection creation ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
* Replace Black with Ruff Format ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))

Expand Down
16 changes: 13 additions & 3 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from stac_pydantic.api.collections import Collections
from stac_pydantic.api.version import STAC_API_VERSION
from stac_pydantic.shared import MimeTypes
from starlette.middleware import Middleware
from starlette.responses import JSONResponse, Response

from stac_fastapi.api.errors import DEFAULT_STATUS_CODES, add_exception_handlers
Expand Down Expand Up @@ -109,9 +110,13 @@ class StacApi:
)
pagination_extension = attr.ib(default=TokenPaginationExtension)
response_class: Type[Response] = attr.ib(default=JSONResponse)
middlewares: List = attr.ib(
middlewares: List[Middleware] = attr.ib(
default=attr.Factory(
lambda: [BrotliMiddleware, CORSMiddleware, ProxyHeaderMiddleware]
lambda: [
Middleware(BrotliMiddleware),
Middleware(CORSMiddleware),
Middleware(ProxyHeaderMiddleware),
]
)
)
route_dependencies: List[Tuple[List[Scope], List[Depends]]] = attr.ib(default=[])
Expand Down Expand Up @@ -434,6 +439,11 @@ def add_route_dependencies(
"""
return add_route_dependencies(self.app.router.routes, scopes, dependencies)

def add_middleware(self, middleware: Middleware):
"""Add a middleware class to the application."""
self.app.user_middleware.insert(0, middleware)
self.app.middleware_stack = self.app.build_middleware_stack()

def __attrs_post_init__(self):
"""Post-init hook.

Expand Down Expand Up @@ -478,7 +488,7 @@ def __attrs_post_init__(self):

# add middlewares
for middleware in self.middlewares:
self.app.add_middleware(middleware)
self.add_middleware(middleware)

# customize route dependencies
for scopes, dependencies in self.route_dependencies:
Expand Down