Skip to content

Fix cors #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions pkg/workloads/cortex/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from fastapi import Body, FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import Response
from starlette.background import BackgroundTasks
Expand Down Expand Up @@ -55,6 +56,15 @@
)

app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

local_cache = {"api": None, "predictor_impl": None, "client": None, "class_set": set()}


Expand Down Expand Up @@ -90,21 +100,18 @@ def is_prediction_request(request):
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, e):
response = Response(content=str(e.detail), status_code=e.status_code)
apply_cors_headers(request, response)
return response


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, e):
response = Response(content=str(e), status_code=400)
apply_cors_headers(request, response)
return response


@app.exception_handler(Exception)
async def uncaught_exception_handler(request, e):
response = Response(content="internal server error", status_code=500)
apply_cors_headers(request, response)
return response


Expand Down Expand Up @@ -132,20 +139,12 @@ async def register_request(request: Request, call_next):
status_code = 500
if response is not None:
status_code = response.status_code
apply_cors_headers(request, response)
api = local_cache["api"]
api.post_request_metrics(status_code, time.time() - request.state.start_time)

return response


def apply_cors_headers(request: Request, response: Response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = request.headers.get(
"Access-Control-Request-Headers", "*"
)


@app.post("/predict")
def predict(request: Any = Body(..., media_type="application/json"), debug=False):
api = local_cache["api"]
Expand Down