ilyass yani commited on
Commit ·
be3fdd0
1
Parent(s): 1fca983
force https
Browse files- app/main.py +11 -3
app/main.py
CHANGED
|
@@ -23,7 +23,7 @@ class HTTPSRedirectMiddleware(BaseHTTPMiddleware):
|
|
| 23 |
but should redirect to HTTPS. Starlette's redirect_slashes uses the request scheme,
|
| 24 |
so we wrap the scope to force HTTPS redirects in production.
|
| 25 |
"""
|
| 26 |
-
async def dispatch(self, request: Request, call_next):
|
| 27 |
# In production, ensure the scheme seen by Starlette is HTTPS
|
| 28 |
# by checking X-Forwarded-Proto header (set by reverse proxies)
|
| 29 |
if (os.getenv("NODE_ENV") == "production" or
|
|
@@ -33,6 +33,11 @@ class HTTPSRedirectMiddleware(BaseHTTPMiddleware):
|
|
| 33 |
# Force the scope to use https so redirects are generated correctly
|
| 34 |
request.scope["scheme"] = "https"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return await call_next(request)
|
| 37 |
|
| 38 |
|
|
@@ -47,9 +52,12 @@ app = FastAPI(
|
|
| 47 |
redirect_slashes=True,
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
# Add HTTPS redirect middleware BEFORE CORS to catch all requests
|
| 51 |
-
if os.getenv("ENABLE_HTTPS_REDIRECT", "false").lower() == "true":
|
| 52 |
-
app.add_middleware(HTTPSRedirectMiddleware)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Configure CORS
|
| 55 |
allowed_origins = [
|
|
|
|
| 23 |
but should redirect to HTTPS. Starlette's redirect_slashes uses the request scheme,
|
| 24 |
so we wrap the scope to force HTTPS redirects in production.
|
| 25 |
"""
|
| 26 |
+
""" async def dispatch(self, request: Request, call_next):
|
| 27 |
# In production, ensure the scheme seen by Starlette is HTTPS
|
| 28 |
# by checking X-Forwarded-Proto header (set by reverse proxies)
|
| 29 |
if (os.getenv("NODE_ENV") == "production" or
|
|
|
|
| 33 |
# Force the scope to use https so redirects are generated correctly
|
| 34 |
request.scope["scheme"] = "https"
|
| 35 |
|
| 36 |
+
return await call_next(request) """
|
| 37 |
+
async def dispatch(self, request: Request, call_next):
|
| 38 |
+
forwarded_proto = request.headers.get("x-forwarded-proto", "").lower()
|
| 39 |
+
if forwarded_proto == "https":
|
| 40 |
+
request.scope["scheme"] = "https"
|
| 41 |
return await call_next(request)
|
| 42 |
|
| 43 |
|
|
|
|
| 52 |
redirect_slashes=True,
|
| 53 |
)
|
| 54 |
|
| 55 |
+
|
| 56 |
# Add HTTPS redirect middleware BEFORE CORS to catch all requests
|
| 57 |
+
""" if os.getenv("ENABLE_HTTPS_REDIRECT", "false").lower() == "true":
|
| 58 |
+
app.add_middleware(HTTPSRedirectMiddleware) """
|
| 59 |
+
app.add_middleware(HTTPSRedirectMiddleware)
|
| 60 |
+
|
| 61 |
|
| 62 |
# Configure CORS
|
| 63 |
allowed_origins = [
|