diff --git a/CHANGES.md b/CHANGES.md index b47b03aa..f12cda47 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)) diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index 194b22a0..ed9a1cc7 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -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 @@ -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=[]) @@ -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. @@ -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: