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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user