Refactor code structure for improved readability and maintainability
This commit is contained in:
parent
389d72a136
commit
aa4c067ea8
1685 changed files with 393439 additions and 71932 deletions
45
.venv_codegen/Lib/site-packages/blackd/middlewares.py
Normal file
45
.venv_codegen/Lib/site-packages/blackd/middlewares.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from collections.abc import Awaitable, Callable, Collection, Iterable
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.typedefs import Middleware
|
||||
from aiohttp.web_middlewares import middleware
|
||||
from aiohttp.web_request import Request
|
||||
from aiohttp.web_response import StreamResponse
|
||||
|
||||
Handler = Callable[[Request], Awaitable[StreamResponse]]
|
||||
|
||||
|
||||
def cors(
|
||||
*,
|
||||
allow_headers: Iterable[str],
|
||||
allow_origins: Collection[str],
|
||||
expose_headers: Iterable[str],
|
||||
) -> Middleware:
|
||||
@middleware
|
||||
async def impl(request: Request, handler: Handler) -> StreamResponse:
|
||||
origin = request.headers.get("Origin")
|
||||
if not origin:
|
||||
return await handler(request)
|
||||
|
||||
if origin not in allow_origins:
|
||||
return web.Response(status=403, text="CORS origin is not allowed")
|
||||
|
||||
is_options = request.method == "OPTIONS"
|
||||
is_preflight = is_options and "Access-Control-Request-Method" in request.headers
|
||||
if is_preflight:
|
||||
resp = StreamResponse()
|
||||
else:
|
||||
resp = await handler(request)
|
||||
|
||||
resp.headers["Access-Control-Allow-Origin"] = origin
|
||||
if expose_headers:
|
||||
resp.headers["Access-Control-Expose-Headers"] = ", ".join(expose_headers)
|
||||
if is_options:
|
||||
resp.headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
|
||||
resp.headers["Access-Control-Allow-Methods"] = ", ".join(
|
||||
("OPTIONS", "POST")
|
||||
)
|
||||
|
||||
return resp
|
||||
|
||||
return impl
|
||||
Loading…
Add table
Add a link
Reference in a new issue