Skip to content

Commit

Permalink
allow user to pass middleware options (#442)
Browse files Browse the repository at this point in the history
* allow user to pass middleware options to stac api constructor, proxy add_middleware call

* update changelog

---------

Co-authored-by: Pete Gadomski <[email protected]>
Co-authored-by: Jonathan Healy <[email protected]>
Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
4 people authored Apr 26, 2024
1 parent 63cac39 commit e7f82d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit e7f82d6

Please sign in to comment.