Esempio n. 1
0
def load_config(app, kwargs_config):
    """
    writes to app.config heiracharchly based on files on disk and consul
    :param app: flask.Flask application instance
    :param kwargs_config: dictionary to update the config
    :return: None
    """

    try:
        app.config.from_object('adsws.config')
    except (IOError, ImportError):
        app.logger.warning("Could not load object adsws.config")
    try:
        app.config.from_object('%s.config' % app.name)
    except (IOError, ImportError):
        app.logger.warning("Could not load object {}.config".format(app.name))

    try:
        f = os.path.join(app.instance_path, 'config.py')
        app.config.from_pyfile(f)
    except IOError:
        app.logger.warning("Could not load {}".format(f))

    try:
        f = os.path.join(app.instance_path, 'local_config.py')
        app.config.from_pyfile(f)
    except IOError:
        app.logger.warning("Could not load {}".format(f))

    try:
        consul = Consul(app)
        consul.apply_remote_config()
    except ConsulConnectionError:
        app.logger.warning(
            "Could not load config from consul at {}".format(
                os.environ.get('CONSUL_HOST', 'localhost')
            )
        )

    if kwargs_config:
        app.config.update(kwargs_config)

    # old baggage... Consul used to store keys in hexadecimal form
    # so the production/staging databases both convert that into raw bytes
    # but those raw bytes were non-ascii chars (unsafe to pass through
    # env vars). So we must continue converting hex ...        
    if app.config.get('SECRET_KEY', None):
        try:
            app.config['SECRET_KEY'] = app.config['SECRET_KEY'].decode('hex')
            app.logger.warning('Converted SECRET_KEY from hex format into bytes')
        except TypeError:
            app.logger.warning('Most likely the SECRET_KEY is not in hex format')
Esempio n. 2
0
def create_app():
    """
    Create the application and return it to the user
    :return: flask.Flask application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    Consul(app)

    load_config(app)

    logging.config.dictConfig(
        app.config['GRAPHICS_LOGGING']
    )

    api = Api(app)
    api.add_resource(Graphics, '/<string:bibcode>')

    db.init_app(app)

    Discoverer(app)

    return app
Esempio n. 3
0
def create_app():
    """
    Create the application and return it to the user
    :return: flask.Flask application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    Consul(app)

    load_config(app)

    logging.config.dictConfig(
        app.config['OBJECTS_LOGGING']
    )

    app.cache = Cache(app) 

    api = Api(app)
    api.add_resource(ObjectSearch, '/', '/<string:objects>', '/<string:objects>/<string:source>')
    api.add_resource(PositionSearch, '/pos/<string:pstring>')
    api.add_resource(QuerySearch, '/query')

    discoverer = Discoverer(app)

    return app
Esempio n. 4
0
def create_app():
    """
    Create the application and return it to the user

    :return: flask.Flask application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    # Load config and logging
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    logging.config.dictConfig(
        app.config['SAMPLE_APPLICATION_LOGGING']
    )

    # Register extensions
    api = Api(app)
    Discoverer(app)
    db.init_app(app)

    api.add_resource(UnixTime, '/time')
    api.add_resource(PrintArg, '/print/<string:arg>')
    api.add_resource(ExampleApiUsage, '/search')

    return app
Esempio n. 5
0
def create_app():
    """
    Create the application and return it to the user
    :return: application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    # Load config and logging
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    logging.config.dictConfig(
        app.config['HARBOUR_LOGGING']
    )

    load_s3(app)

    # Register extensions
    watchman = Watchman(app, version=dict(scopes=['']))
    api = Api(app)
    Discoverer(app)
    db.init_app(app)

    # Add the end resource end points
    api.add_resource(AuthenticateUserClassic, '/auth/classic', methods=['POST'])
    api.add_resource(AuthenticateUserTwoPointOh, '/auth/twopointoh', methods=['POST'])

    api.add_resource(
        ClassicLibraries,
        '/libraries/classic/<int:uid>',
        methods=['GET']
    )
    api.add_resource(
        TwoPointOhLibraries,
        '/libraries/twopointoh/<int:uid>',
        methods=['GET']
    )

    api.add_resource(
        ExportTwoPointOhLibraries,
        '/export/twopointoh/<export>',
        methods=['GET']
    )

    api.add_resource(ClassicUser, '/user', methods=['GET'])
    api.add_resource(AllowedMirrors, '/mirrors', methods=['GET'])

    return app
