-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rb/hidden-feedback
- Loading branch information
Showing
5 changed files
with
82 additions
and
36 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
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
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,43 @@ | ||
from urllib.parse import urlparse | ||
|
||
from fastapi.middleware.cors import CORSMiddleware | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.types import ASGIApp | ||
|
||
|
||
class LocalhostCORSMiddleware(CORSMiddleware): | ||
""" | ||
Custom CORS middleware that allows any request from localhost/127.0.0.1 domains, | ||
while using standard CORS rules for other origins. | ||
""" | ||
|
||
def __init__(self, app: ASGIApp, **kwargs) -> None: | ||
super().__init__(app, **kwargs) | ||
|
||
async def is_allowed_origin(self, origin: str) -> bool: | ||
if origin: | ||
parsed = urlparse(origin) | ||
hostname = parsed.hostname or '' | ||
|
||
# Allow any localhost/127.0.0.1 origin regardless of port | ||
if hostname in ['localhost', '127.0.0.1']: | ||
return True | ||
|
||
# For missing origin or other origins, use the parent class's logic | ||
return await super().is_allowed_origin(origin) | ||
|
||
|
||
class NoCacheMiddleware(BaseHTTPMiddleware): | ||
""" | ||
Middleware to disable caching for all routes by adding appropriate headers | ||
""" | ||
|
||
async def dispatch(self, request, call_next): | ||
response = await call_next(request) | ||
if not request.url.path.startswith('/assets'): | ||
response.headers['Cache-Control'] = ( | ||
'no-cache, no-store, must-revalidate, max-age=0' | ||
) | ||
response.headers['Pragma'] = 'no-cache' | ||
response.headers['Expires'] = '0' | ||
return response |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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