From 0eea5f4b1b382744f2da3bfc3440140865afe40e Mon Sep 17 00:00:00 2001 From: Evgenii Akentev Date: Sat, 18 May 2019 22:46:24 +0300 Subject: [PATCH] add cors --- app.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index b288490..7e8ee1c 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ from random import shuffle, random -from bottle import get, post, run, request, static_file +from bottle import get, post, run, request, static_file, response import uuid from geo import distance, move_coordinate from street_predictor import parse_summary @@ -101,8 +101,23 @@ def create_test_game(): games = {'test': create_test_game()} +# the decorator +def enable_cors(fn): + def _enable_cors(*args, **kwargs): + # set CORS headers + response.headers['Access-Control-Allow-Origin'] = '*' + response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' + response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' + + if request.method != 'OPTIONS': + # actual request; reply with the actual response + return fn(*args, **kwargs) + + return _enable_cors + @get('/api/games') +@enable_cors def get_games(): return { "games": [{"game_id": g.id, "coordinates": g.current_coordinates if g.is_finished else None} for g in @@ -111,6 +126,7 @@ def get_games(): @post('/api/games') +@enable_cors def post_game(): game_id = str(uuid.uuid4()) @@ -130,6 +146,7 @@ def post_game(): @get('/api/games/') +@enable_cors def get_game(game_id): game = games[game_id] @@ -139,6 +156,7 @@ def get_game(game_id): @get('/api/games//tips') +@enable_cors def get_game(game_id): game = games[game_id] @@ -146,6 +164,7 @@ def get_game(game_id): @post('/api/games//ask-tip') +@enable_cors def get_game(game_id): game = games[game_id] @@ -154,6 +173,7 @@ def get_game(game_id): @post('/api/games//move') +@enable_cors def get_game(game_id): game = games[game_id] @@ -173,6 +193,7 @@ def get_game(game_id): @post('/api/games//finish') +@enable_cors def finish_game(game_id): game = games[game_id] @@ -198,22 +219,27 @@ def finish_game(game_id): } @get('/') +@enable_cors def index(): return static_file('index.html', "front/build") @get('/static/css/') +@enable_cors def static_css(staticFile): return static_file(staticFile, "front/build/static/css") @get('/static/js/') +@enable_cors def static_js(staticFile): return static_file(staticFile, "front/build/static/js") @get('/static/media/') +@enable_cors def static_media(staticFile): return static_file(staticFile, "front/build/static/media") @get('/') +@enable_cors def index(whatever): return static_file('index.html', "front/build") -- 2.50.1