예제 #1
0
def create_api_app(package_name, package_path, settings_override=None):
    app = create_app(package_name, settings_override)

    # Configure the database
    db.init_app(app)

    # Drop all uses a tip from http://piotr.banaszkiewicz.org/blog/2012/06/29/flask-sqlalchemy-init_app/
    with app.test_request_context():
        if app.config.get('DROP_ON_INIT') is True:
            db.drop_all()
        db.create_all()

    # Enable CORS. An API is unlikely to work without this.
    CORS(app)

    # Enable rate limiting.
    limiter = Limiter(app, key_func=get_remote_address)

    # Do not apply a limit to OPTIONS. If the OPTIONS pre-flight returns 429
    # then the browser displays a CORS error and the underlying API error is hidden
    # behind a 'fetch failed' message. Many thanks to tkrajca at
    # https://github.com/alisaifee/flask-limiter/issues/70
    limiter.request_filter(lambda: request.method.upper() == 'OPTIONS')

    return app, limiter
예제 #2
0
def register_limiter(app):
    '''
    访问频率限制
    :param app:
    :return:
    '''
    limiter = Limiter(app,
                      default_limits=['100/second'],
                      key_func=get_remote_address)
    limiter.request_filter(filter_func)