"""Application. The API application is a `flask` application. It provides simple features such as registering a url for a specific handlers. """ import base64 import connector_neo4j from flask import Flask from owslogger import flask_logger from owsrequest import flask_request from snowflake_connector.snowflake_conn import set_default_sessionmaker from sound_recordings import config app = Flask(config.SERVICE_NAME) flask_logger.setup( app, config.ENVIRONMENT, config.LOGGER_NAME, config.LOGGER_LEVEL, config.SERVICE_NAME, config.SERVICE_VERSION, exclude_paths=[config.HEALTH_CHECK], ) try: import uwsgi # noqa uwsgiRunning = True except ImportError: uwsgiRunning = False flask_request.setup( app, config.ENVIRONMENT, add_request_context=True, label_profile=False, verify_access=True, rules_file='sound_recordings/access_rules.yml', access_log_only=config.ONLY_LOG_ACCESS_ERRORS, uwsgi_cache_enabled=uwsgiRunning, exclude_paths=config.EXCLUDE_PATH) if config.ENABLE_FLASGGER: from flasgger import Swagger app.config['SWAGGER'] = { 'title': 'ows-sound-recordings Documentation', 'uiversion': 2, } try: Swagger(app, template_file=config.SWAGGER_FILE_PATH) except FileNotFoundError: print('Could not find swagger file at {}'.format(config.SWAGGER_FILE_PATH)) elif config.ENVIRONMENT in [config.PROD_ENVIRONMENT]: if not config.SENTRY: raise Exception(f'Sentry is not configured in {config.ENVIRONMENT}') neo4j_username = config.NEO4J_USERNAME neo4j_password = config.NEO4J_PASSWORD neo4j_url = config.NEO4J_URL connector_neo4j.configure( neo4j_url, neo4j_username, neo4j_password, None, max_connection_pool_size=config.NEO4J_MAX_CONN_POOLSIZE ) sf_config = { 'role': config.SNOWFLAKE_ROLE, 'account': config.SNOWFLAKE_ACCOUNT, 'user': config.SNOWFLAKE_USER, 'password': 'dummy_password', 'database': config.SNOWFLAKE_DATABASE, 'schema': config.SNOWFLAKE_SCHEMA, 'warehouse': config.SNOWFLAKE_WAREHOUSE, 'client_session_keep_alive': True } if config.SNOWFLAKE_KEY: config.SNOWFLAKE_CONNECT_ARGS = {'private_key': base64.b64decode(bytes(config.SNOWFLAKE_KEY, encoding='utf-8'))} # noqa:E501 # service-wide Snowflake connection pool settings set_default_sessionmaker( pool_size=config.SNOWFLAKE_POOL_SIZE, pool_recycle=config.SNOWFLAKE_POOL_RECYCLE, pool_pre_ping=config.SNOWFLAKE_POOL_PRE_PING, pool_reset_on_return=config.SNOWFLAKE_POOL_RESET_ON_RETURN, max_overflow=config.SNOWFLAKE_POOL_MAX_OVERFLOW, connect_args=config.SNOWFLAKE_CONNECT_ARGS, sf_config=sf_config )