Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@
bottle.TEMPLATE_PATH.append('/static/bookreader')
bottle.TEMPLATE_PATH.append('/static/bookreader/BookReader')


@app.hook('before_request')
def enforce_quota():
"""
Preventively reject over-quota/blacklisted requests to API endpoints
before they are processed, instead of only recording usage afterward.
"""
if bottle.request.path.startswith("/api/"):
quota_status = model.log_quota.check_quota(bottle.request)
if quota_status.get("status"):
bottle.abort(429, quota_status.get(
"error_message", "Rate limit exceeded."))

# Debug timing to improve speed
time_labels = []
time_nums = []
Expand Down