From 6952de98cf6b27e4e9b4ae10a181c3261f8d6d96 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 28 Feb 2019 17:22:26 +0100 Subject: [PATCH] Add CORS headers to API responses --- src/api_main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/api_main.py b/src/api_main.py index 7fe9484c..bdb18cea 100644 --- a/src/api_main.py +++ b/src/api_main.py @@ -169,6 +169,11 @@ async def get_stats(request: web.Request, conn): "messages": message_count }) +@web.middleware +async def add_cors_headers(request, handler): + resp = await handler(request) + resp.headers["Access-Control-Allow-Origin"] = "*"; + return resp @web.middleware async def render_pk_errors(request, handler): @@ -178,7 +183,7 @@ async def render_pk_errors(request, handler): raise web.HTTPBadRequest(body=e.message) -app = web.Application(middlewares=[render_pk_errors]) +app = web.Application(middlewares=[render_pk_errors, add_cors_headers]) app.add_routes([ web.get("/systems/{id}", get_system), web.get("/systems/{id}/switches", get_switches),