From b95c85dc628b5e2ae00e7d18d41c6273dbaae509 Mon Sep 17 00:00:00 2001 From: Erwin Aguero Date: Thu, 9 Nov 2023 08:45:47 +0000 Subject: [PATCH] Change flask_cors to be applied after blue print is initalized --- src/api/routes.py | 6 +++++- src/app.py | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/api/routes.py b/src/api/routes.py index 3b2dc016c1..029589a3a1 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -4,9 +4,13 @@ from flask import Flask, request, jsonify, url_for, Blueprint from api.models import db, User from api.utils import generate_sitemap, APIException +from flask_cors import CORS api = Blueprint('api', __name__) +# Allow CORS requests to this API +CORS(api) + @api.route('/hello', methods=['POST', 'GET']) def handle_hello(): @@ -15,4 +19,4 @@ def handle_hello(): "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" } - return jsonify(response_body), 200 \ No newline at end of file + return jsonify(response_body), 200 diff --git a/src/app.py b/src/app.py index dd16213ea7..a723a047a0 100644 --- a/src/app.py +++ b/src/app.py @@ -5,34 +5,32 @@ from flask import Flask, request, jsonify, url_for, send_from_directory from flask_migrate import Migrate from flask_swagger import swagger -from flask_cors import CORS from api.utils import APIException, generate_sitemap from api.models import db from api.routes import api from api.admin import setup_admin from api.commands import setup_commands -#from models import Person +# from models import Person ENV = "development" if os.getenv("FLASK_DEBUG") == "1" else "production" -static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../public/') +static_file_dir = os.path.join(os.path.dirname( + os.path.realpath(__file__)), '../public/') app = Flask(__name__) app.url_map.strict_slashes = False # database condiguration db_url = os.getenv("DATABASE_URL") if db_url is not None: - app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace("postgres://", "postgresql://") + app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace( + "postgres://", "postgresql://") else: app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////tmp/test.db" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False -MIGRATE = Migrate(app, db, compare_type = True) +MIGRATE = Migrate(app, db, compare_type=True) db.init_app(app) -# Allow CORS requests to this API -CORS(app) - # add the admin setup_admin(app) @@ -43,11 +41,15 @@ app.register_blueprint(api, url_prefix='/api') # Handle/serialize errors like a JSON object + + @app.errorhandler(APIException) def handle_invalid_usage(error): return jsonify(error.to_dict()), error.status_code # generate sitemap with all your endpoints + + @app.route('/') def sitemap(): if ENV == "development": @@ -55,12 +57,14 @@ def sitemap(): return send_from_directory(static_file_dir, 'index.html') # any other endpoint will try to serve it like a static file + + @app.route('/', methods=['GET']) def serve_any_other_file(path): if not os.path.isfile(os.path.join(static_file_dir, path)): path = 'index.html' response = send_from_directory(static_file_dir, path) - response.cache_control.max_age = 0 # avoid cache memory + response.cache_control.max_age = 0 # avoid cache memory return response