Skip to content

Commit b95c85d

Browse files
committed
Change flask_cors to be applied after blue print is initalized
1 parent 9b4d0d3 commit b95c85d

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/api/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
from flask import Flask, request, jsonify, url_for, Blueprint
55
from api.models import db, User
66
from api.utils import generate_sitemap, APIException
7+
from flask_cors import CORS
78

89
api = Blueprint('api', __name__)
910

11+
# Allow CORS requests to this API
12+
CORS(api)
13+
1014

1115
@api.route('/hello', methods=['POST', 'GET'])
1216
def handle_hello():
@@ -15,4 +19,4 @@ def handle_hello():
1519
"message": "Hello! I'm a message that came from the backend, check the network tab on the google inspector and you will see the GET request"
1620
}
1721

18-
return jsonify(response_body), 200
22+
return jsonify(response_body), 200

src/app.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,32 @@
55
from flask import Flask, request, jsonify, url_for, send_from_directory
66
from flask_migrate import Migrate
77
from flask_swagger import swagger
8-
from flask_cors import CORS
98
from api.utils import APIException, generate_sitemap
109
from api.models import db
1110
from api.routes import api
1211
from api.admin import setup_admin
1312
from api.commands import setup_commands
1413

15-
#from models import Person
14+
# from models import Person
1615

1716
ENV = "development" if os.getenv("FLASK_DEBUG") == "1" else "production"
18-
static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../public/')
17+
static_file_dir = os.path.join(os.path.dirname(
18+
os.path.realpath(__file__)), '../public/')
1919
app = Flask(__name__)
2020
app.url_map.strict_slashes = False
2121

2222
# database condiguration
2323
db_url = os.getenv("DATABASE_URL")
2424
if db_url is not None:
25-
app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace("postgres://", "postgresql://")
25+
app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace(
26+
"postgres://", "postgresql://")
2627
else:
2728
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////tmp/test.db"
2829

2930
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
30-
MIGRATE = Migrate(app, db, compare_type = True)
31+
MIGRATE = Migrate(app, db, compare_type=True)
3132
db.init_app(app)
3233

33-
# Allow CORS requests to this API
34-
CORS(app)
35-
3634
# add the admin
3735
setup_admin(app)
3836

@@ -43,24 +41,30 @@
4341
app.register_blueprint(api, url_prefix='/api')
4442

4543
# Handle/serialize errors like a JSON object
44+
45+
4646
@app.errorhandler(APIException)
4747
def handle_invalid_usage(error):
4848
return jsonify(error.to_dict()), error.status_code
4949

5050
# generate sitemap with all your endpoints
51+
52+
5153
@app.route('/')
5254
def sitemap():
5355
if ENV == "development":
5456
return generate_sitemap(app)
5557
return send_from_directory(static_file_dir, 'index.html')
5658

5759
# any other endpoint will try to serve it like a static file
60+
61+
5862
@app.route('/<path:path>', methods=['GET'])
5963
def serve_any_other_file(path):
6064
if not os.path.isfile(os.path.join(static_file_dir, path)):
6165
path = 'index.html'
6266
response = send_from_directory(static_file_dir, path)
63-
response.cache_control.max_age = 0 # avoid cache memory
67+
response.cache_control.max_age = 0 # avoid cache memory
6468
return response
6569

6670

0 commit comments

Comments
 (0)