Skip to content

Commit

Permalink
Fix BrokenResourceError on starlette. See fastapi/fastapi#4041 for mo…
Browse files Browse the repository at this point in the history
…re details.

Add middleware for handle requests without headers in request.
  • Loading branch information
eliorerz committed Jan 14, 2022
1 parent 4853c39 commit 62a238a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ COPY . app/
WORKDIR app/
RUN python3 -m pip install -I --no-cache-dir -r requirements.txt vcversioner

RUN python3 -m pip install -I --no-cache-dir starlette==0.17.0 # sohuld be removed after fastapi update

CMD ["python3", "-m", "bug_master"]
9 changes: 9 additions & 0 deletions bug_master/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi import FastAPI, Request, Response
from fastapi.routing import APIRoute, APIRouter
from slack_sdk import signature
from starlette.responses import JSONResponse, StreamingResponse
from uvicorn_loguru_integration import run_uvicorn_loguru

from . import consts
Expand Down Expand Up @@ -56,6 +57,14 @@ async def custom_route_handler(request: Request) -> Response:
signature_verifier = signature.SignatureVerifier(consts.SIGNING_SECRET)


@app.middleware('http')
async def headers_middleware(request: Request, call_next: Callable):
if not hasattr(request, "headers"):
return JSONResponse(content={"message": "Invalid request"}, status_code=401)
response: StreamingResponse = await call_next(request)
return response


@router.post("/slack/config")
async def config(request: Request):
body = {k.decode(): v.pop().decode() for k, v in parse_qs(await request.body()).items()}
Expand Down

0 comments on commit 62a238a

Please sign in to comment.