Exemplo n.º 1
0
def make_limiter(app):
    def limiter_key_func():
        from flask_login import current_user

        if hasattr(current_user, "id"):
            return current_user.id
        return get_remote_address()

    limiter = Limiter(
        app,
        key_func=limiter_key_func,
        default_limits=["60 per minute"],
        default_limits_per_method=True,
    )
    limiter.enabled = QuerybookSettings.PRODUCTION
    for handler in app.logger.handlers:
        limiter.logger.addHandler(handler)

    @app.after_request
    def limiter_add_headers(response):
        if limiter.enabled and limiter.current_limit and limiter.current_limit.breached:
            response.headers[
                "flask-limit-amount"] = limiter.current_limit.limit.amount
            response.headers["flask-limit-key"] = limiter.current_limit.key
            response.headers[
                "flask-limit-reset-at"] = limiter.current_limit.reset_at
            response.headers[
                "flask-limit-window-size"] = limiter.current_limit.limit.get_expiry(
                )
        return response

    return limiter
def make_limiter(app):
    limiter = Limiter(
        app,
        key_func=lambda: current_user.id
        if hasattr(current_user, "id")
        else get_remote_address(),
        default_limits=["1000 per day", "30 per minute"],
    )
    limiter.enabled = QuerybookSettings.PRODUCTION
    for handler in app.logger.handlers:
        limiter.logger.addHandler(handler)
    return limiter
Exemplo n.º 3
0
# pylint: disable=invalid-name
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = JWT_SECRET_KEY
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = JWT_ACCESS_TOKEN_EXPIRES
jwt = JWTManager(app)

VERSION = "v0.1"

limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["2 per minute", "1 per second"],
)

if FLASK_DEBUG:
    limiter.enabled = False


@app.route("/")
@conditional_decorator(jwt_required, USE_LOGIN)
def hello():
    """
    Return "Hello World" as a default for the "/" route
    """
    return "Hello World"


@app.route("/version")
@conditional_decorator(jwt_required, USE_LOGIN)
def get_version():
    """