Esempio n. 6
0
def create_app():
    """Application factory"""

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    Consul(app)  # load_config expects consul to be registered
    load_config(app)

    Discoverer(app)
    api = Api(app)
    api.add_resource(WordCloud, '/word-cloud')
    api.add_resource(AuthorNetwork, '/author-network')
    api.add_resource(PaperNetwork, '/paper-network')
    return app
Esempio n. 7
0
def create_app():
    """
    Create the application and return it to the user
    :return: application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    # Load config and logging
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    logging.config.dictConfig(app.config['BIBLIB_LOGGING'])

    # Register extensions
    api = Api(app)
    Discoverer(app)
    db.init_app(app)

    # Add the end resource end points
    api.add_resource(UserView, '/libraries', methods=['GET', 'POST'])

    api.add_resource(LibraryView,
                     '/libraries/<string:library>',
                     methods=['GET'])

    api.add_resource(DocumentView,
                     '/documents/<string:library>',
                     methods=['POST', 'DELETE', 'PUT'])

    api.add_resource(PermissionView,
                     '/permissions/<string:library>',
                     methods=['GET', 'POST'])

    api.add_resource(TransferView,
                     '/transfer/<string:library>',
                     methods=['POST'])

    api.add_resource(ClassicView, '/classic', methods=['GET'])

    api.add_resource(TwoPointOhView, '/twopointoh', methods=['GET'])

    return app
Esempio n. 8
0
def create_app():
    """
    Application factory
    :return configured flask.Flask application instance
    """
    app = Flask(__name__, static_folder=None)

    app.url_map.strict_slashes = False
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    logging.config.dictConfig(app.config['SOLR_SERVICE_LOGGING'])

    api = Api(app)

    @api.representation('application/json')
    def json(data, code, headers):
        """
        Since we force SOLR to always return JSON, it is faster to
        return JSON as text string directly, without parsing and serializing
        it multiple times
        """
        if not isinstance(data, basestring):
            resp = jsonify(data)
            resp.status_code = code
        else:
            resp = make_response(data, code)
        resp.headers['Content-Type'] = 'application/json'
        resp.headers['Server'] = 'Solr Microservice {v}'.format(
            v=app.config.get('SOLR_SERVICE_VERSION'))
        if code == 200:
            resp.headers['Cache-Control'] = app.config.get(
                'SOLR_CACHE_CONTROL', "public, max-age=600")
        return resp

    api.add_resource(StatusView, '/status')
    api.add_resource(Tvrh, '/tvrh')
    api.add_resource(Search, '/query')
    api.add_resource(Qtree, '/qtree')
    api.add_resource(BigQuery, '/bigquery')

    Discoverer(app)
    return app
Esempio n. 9
0
def create_app():
    """
    Create the application and return it to the user
    :return: flask.Flask application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    Consul(app)

    load_config(app)

    api = Api(app)
    api.add_resource(Metrics, '/')
    api.add_resource(PubMetrics, '/<string:bibcode>')

    db.init_app(app)

    discoverer = Discoverer(app)

    return app
