Skip to content

Commit df3c5ba

Browse files
committed
rate-limiting
1 parent c8bcbb9 commit df3c5ba

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ gunicorn = "*"
77
decorator = "*"
88
brotlipy = "*"
99
gevent = "*"
10+
redis = "*"
11+
hiredis = "*"
1012
Flask = "*"
13+
Flask-Limiter = "*"
1114

1215
[packages.raven]
1316
extras = [ "flask",]

Pipfile.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

httpbin/core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import argparse
1717

1818
from flask import Flask, Response, request, render_template, redirect, jsonify as flask_jsonify, make_response, url_for
19+
from flask_limiter import Limiter
20+
from flask_limiter.util import get_remote_address
1921
from six.moves import range as xrange
2022
from werkzeug.datastructures import WWWAuthenticate, MultiDict
2123
from werkzeug.http import http_date
@@ -57,6 +59,19 @@ def jsonify(*args, **kwargs):
5759
if 'SENTRY_DSN' in os.environ:
5860
sentry = Sentry(app, dsn=os.environ['SENTRY_DSN'])
5961

62+
# Setup rate-limiting.
63+
if 'REDIS_URL' in os.environ:
64+
app.config['RATELIMIT_STORAGE_URL'] = os.environ['REDIS_URL']
65+
app.config['RATELIMIT_HEADERS_ENABLED'] = True
66+
67+
limiter = Limiter(
68+
app,
69+
key_func=get_remote_address,
70+
global_limits=["100000 per day", "10000 per hour"]
71+
)
72+
73+
74+
6075

6176
# Set up Bugsnag exception tracking, if desired. To use Bugsnag, install the
6277
# Bugsnag Python client with the command "pip install bugsnag", and set the

0 commit comments

Comments
 (0)