Skip to content

Commit

Permalink
Added CORS * support to API
Browse files Browse the repository at this point in the history
  • Loading branch information
inflector committed Dec 11, 2017
1 parent 17849a8 commit a971939
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions agent/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sphinx_rtd_theme
recommonmark

aiohttp-jinja2
aiohttp-cors
bson

tensorflow<1.4.0
Expand Down
30 changes: 28 additions & 2 deletions agent/sn_agent/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import aiohttp_cors

from aiohttp import web, WSMsgType
from aiohttp.web_response import Response
Expand Down Expand Up @@ -98,6 +99,31 @@ async def on_shutdown(app):

def setup_api(app):
app['sockets'] = []
app.router.add_post('/api', http_handler)
app.router.add_get('/api/ws', ws_handler)
# app.router.add_post('/api', http_handler)
# app.router.add_get('/api/ws', ws_handler)

# Setup CORS - cross domain support
cors = aiohttp_cors.setup(app)
resource = cors.add(app.router.add_resource("/api"))
cors.add(
resource.add_route("POST", http_handler), {
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers=("X-Custom-Server-Header",),
allow_headers=("X-Requested-With", "Content-Type"),
max_age=3600,
)
})

resource = cors.add(app.router.add_resource("/api/ws"))
cors.add(
resource.add_route("GET", http_handler), {
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers=("X-Custom-Server-Header",),
allow_headers=("X-Requested-With", "Content-Type"),
max_age=3600,
)
})

app.on_shutdown.append(on_shutdown)

0 comments on commit a971939

Please sign in to comment.