Skip to content

Adding CORSMiddleware #741

@nikkopante

Description

@nikkopante

I am trying to add CORSMiddleware but having some trouble to implement it. I followed the instructions in the manual. However, I think it is not accepting the syntax in the manual since it cannot iterate over the parameters added to the StacApi class.

stac-fastapi-pgstac  | INFO:     Uvicorn running on http://0.0.0.0:8082 (Press CTRL+C to quit)
stac-fastapi-pgstac  | INFO:     Started parent process [1]
stac-fastapi-pgstac  | INFO:     Started server process [21]
stac-fastapi-pgstac  | INFO:     Waiting for application startup.
stac-fastapi-pgstac  | INFO:     ASGI 'lifespan' protocol appears unsupported.
stac-fastapi-pgstac  | ERROR:    Exception in ASGI application
stac-fastapi-pgstac  | Traceback (most recent call last):
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
stac-fastapi-pgstac  |     result = await app(  # type: ignore[func-returns-value]
stac-fastapi-pgstac  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
stac-fastapi-pgstac  |     return await self.app(scope, receive, send)
stac-fastapi-pgstac  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
stac-fastapi-pgstac  |     await super().__call__(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
stac-fastapi-pgstac  |     self.middleware_stack = self.build_middleware_stack()
stac-fastapi-pgstac  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 108, in build_middleware_stack
stac-fastapi-pgstac  |     for cls, args, kwargs in reversed(middleware):
stac-fastapi-pgstac  |         ^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  | TypeError: cannot unpack non-iterable function object

After this and some digging in the FastAPI docs, I implemented it after the StacApi class and appended the middleware after it.

api = StacApi(
    settings=settings,
    extensions=extensions,
    client=CoreCrudClient(post_request_model=post_request_model),  # type: ignore
    response_class=ORJSONResponse,
    items_get_request_model=items_get_request_model,
    search_get_request_model=get_request_model,
    search_post_request_model=post_request_model,
)
app = api.app
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["OPTIONS", "GET"],
    allow_headers=["Content-Type", "Authorization"],
    )

I now get a different error and still my stac-browser cannot view my API because of CORS.

stac-fastapi-pgstac  | 'State' object has no attribute 'get_connection'
stac-fastapi-pgstac  | NoneType: None

stac-fastapi-pgstac  | ERROR:    Exception in ASGI application
stac-fastapi-pgstac  | Traceback (most recent call last):
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/datastructures.py", line 699, in __getattr__
stac-fastapi-pgstac  |     return self._state[key]
stac-fastapi-pgstac  |            ~~~~~~~~~~~^^^^^
stac-fastapi-pgstac  | KeyError: 'get_connection'
stac-fastapi-pgstac  |
stac-fastapi-pgstac  | During handling of the above exception, another exception occurred:
stac-fastapi-pgstac  |
stac-fastapi-pgstac  | Traceback (most recent call last):
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
stac-fastapi-pgstac  |     result = await app(  # type: ignore[func-returns-value]
stac-fastapi-pgstac  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
stac-fastapi-pgstac  |     return await self.app(scope, receive, send)
stac-fastapi-pgstac  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
stac-fastapi-pgstac  |     await super().__call__(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
stac-fastapi-pgstac  |     await self.middleware_stack(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
stac-fastapi-pgstac  |     raise exc
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
stac-fastapi-pgstac  |     await self.app(scope, receive, _send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/cors.py", line 85, in __call__
stac-fastapi-pgstac  |     await self.app(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/stac_fastapi/api/middleware.py", line 76, in __call__
stac-fastapi-pgstac  |     await self.app(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/cors.py", line 85, in __call__
stac-fastapi-pgstac  |     await self.app(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/brotli_asgi/__init__.py", line 85, in __call__
stac-fastapi-pgstac  |     await gzip_responder(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/gzip.py", line 44, in __call__
stac-fastapi-pgstac  |     await self.app(scope, receive, self.send_with_gzip)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
stac-fastapi-pgstac  |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
stac-fastapi-pgstac  |     raise exc
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
stac-fastapi-pgstac  |     await app(scope, receive, sender)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__
stac-fastapi-pgstac  |     await self.middleware_stack(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 776, in app
stac-fastapi-pgstac  |     await route.handle(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
stac-fastapi-pgstac  |     await self.app(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
stac-fastapi-pgstac  |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
stac-fastapi-pgstac  |     raise exc
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
stac-fastapi-pgstac  |     await app(scope, receive, sender)
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 72, in app
stac-fastapi-pgstac  |     response = await func(request)
stac-fastapi-pgstac  |                ^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 278, in app
stac-fastapi-pgstac  |     raw_response = await run_endpoint_function(
stac-fastapi-pgstac  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
stac-fastapi-pgstac  |     return await dependant.call(**values)
stac-fastapi-pgstac  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/stac_fastapi/api/routes.py", line 64, in _endpoint
stac-fastapi-pgstac  |     return _wrap_response(await func(request=request, **request_data.kwargs()))
stac-fastapi-pgstac  |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/stac_fastapi/types/core.py", line 644, in landing_page
stac-fastapi-pgstac  |     collections = await self.all_collections(request=kwargs["request"])
stac-fastapi-pgstac  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/app/stac_fastapi/pgstac/core.py", line 46, in all_collections
stac-fastapi-pgstac  |     async with request.app.state.get_connection(request, "r") as conn:
stac-fastapi-pgstac  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stac-fastapi-pgstac  |   File "/usr/local/lib/python3.11/site-packages/starlette/datastructures.py", line 702, in __getattr__
stac-fastapi-pgstac  |     raise AttributeError(message.format(self.__class__.__name__, key))
stac-fastapi-pgstac  | AttributeError: 'State' object has no attribute 'get_connection'

My STAC Browser error:
The requested page could not be loaded. Reason: This issue may occur when servers don't allow external access via web browsers (e.g., when CORS headers are not present). Please contact the service operator to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions