Skip to content

Commit

Permalink
Add basic rate limiting
Browse files Browse the repository at this point in the history
Closes #100
  • Loading branch information
laraross authored and suchow committed Sep 28, 2015
1 parent 0526818 commit 617739a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Web app that serves proselint's API."""

from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS, cross_origin
from flask_limiter import Limiter
import uuid
import os
import urllib2
Expand All @@ -13,6 +14,7 @@
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = "Origin, X-Requested-With, Content-Type, Accept"
limiter = Limiter(app, global_limits=["600 per hour"])

q = Queue(connection=conn)

Expand All @@ -27,7 +29,15 @@ def worker_function(text):
return command_line.lint(filename)


@app.errorhandler(429)
def ratelimit_handler(e):
return make_response(
jsonify(status="error", message="Rate limit exceeded."), 429
)


@app.route('/v1/', methods=['GET', 'POST'])
@limiter.limit("60 per minute")
@cross_origin() # allow all origins all methods.
def lint():
"""Run linter on the provided text and return the results."""
Expand Down

0 comments on commit 617739a

Please sign in to comment.