Esempio n. 10
0
def create_app(config=None):
    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    Discoverer(app)
    Consul(app)  # load_config expects consul to be registered
    load_config(app, config)
    db.init_app(app)
    logging.config.dictConfig(
        app.config['ORCID_LOGGING']
    )
    
    ## pysqlite driver breaks transactions, we have to apply some hacks as per
    ## http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#pysqlite-serializable
    
    if 'sqlite' in (app.config.get('SQLALCHEMY_BINDS') or {'orcid':''})['orcid']:
        from sqlalchemy import event
        
        binds = app.config.get('SQLALCHEMY_BINDS')
        if binds and 'orcid' in binds:
            engine = db.get_engine(app, bind=(app.config.get('SQLALCHEMY_BINDS') and 'orcid'))
        else:
            engine = db.get_engine(app)
        
        @event.listens_for(engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None

        @event.listens_for(engine, "begin")
        def do_begin(conn):
            # emit our own BEGIN
            conn.execute("BEGIN")
    
    app.register_blueprint(bp)
    return app
Esempio n. 11
0
def create_app():
    """
    Create the application and return it to the user

    :return: flask.Flask application
    """

    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False

    # Load config and logging
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    logging.config.dictConfig(app.config['EXPORT_SERVICE_LOGGING'])

    # Register extensions
    api = Api(app)
    Discoverer(app)

    api.add_resource(Aastex, '/aastex')
    api.add_resource(Bibtex, '/bibtex')
    api.add_resource(Endnote, '/endnote')

    return app
Esempio n. 12
0
def create_app(**config):
    """
    Application factory
    :return configured flask.Flask application instance
    """
    app = Flask(__name__, static_folder=None)

    app.url_map.strict_slashes = False
    Consul(app)  # load_config expects consul to be registered
    load_config(app)
    if config:
        app.config.update(config)
        
    db.init_app(app)
    logging.config.dictConfig(
        app.config['SOLR_SERVICE_LOGGING']
    )
    
    ## pysqlite driver breaks transactions, we have to apply some hacks as per
    ## http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#pysqlite-serializable
    
    if 'sqlite' in (app.config.get('SQLALCHEMY_BINDS') or {'solr_service':''})['solr_service']:
        from sqlalchemy import event
        
        binds = app.config.get('SQLALCHEMY_BINDS')
        if binds and 'solr_service' in binds:
            engine = db.get_engine(app, bind=(app.config.get('SQLALCHEMY_BINDS') and 'solr_service'))
        else:
            engine = db.get_engine(app)
        
        @event.listens_for(engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None

        @event.listens_for(engine, "begin")
        def do_begin(conn):
            # emit our own BEGIN
            conn.execute("BEGIN")
    
    api = Api(app)

    @api.representation('application/json')
    def json(data, code, headers):
        """
        Since we force SOLR to always return JSON, it is faster to
        return JSON as text string directly, without parsing and serializing
        it multiple times
        """
        if not isinstance(data, basestring):
            resp = jsonify(data)
            resp.status_code = code
        else:
            resp = make_response(data, code)
        resp.headers['Content-Type'] = 'application/json'
        resp.headers['Server'] = 'Solr Microservice {v}'.format(
            v=app.config.get('SOLR_SERVICE_VERSION')
        )
        if code == 200:
            resp.headers['Cache-Control'] = app.config.get('SOLR_CACHE_CONTROL', "public, max-age=600")
        return resp

    api.add_resource(StatusView, '/status')
    api.add_resource(Tvrh, '/tvrh')
    api.add_resource(Search, '/query')
    api.add_resource(Qtree, '/qtree')
    api.add_resource(BigQuery, '/bigquery')

    Discoverer(app)
    return app
Esempio n. 13
0
def create_app(**config):
    
    opath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    if opath not in sys.path:
        sys.path.insert(0, opath)
    
    app = Flask(__name__, static_folder=None)
    app.url_map.strict_slashes = False
    
    
    app.config.from_pyfile('config.py')
    
    # Load config from Consul
    Consul(app)  # load_config expects consul to be registered
    try:
        app.extensions['consul'].apply_remote_config()
    except ConsulConnectionError as error:
        app.logger.warning('Could not apply config from consul: {}'
                           .format(error))
    
    # and finally from the local_config.py
    try:
      app.config.from_pyfile('local_config.py')
    except IOError:
      pass
  
    if config:
        app.config.update(config)
    
    db.init_app(app)
    
    ## pysqlite driver breaks transactions, we have to apply some hacks as per
    ## http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#pysqlite-serializable
    
    if 'sqlite' in (app.config.get('SQLALCHEMY_BINDS') or {'myads':''})['myads']:
        from sqlalchemy import event
        
        binds = app.config.get('SQLALCHEMY_BINDS')
        if binds and 'myads' in binds:
            engine = db.get_engine(app, bind=(app.config.get('SQLALCHEMY_BINDS') and 'myads'))
        else:
            engine = db.get_engine(app)
        
        @event.listens_for(engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None

        @event.listens_for(engine, "begin")
        def do_begin(conn):
            # emit our own BEGIN
            conn.execute("BEGIN")
        
    
    # Note about imports being here rather than at the top level
    # I want to enclose the import into the scope of the create_app()
    # and not advertise any of the views
    from myads_service import views
    for o in inspect.getmembers(views, predicate=lambda x: inspect.ismodule(x)):
        for blueprint in inspect.getmembers(o[1], predicate=lambda x: isinstance(x, Blueprint)):
            app.register_blueprint(blueprint[1])

    discoverer = Discoverer(app)        
    return app