From ad087b954769a078d9d387bfd170f0407a260264 Mon Sep 17 00:00:00 2001 From: Matteo Rosati Date: Thu, 15 Jan 2026 15:19:36 +0100 Subject: [PATCH] add CORS headers --- app.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index fa3e2ee..6c7420b 100644 --- a/app.py +++ b/app.py @@ -150,7 +150,7 @@ def plext_to_dict(plext: Plext) -> dict: ], } -@app.route("/plexts/from-db", methods=["GET"]) +@app.route("/plexts/from-db", methods=["GET", "OPTIONS"]) @basic_auth_required def get_plexts_from_db(): """ @@ -164,6 +164,17 @@ def get_plexts_from_db(): Returns: JSON response with list of plexts (without _id field) """ + # CORS headers + cors_headers = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type, Authorization", + } + + # Handle OPTIONS preflight request + if request.method == "OPTIONS": + return "", 200, cors_headers + try: # Parse query parameters player_name = request.args.get("player_name") @@ -175,13 +186,13 @@ def get_plexts_from_db(): try: timestamp_from = int(timestamp_from) except ValueError: - return jsonify({"error": "timestamp_from must be an integer"}), 400 + return jsonify({"error": "timestamp_from must be an integer"}), 400, cors_headers if timestamp_to is not None: try: timestamp_to = int(timestamp_to) except ValueError: - return jsonify({"error": "timestamp_to must be an integer"}), 400 + return jsonify({"error": "timestamp_to must be an integer"}), 400, cors_headers # Build MongoDB filter query filter_query = {} @@ -222,17 +233,17 @@ def get_plexts_from_db(): return jsonify({ "count": len(plexts), "plexts": plexts - }) + }), 200, cors_headers except PyMongoError as e: logger.error(f"MongoDB error: {e}") - return jsonify({"error": "Database error"}), 500 + return jsonify({"error": "Database error"}), 500, cors_headers finally: client.close() except Exception as e: logger.exception("Unexpected error in get_plexts_from_db") - return jsonify({"error": "An error occurred"}), 500 + return jsonify({"error": "An error occurred"}), 500, cors_headers @app.route("/plexts/from-api", methods=["GET"]) @basic_auth_required