CORS in dev_server.

This commit is contained in:
sfigato 2019-08-06 00:07:40 +02:00 committed by blallo
parent 581af133ea
commit 50f0b64182
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -15,34 +15,46 @@ routes = web.RouteTableDef()
@routes.get("/{tail:.*}") @routes.get("/{tail:.*}")
async def get_handler(request: web.Request) -> web.Response: async def get_handler(request: web.Request) -> web.Response:
data = await request.get() alog.info("GET -> [%s]: %s", request.path, request.query)
alog.info("GET -> [%s]: %s", request.path, data) return web.json_response(
return web.json_response({"method": request.method, "path": request.path}) {"method": request.method, "path": request.path},
headers={"Access-Control-Allow-Origin": "*"},
)
@routes.post("/{tail:.*}") @routes.post("/{tail:.*}")
async def post_handler(request: web.Request) -> web.Response: async def post_handler(request: web.Request) -> web.Response:
data = await request.post() data = await request.post()
alog.info("POST -> [%s]: %s", request.path, data) 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:.*}") @routes.put("/{tail:.*}")
async def put_handler(request: web.Request) -> web.Response: 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) 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:.*}") @routes.delete("/{tail:.*}")
async def delete_handler(request: web.Request) -> web.Response: async def delete_handler(request: web.Request) -> web.Response:
alog.info("DELETE -> [%s]", request.path) data = await request.post()
return web.json_response({"method": request.method, "path": request.path}) 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: async def options_handler(request: web.Request) -> web.Response:
alog.info("OPTIONS -> [%s]", request.path) 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: def run(address: str, port: int) -> None: