use .env in app.py
This commit is contained in:
32
app.py
32
app.py
@@ -8,6 +8,9 @@ from ingress import IngressAPI
|
||||
from models import EventType, Plext
|
||||
from pymongo import MongoClient
|
||||
from pymongo.errors import PyMongoError
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Timezone configuration
|
||||
TIMEZONE = ZoneInfo("Europe/Rome")
|
||||
@@ -49,6 +52,7 @@ def basic_auth_required(f):
|
||||
Returns:
|
||||
401 Unauthorized if authentication fails or is missing
|
||||
"""
|
||||
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
auth = request.authorization
|
||||
@@ -58,7 +62,7 @@ def basic_auth_required(f):
|
||||
"Could not verify your access level for that URL.\n"
|
||||
"You have to login with proper credentials",
|
||||
401,
|
||||
{"WWW-Authenticate": 'Basic realm="Login Required"'}
|
||||
{"WWW-Authenticate": 'Basic realm="Login Required"'},
|
||||
)
|
||||
|
||||
return f(*args, **kwargs)
|
||||
@@ -150,6 +154,7 @@ def plext_to_dict(plext: Plext) -> dict:
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
||||
@basic_auth_required
|
||||
def get_plexts_from_db():
|
||||
@@ -186,13 +191,21 @@ def get_plexts_from_db():
|
||||
try:
|
||||
timestamp_from = int(timestamp_from)
|
||||
except ValueError:
|
||||
return jsonify({"error": "timestamp_from must be an integer"}), 400, cors_headers
|
||||
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, cors_headers
|
||||
return (
|
||||
jsonify({"error": "timestamp_to must be an integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
|
||||
# Build MongoDB filter query
|
||||
filter_query = {}
|
||||
@@ -222,18 +235,14 @@ def get_plexts_from_db():
|
||||
projection = {"_id": 0}
|
||||
|
||||
# Execute query with sorting (timestamp DESC - most recent first)
|
||||
cursor = collection.find(
|
||||
filter=filter_query,
|
||||
projection=projection
|
||||
).sort("timestamp", -1)
|
||||
cursor = collection.find(filter=filter_query, projection=projection).sort(
|
||||
"timestamp", -1
|
||||
)
|
||||
|
||||
# Convert cursor to list
|
||||
plexts = list(cursor)
|
||||
|
||||
return jsonify({
|
||||
"count": len(plexts),
|
||||
"plexts": plexts
|
||||
}), 200, cors_headers
|
||||
return jsonify({"count": len(plexts), "plexts": plexts}), 200, cors_headers
|
||||
|
||||
except PyMongoError as e:
|
||||
logger.error(f"MongoDB error: {e}")
|
||||
@@ -245,6 +254,7 @@ def get_plexts_from_db():
|
||||
logger.exception("Unexpected error in get_plexts_from_db")
|
||||
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
||||
|
||||
|
||||
@app.route("/plexts/from-api", methods=["GET"])
|
||||
@basic_auth_required
|
||||
def get_plexts_from_api():
|
||||
|
||||
Reference in New Issue
Block a user