Skip to content

Commit e7f82d6

Browse files
geospatial-jeffgadomskijonhealy1vincentsarago
authored
allow user to pass middleware options (#442)
* 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]>
1 parent 63cac39 commit e7f82d6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
* 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))
8+
* Add Response Model to OpenAPI, even if model validation is turned off ([#625](https://github.com/stac-utils/stac-fastapi/pull/625))
9+
510
## Changes
611

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

stac_fastapi/api/stac_fastapi/api/app.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from stac_pydantic.api.collections import Collections
1313
from stac_pydantic.api.version import STAC_API_VERSION
1414
from stac_pydantic.shared import MimeTypes
15+
from starlette.middleware import Middleware
1516
from starlette.responses import JSONResponse, Response
1617

1718
from stac_fastapi.api.errors import DEFAULT_STATUS_CODES, add_exception_handlers
@@ -109,9 +110,13 @@ class StacApi:
109110
)
110111
pagination_extension = attr.ib(default=TokenPaginationExtension)
111112
response_class: Type[Response] = attr.ib(default=JSONResponse)
112-
middlewares: List = attr.ib(
113+
middlewares: List[Middleware] = attr.ib(
113114
default=attr.Factory(
114-
lambda: [BrotliMiddleware, CORSMiddleware, ProxyHeaderMiddleware]
115+
lambda: [
116+
Middleware(BrotliMiddleware),
117+
Middleware(CORSMiddleware),
118+
Middleware(ProxyHeaderMiddleware),
119+
]
115120
)
116121
)
117122
route_dependencies: List[Tuple[List[Scope], List[Depends]]] = attr.ib(default=[])
@@ -434,6 +439,11 @@ def add_route_dependencies(
434439
"""
435440
return add_route_dependencies(self.app.router.routes, scopes, dependencies)
436441

442+
def add_middleware(self, middleware: Middleware):
443+
"""Add a middleware class to the application."""
444+
self.app.user_middleware.insert(0, middleware)
445+
self.app.middleware_stack = self.app.build_middleware_stack()
446+
437447
def __attrs_post_init__(self):
438448
"""Post-init hook.
439449
@@ -478,7 +488,7 @@ def __attrs_post_init__(self):
478488

479489
# add middlewares
480490
for middleware in self.middlewares:
481-
self.app.add_middleware(middleware)
491+
self.add_middleware(middleware)
482492

483493
# customize route dependencies
484494
for scopes, dependencies in self.route_dependencies:

0 commit comments

Comments
 (0)