From 50f0b64182b7f0c83ca5881ce00bbdca9277caba Mon Sep 17 00:00:00 2001 From: Blallo Date: Tue, 6 Aug 2019 00:07:40 +0200 Subject: [PATCH] CORS in dev_server. --- api/dev_server.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/api/dev_server.py b/api/dev_server.py index ea7d237..290aa96 100644 --- a/api/dev_server.py +++ b/api/dev_server.py @@ -15,34 +15,46 @@ routes = web.RouteTableDef() @routes.get("/{tail:.*}") async def get_handler(request: web.Request) -> web.Response: - data = await request.get() - alog.info("GET -> [%s]: %s", request.path, data) - return web.json_response({"method": request.method, "path": request.path}) + alog.info("GET -> [%s]: %s", request.path, request.query) + return web.json_response( + {"method": request.method, "path": request.path}, + headers={"Access-Control-Allow-Origin": "*"}, + ) @routes.post("/{tail:.*}") async def post_handler(request: web.Request) -> web.Response: data = await request.post() alog.info("POST -> [%s]: %s", request.path, data) - return web.json_response({"method": request.method, "path": request.path}) + return web.json_response( + {"method": request.method, "path": request.path}, + headers={"Access-Control-Allow-Origin": "*"}, + ) @routes.put("/{tail:.*}") async def put_handler(request: web.Request) -> web.Response: - data = await request.put() + data = await request.post() alog.info("PUT -> [%s]: %s", request.path, data) - return web.json_response({"method": request.method, "path": request.path}) + return web.json_response( + {"method": request.method, "path": request.path}, + headers={"Access-Control-Allow-Origin": "*"}, + ) @routes.delete("/{tail:.*}") async def delete_handler(request: web.Request) -> web.Response: - alog.info("DELETE -> [%s]", request.path) - return web.json_response({"method": request.method, "path": request.path}) + data = await request.post() + alog.info("DELETE -> [%s]: %s", request.path, data) + return web.json_response( + {"method": request.method, "path": request.path}, + headers={"Access-Control-Allow-Origin": "*"}, + ) async def options_handler(request: web.Request) -> web.Response: alog.info("OPTIONS -> [%s]", request.path) - return web.Response(status=200, headers={"Access-Control-Allow-Origin": "*"}) + return web.Response(status=200) def run(address: str, port: int) -> None: