-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CORS middleware for fastapi to make it easier to use
- Loading branch information
1 parent
b4f57e1
commit 54c3c0b
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Convenience method to easily extend FastAPI servers | ||
with the appropriate CORS Middleware to allow | ||
interactions from the Encord platform. | ||
""" | ||
|
||
import typing | ||
|
||
try: | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from starlette.types import ASGIApp | ||
except ModuleNotFoundError: | ||
print( | ||
'To use the `fastapi` dependencies, you must also install fastapi. `python -m pip install "fastapi[standard]"' | ||
) | ||
exit() | ||
|
||
ENCORD_DOMAIN_REGEX = ( | ||
r"^https:\/\/(?:(?:cord-ai-development--[\w\d]+-[\w\d]+\.web.app)|(?:(?:dev|staging|app)\.encord\.com))$" | ||
) | ||
|
||
|
||
class EncordCORSMiddleware(CORSMiddleware): | ||
""" | ||
Like a regular `fastapi.midleware.cors.CORSMiddleware` but matches against | ||
the encord origin by default. | ||
**Example:** | ||
```python | ||
from fastapi import FastAPI | ||
from encord_agents.fastapi.cors import EncordCORSMiddleware | ||
app = FastAPI() | ||
app.add_middleware(EncordCORSMiddleware) | ||
``` | ||
The CORS middleware will allow POST requests from the Encord domain. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
app: ASGIApp, | ||
allow_origins: typing.Sequence[str] = (), | ||
allow_methods: typing.Sequence[str] = ("POST",), | ||
allow_headers: typing.Sequence[str] = (), | ||
allow_credentials: bool = False, | ||
allow_origin_regex: str = ENCORD_DOMAIN_REGEX, | ||
expose_headers: typing.Sequence[str] = (), | ||
max_age: int = 3600, | ||
) -> None: | ||
super().__init__( | ||
app, | ||
allow_origins, | ||
allow_methods, | ||
allow_headers, | ||
allow_credentials, | ||
allow_origin_regex, | ||
expose_headers, | ||
max_age, | ||
) |