add CORS headers
This commit is contained in:
23
app.py
23
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
|
@basic_auth_required
|
||||||
def get_plexts_from_db():
|
def get_plexts_from_db():
|
||||||
"""
|
"""
|
||||||
@@ -164,6 +164,17 @@ def get_plexts_from_db():
|
|||||||
Returns:
|
Returns:
|
||||||
JSON response with list of plexts (without _id field)
|
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:
|
try:
|
||||||
# Parse query parameters
|
# Parse query parameters
|
||||||
player_name = request.args.get("player_name")
|
player_name = request.args.get("player_name")
|
||||||
@@ -175,13 +186,13 @@ def get_plexts_from_db():
|
|||||||
try:
|
try:
|
||||||
timestamp_from = int(timestamp_from)
|
timestamp_from = int(timestamp_from)
|
||||||
except ValueError:
|
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:
|
if timestamp_to is not None:
|
||||||
try:
|
try:
|
||||||
timestamp_to = int(timestamp_to)
|
timestamp_to = int(timestamp_to)
|
||||||
except ValueError:
|
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
|
# Build MongoDB filter query
|
||||||
filter_query = {}
|
filter_query = {}
|
||||||
@@ -222,17 +233,17 @@ def get_plexts_from_db():
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
"count": len(plexts),
|
"count": len(plexts),
|
||||||
"plexts": plexts
|
"plexts": plexts
|
||||||
})
|
}), 200, cors_headers
|
||||||
|
|
||||||
except PyMongoError as e:
|
except PyMongoError as e:
|
||||||
logger.error(f"MongoDB error: {e}")
|
logger.error(f"MongoDB error: {e}")
|
||||||
return jsonify({"error": "Database error"}), 500
|
return jsonify({"error": "Database error"}), 500, cors_headers
|
||||||
finally:
|
finally:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Unexpected error in get_plexts_from_db")
|
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"])
|
@app.route("/plexts/from-api", methods=["GET"])
|
||||||
@basic_auth_required
|
@basic_auth_required
|
||||||
|
|||||||
Reference in New Issue
Block a user