deploy: sync backend to Space root (learn-lesson HF cache fix)
Browse files- app/main.py +30 -1
app/main.py
CHANGED
|
@@ -6,7 +6,7 @@ import time
|
|
| 6 |
from fastapi import FastAPI, Request
|
| 7 |
from fastapi.exceptions import RequestValidationError
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
-
from fastapi.responses import JSONResponse
|
| 10 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 11 |
|
| 12 |
from app.core import rate_limiter
|
|
@@ -540,6 +540,35 @@ async def rate_limit_middleware(request: Request, call_next):
|
|
| 540 |
return await call_next(request)
|
| 541 |
|
| 542 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
STATUS_CODE_MAP = {
|
| 544 |
400: "BAD_REQUEST",
|
| 545 |
401: "UNAUTHORIZED",
|
|
|
|
| 6 |
from fastapi import FastAPI, Request
|
| 7 |
from fastapi.exceptions import RequestValidationError
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from fastapi.responses import JSONResponse, Response
|
| 10 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 11 |
|
| 12 |
from app.core import rate_limiter
|
|
|
|
| 540 |
return await call_next(request)
|
| 541 |
|
| 542 |
|
| 543 |
+
@app.middleware("http")
|
| 544 |
+
async def credentialed_cors_preflight(request: Request, call_next):
|
| 545 |
+
"""Answer OPTIONS with Allow-Credentials so browser signup is not blocked.
|
| 546 |
+
|
| 547 |
+
Hugging Face and some Starlette paths return an empty credentials header on
|
| 548 |
+
preflight. Chrome then rejects fetch({ credentials: 'include' }) from
|
| 549 |
+
docdoe.in to the Space, and the student only sees 'could not create account'.
|
| 550 |
+
"""
|
| 551 |
+
if request.method == "OPTIONS":
|
| 552 |
+
origin = request.headers.get("Origin", "")
|
| 553 |
+
if origin in settings.cors_origin_list:
|
| 554 |
+
requested = request.headers.get(
|
| 555 |
+
"Access-Control-Request-Headers",
|
| 556 |
+
"authorization,content-type",
|
| 557 |
+
)
|
| 558 |
+
return Response(
|
| 559 |
+
status_code=200,
|
| 560 |
+
headers={
|
| 561 |
+
"Access-Control-Allow-Origin": origin,
|
| 562 |
+
"Access-Control-Allow-Credentials": "true",
|
| 563 |
+
"Access-Control-Allow-Methods": "GET,POST,PUT,PATCH,DELETE,OPTIONS",
|
| 564 |
+
"Access-Control-Allow-Headers": requested,
|
| 565 |
+
"Access-Control-Max-Age": "600",
|
| 566 |
+
"Vary": "Origin",
|
| 567 |
+
},
|
| 568 |
+
)
|
| 569 |
+
return await call_next(request)
|
| 570 |
+
|
| 571 |
+
|
| 572 |
STATUS_CODE_MAP = {
|
| 573 |
400: "BAD_REQUEST",
|
| 574 |
401: "UNAUTHORIZED",
|