Flask + Hypercorn CORS middleware not working

76 Views Asked by At

I am migrating an application from gunicorn + flask to hypercorn and fastapi. As a first step I am just adding Hypercorn and wrapping the flask app with WSGIWrapper. When I attempt to test for cross origin requests, I get httpstream errors from Hypercorn:

  File ".../lib/python3.9/site-packages/hypercorn/asyncio/task_group.py", line 27, in _handle
    await app(scope, receive, send, sync_spawn, call_soon)
  File ".../lib/python3.9/site-packages/hypercorn/app_wrappers.py", line 51, in __call__
    await self.handle_http(scope, receive, send, sync_spawn, call_soon)
  File ".../lib/python3.9/site-packages/hypercorn/app_wrappers.py", line 84, in handle_http
    await send({"type": "http.response.body", "body": b"", "more_body": False})
  File "/.../lib/python3.9/site-packages/hypercorn/protocol/http_stream.py", line 168, in app_send
AttributeError: 'HTTPStream' object has no attribute 'response'

I create my flask app and server like so:

    wsgi_app = Flask(import_name, static_folder=None)
    uvloop.install()
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    CORS(app=wsgi_app, origins=permitted_cors_origins, expose_headers=["Start-Time", "End-Time"])

    asyncio.run(serve(
        app=wsgi_app,
        config=config,
        mode="wsgi",
    ))

and using hypercorn = "^0.16.0". Is there something I am missing in my configuration? There doesn't seem to be any additional configuration implied in the hypercorn docs.

0

There are 0 best solutions below