From 4491fa1ee384cd3e789e30aaa1252cdbb2ea3246 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 17 Oct 2024 13:56:03 +0200 Subject: [PATCH] update CORS documentation (#764) --- docs/src/tips-and-tricks.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/src/tips-and-tricks.md b/docs/src/tips-and-tricks.md index c47433d0..6c8e2537 100644 --- a/docs/src/tips-and-tricks.md +++ b/docs/src/tips-and-tricks.md @@ -2,24 +2,25 @@ This page contains a few 'tips and tricks' for getting **stac-fastapi** working in various situations. -## Get stac-fastapi working with CORS +## Application Middlewares -CORS (Cross-Origin Resource Sharing) support may be required to use stac-fastapi in certain situations. -For example, if you are running [stac-browser](https://github.com/radiantearth/stac-browser) to browse the STAC catalog created by **stac-fastapi**, then you will need to enable CORS support. -To do this, edit your backend's `app.py` and add the following import: +By default the `StacApi` class will enable 3 Middlewares (`BrotliMiddleware`, `CORSMiddleware` and `ProxyHeaderMiddleware`). You may want to overwrite the defaults configuration by editing your backend's `app.py`: ```python -from fastapi.middleware.cors import CORSMiddleware -``` - -and then edit the `api = StacApi(...` call to add the following parameter: +from starlette.middleware import Middleware -```python -middlewares=[lambda app: CORSMiddleware(app, allow_origins=["*"])] +from stac_fastapi.api.app import StacApi +from stac_fastapi.api.middleware import CORSMiddleware + +api = StacApi( + ... + middlewares=[ + Middleware(CORSMiddleware, allow_origins=["https://myendpoints.io"]) + ], + ... +) ``` -If needed, you can edit the `allow_origins` parameter to only allow CORS requests from specific origins. - ## Set API title, description and version For the landing page, you can set the API title, description and version using environment variables.