示例#1
0
文件: config.py 项目: CoCongV/Blog
    def init_app(cls, app):
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()

        mail_handler = SMTPHandler(mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
                                   fromaddr=cls.FLASK_MAIL_SENDER,
                                   toaddrs=[cls.FLASK_ADMIN],
                                   subject=cls.FLASK_MAIL_SUBJECT_PREFIX +
                                   ' Application Error',
                                   credentials=credentials,
                                   secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
        logfile = os.path.join(cls.LOG_PATH, cls.LOG_NAME)
        file_handler = TimedRotatingFileHandler(logfile,
                                                when=cls.LOG_TIME,
                                                backupCount=cls.LOG_BACK_COUNT)
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)
        sentry = Sentry()
        sentry.init_app(app,
                        dsn=cls.SENTRY_DSN,
                        logging=True,
                        level=logging.ERROR)
示例#2
0
def create_app():
    app = Flask(__name__)
    app.debug = os.getenv('DEBUG') == 'True'
    if os.getenv('SENTRY_DSN'):
        sentry = Sentry()
        sentry.init_app(app)
    return app
def init_app(application):
    """Initialize the main app with config information and routes."""
    # Import local modules here to avoid circular dependencies.
    from {{cookiecutter.project_module}} import errorhandlers, resources
    from {{cookiecutter.project_module}}.settings import current_config

    application.config.from_object(current_config())

    # Configure logging
    logging.config.dictConfig(application.config["LOGGING"])

    # Configure Sentry
    if application.config["SENTRY_DSN"]:
        sentry = Sentry(
            dsn=application.config["SENTRY_DSN"],
            logging=True,
            level=logging.ERROR,
        )
        sentry.init_app(application)

    # Add routes and resources.
    resources.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Register error handlers
    errorhandlers.init_app(application)

    # Please keep in mind that it is a security issue to use such a middleware
    # in a non-proxy setup because it will blindly trust the incoming headers
    # which might be forged by malicious clients.
    # We require this in order to serve the HTML version of the OpenAPI docs
    # via https.
    application.wsgi_app = ProxyFix(application.wsgi_app)
示例#4
0
def init_logging(app):
    location_log_config = app.config['LOGGING_CONFIG_LOCATION']
    if os.path.isfile(location_log_config):
        logging.config.fileConfig(location_log_config,
                                  disable_existing_loggers=True)
        logger.info('Loaded logging configuration file "%s"',
                    location_log_config)
    else:
        logger.warning('Error loading configuration file "%s"',
                       location_log_config)
    if app.config['SENTRY_DSN']:
        # This could not be done in the default .ini because the
        # handler has to be passed to `raven.setup_logging`.

        # the following adds itself to app.extensions['sentry']
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])

        handler = SentryHandler(app.extensions['sentry'].client)
        handler.level = logging.NOTSET
        setup_logging(handler)

        logger.debug("Sentry DSN: {}".format(app.config['SENTRY_DSN']))
    else:
        logger.debug("No sentry DSN specified")
示例#5
0
def make_application():
    from metadataproxy.settings import Settings

    flask_app = Flask(__name__, static_url_path='')

    if Settings.BUGSNAG_API_KEY:
        import bugsnag
        from bugsnag.flask import handle_exceptions
        bugsnag.configure(api_key=Settings.BUGSNAG_API_KEY)
        handle_exceptions(flask_app)
    elif Settings.SENTRY_DSN:
        from raven.contrib.flask import Sentry
        sentry = Sentry()
        sentry.init_app(flask_app, dsn=Settings.SENTRY_DSN)

    flask_app.config.from_object('metadataproxy.settings.Settings')
    flask_app.debug = Settings.DEBUG

    if flask_app.config['MOCK_API']:
        import metadataproxy.routes.mock
        flask_app.register_blueprint(metadataproxy.routes.mock.blueprint_http)
        metadataproxy.routes.mock.blueprint_http.config = flask_app.config
    else:
        import metadataproxy.routes.proxy
        flask_app.register_blueprint(metadataproxy.routes.proxy.blueprint_http)
        metadataproxy.routes.proxy.blueprint_http.config = flask_app.config

    return flask_app
示例#6
0
def get_wsgi_app(config):

    app = Flask("PROJECT")
    app.config.from_object(config)

    # register blueprint
    blueprints = (bp_hello, )
    for bp in blueprints:
        app.register_blueprint(bp)

    # orm and redis, default not open
    # app.sa_engine = engine_from_config(app.config["SQLALCHEMY_OPTIONS"],
                                       # prefix="")

    # app.DBSession = scoped_session(sessionmaker(bind=app.sa_engine),
                                   # scopefunc=_app_ctx_stack.__ident_func__)

    # app.redis_client = StrictRedis(**app.config["REDIS_CONFIG"])

    # @app.before_request
    # def before_request():
        # g.rds = current_app.redis_client
        # g.db = current_app.DBSession()

    # @app.teardown_request
    # def teardown_request(exception):
        # g.db.close()
    # init thrall sentry
    if (not app.debug) and app.config.get("SENTRY_ON", False):
        from raven.contrib.flask import Sentry
        sentry = Sentry(dsn=app.config["SENTRY_DSN"], logging=True, level=logging.ERROR)
        app.config['SENTRY_NAME'] = app.config["SENTRY_IDENTIFY"]
        sentry.init_app(app)

    return app
示例#7
0
文件: __init__.py 项目: ox-it/moxie
def create_app():
    app = Moxie(__name__)
    configurator = Configurator(app)
    cfg_path = path.join(app.root_path, 'default_settings.yaml')
    configurator.from_yaml(cfg_path)
    configurator.from_envvar('MOXIE_SETTINGS', silent=True)

    # logging configuration for Raven/Sentry
    if raven_available and 'SENTRY_DSN' in app.config:
        sentry = Sentry(dsn=app.config['SENTRY_DSN'])
        # capture uncaught exceptions within Flask
        sentry.init_app(app)
        handler = SentryHandler(app.config['SENTRY_DSN'],
                                level=logging.getLevelName(
                                    app.config.get('SENTRY_LEVEL', 'WARNING')))
        setup_logging(handler)

    statsd.init_app(app)
    cache.init_app(app)
    db.init_app(app)

    # Static URL Route for API Health checks
    app.add_url_rule('/_health', view_func=check_services)
    app.add_url_rule('/', view_func=RootView.as_view('root'))
    return app
示例#8
0
def init_logging(app):
    location_log_config = app.config['LOGGING_CONFIG_LOCATION']
    if os.path.isfile(location_log_config):
        logging.config.fileConfig(location_log_config,
                                  disable_existing_loggers=True)
        logger.info('Loaded logging configuration file "%s"',
                    location_log_config)
    else:
        logger.warning('Error loading configuration file "%s"',
                       location_log_config)
    if app.config['SENTRY_DSN']:
        # This could not be done in the default .ini because the
        # handler has to be passed to `raven.setup_logging`.

        # the following adds itself to app.extensions['sentry']
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])

        handler = SentryHandler(app.extensions['sentry'].client)
        handler.level = logging.NOTSET
        setup_logging(handler)

        logger.debug("Sentry DSN: {}".format(app.config['SENTRY_DSN']))
    else:
        logger.debug("No sentry DSN specified")
示例#9
0
def create_app(name, settings_override={}):
    app = Flask(name, static_folder='build/static', static_url_path='/static')

    configure(app)
    app.json_encoder = CustomJSONEncoder
    app.config.update(settings_override)
    app.register_blueprint(routes, url_prefix='/api')
    app.register_blueprint(auth, url_prefix='/api')
    app.register_blueprint(static)
    app.register_blueprint(rq_dashboard.blueprint, url_prefix="/rq")

    migrate.init_app(app, db)
    db.init_app(app)
    login_manager.init_app(app)
    admin.init_app(app)
    rq.init_app(app)

    if os.environ.get('CONFIG_ENV') == 'prod':
        sentry = Sentry(
            app,
            dsn=(
                'https://*****:*****@sentry.io/124868'))
    else:
        sentry = Sentry(app)
    sentry.init_app(app)

    return app
示例#10
0
def init_app(application, interface):
    """Initialize the main app with config information and routes."""
    # Note that local modules are imported here to avoid circular imports in
    # modules that need to import the app.
    from simulations import errorhandlers, jwt, middleware, resources, storage

    # Configuration
    app.wsgi_app = ProxyFix(app.wsgi_app)
    logging.config.dictConfig(application.config["LOGGING"])
    if application.config["SENTRY_DSN"]:
        sentry = Sentry(dsn=application.config["SENTRY_DSN"],
                        logging=True,
                        level=logging.ERROR)
        sentry.init_app(application)

    # Middleware and global handlers
    middleware.init_app(application)
    jwt.init_app(application)
    CORS(application)
    errorhandlers.init_app(application)

    # Routes and resources
    resources.init_app(application)

    # Preload all models in production/staging environments
    if os.environ["ENVIRONMENT"] in ("production", "staging"):
        storage.preload_public_models()
示例#11
0
def create_app():
    app = Flask(__name__)
    app.debug = os.getenv('DEBUG') == 'True'
    if os.getenv('SENTRY_DSN'):
        sentry = Sentry()
        sentry.init_app(app)
    return app
示例#12
0
def init_app(application):
    """Initialize the main app with config information and routes."""
    from id_mapper.settings import current_config
    application.config.from_object(current_config())

    # Configure logging
    logging.config.dictConfig(application.config['LOGGING'])

    # Configure Sentry
    if application.config['SENTRY_DSN']:
        sentry = Sentry(dsn=application.config['SENTRY_DSN'],
                        logging=True,
                        level=logging.ERROR)
        sentry.init_app(application)

    # Add routes and resources.
    from id_mapper import resources
    resources.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Register error handlers
    errorhandlers.init_app(application)

    # Please keep in mind that it is a security issue to use such a middleware
    # in a non-proxy setup because it will blindly trust the incoming headers
    # which might be forged by malicious clients.
    # We require this in order to serve the HTML version of the OpenAPI docs
    # via https.
    application.wsgi_app = ProxyFix(application.wsgi_app)
示例#13
0
    def setup_sentry(self, sentry_dsn):
        sentry_dsn = sentry_dsn or os.getenv("SENTRY_DSN", None)
        if not Sentry or not sentry_dsn:
            return

        sentry = Sentry(dsn=sentry_dsn)
        sentry.init_app(self.app)
        self.app.logger.info("Sentry is enabled.")
示例#14
0
def create_app(configuration=None, app_name=None, blueprints=None):
    """Create the main Flask app."""

    if app_name is None:
        app_name = config.DefaultConfig.APP_NAME
    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    app = Flask(app_name)
    # configure app from object or environment
    configure_app(app, configuration)

    if app.config.get('SENTRY_DSN'):
        from raven.contrib.flask import Sentry
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])
        sentry_report_uri = 'https://sentry.io/api/%s/csp-report/?sentry_key=%s' % (
            sentry.client.remote.project, sentry.client.remote.public_key)
        talisman.content_security_policy_report_uri = sentry_report_uri
        sentry_public_dsn = 'https://%[email protected]/%s' % (
            sentry.client.remote.public_key, sentry.client.remote.project)
        app.config['SENTRY_DSN_PUBLIC'] = sentry_public_dsn

    # init extensions once we have app context
    init_extensions(app)
    # then blueprints, for url/view routing
    register_blueprints(app, blueprints)

    configure_logging(app)
    configure_error_pages(app)

    # store production assets
    if app.config.get('STORE_DOMAIN'):
        store_domain = urlparse.urlparse(app.config['STORE_DOMAIN']).netloc
        # set production security headers
        if app.config['ENVIRONMENT'] == "Production":
            # append media-src to include flask-store domain
            CALLPOWER_CSP['media-src'].extend(store_domain)
            talisman.init_app(app,
                              force_https=True,
                              content_security_policy=CALLPOWER_CSP)
    else:
        app.logger.error(
            'Flask-store is not configured, you may not be able to serve audio recordings'
        )

    # then extension specific configurations
    configure_babel(app)
    configure_login(app)
    configure_assets(app)
    configure_restless(app)

    # finally instance specific configurations
    context_processors(app)
    instance_defaults(app)

    app.logger.info('Call Power started')
    return app
示例#15
0
def create_app():
    sentry_url = os.getenv('SENTRY_URL').strip()
    sentry = Sentry(dsn=sentry_url)
    print("sentry", sentry_url)
    app = Flask(__name__)
    app.logger.addHandler(logging.StreamHandler(sys.stdout))
    app.logger.setLevel(logging.DEBUG)
    sentry.init_app(app)
    return app
示例#16
0
def init_app(application, interface):
    """Initialize the main app with config information and routes."""
    if os.environ["ENVIRONMENT"] == "production":
        from memote_webservice.settings import Production
        application.config.from_object(Production())
    elif os.environ["ENVIRONMENT"] == "testing":
        from memote_webservice.settings import Testing
        application.config.from_object(Testing())
    else:
        from memote_webservice.settings import Development
        application.config.from_object(Development())

    # Configure logging
    logging.config.dictConfig(application.config['LOGGING'])
    root_logger = logging.getLogger()
    for handler in root_logger.handlers:
        handler.setFormatter(jsonlogger.JsonFormatter())
    structlog.configure(
        processors=[
            structlog.stdlib.filter_by_level,
            structlog.stdlib.add_logger_name,
            structlog.stdlib.add_log_level,
            structlog.processors.TimeStamper(fmt="iso"),
            structlog.stdlib.PositionalArgumentsFormatter(),
            structlog.processors.StackInfoRenderer(),
            structlog.processors.format_exc_info,
            structlog.processors.UnicodeDecoder(),
            structlog.stdlib.render_to_log_kwargs,
        ],
        # comment
        context_class=dict,
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True,
    )

    # Configure Sentry
    if application.config['SENTRY_DSN']:
        sentry = Sentry(dsn=application.config['SENTRY_DSN'],
                        logging=True,
                        level=logging.WARNING)
        sentry.init_app(application)

    # Add routes and resources.
    from memote_webservice.resources.submit import api as submit
    interface.add_namespace(submit, path="/submit")
    from memote_webservice.resources.result import api as result
    interface.add_namespace(result, path="/result")
    interface.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Add Redis caching.
    redis_store.init_app(application)

    LOGGER.debug("Successfully initialized the app.")
def init_errorhandler(app):
    sentry = Sentry(dsn=app.config["SENTRY_DSN"])
    sentry.init_app(app)

    app.register_error_handler(werkzeug_exceptions.NotFound, handle_404)
    app.register_error_handler(
        marsh_exceptions.ValidationError, handle_marsh_validation_error
    )
    app.register_error_handler(Exception, handle_global_error(app, sentry))
示例#18
0
def init_sentry():
    """
    Initialize the Sentry client.

    :return: A Sentry instance used universally for capturing uncaught exceptions.
    """
    sentry = Sentry(dsn=config.secrets.server('sentry_server_dsn'))
    sentry.init_app(app)
    return sentry
示例#19
0
def register_extensions(app):
    sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])
    sentry.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)
    whooshee.init_app(app)
    register_shell_context(app)
    csrf.init_app(app)
    moment.init_app(app)
    migrate = Migrate(app, db)
示例#20
0
def init_app(application):
    """Initialize the main app with config information and routes."""
    from map_storage.settings import current_config
    application.config.from_object(current_config())

    # Configure logging
    logging.config.dictConfig(application.config['LOGGING'])

    # Configure Sentry
    if application.config['SENTRY_DSN']:
        sentry = Sentry(dsn=application.config['SENTRY_DSN'],
                        logging=True,
                        level=logging.ERROR)
        sentry.init_app(application)

    # Initialize the database
    from .models import db
    Migrate(application, db)
    db.init_app(application)

    # Add JWT handling middleware
    jwt.init_app(application)

    # Register error handlers
    errorhandlers.init_app(application)

    # Add routes and resources.
    resources.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Add an error handler for webargs parser error, ensuring a JSON response
    # including all error messages produced from the parser.
    @application.errorhandler(422)
    def handle_webargs_error(error):
        response = jsonify(error.data['messages'])
        response.status_code = error.code
        return response

    # Handle werkzeug HTTPExceptions (typically raised through `flask.abort`) by
    # returning a JSON response including the error description.
    @application.errorhandler(HTTPException)
    def handle_error(error):
        response = jsonify({'message': error.description})
        response.status_code = error.code
        return response

    # Please keep in mind that it is a security issue to use such a middleware
    # in a non-proxy setup because it will blindly trust the incoming headers
    # which might be forged by malicious clients.
    # We require this in order to serve the HTML version of the OpenAPI docs
    # via https.
    application.wsgi_app = ProxyFix(application.wsgi_app)
示例#21
0
def _load_extension_sentry(app):
    """Init sentry for logging stream.

    reference:
        - https://docs.sentry.io/clients/python/integrations/flask/
    """
    from raven.contrib.flask import Sentry
    if env.python_env not in ['production', 'demo']:
        return
    sentry = Sentry(dsn=env.must_get('SENTRY_DSN'))
    sentry.init_app(app)
示例#22
0
def create_app():
    app = Flask(__name__)
    app.url_map.converters['date'] = DateConverter
    app.config.from_object('citadel.config')
    app.secret_key = app.config['SECRET_KEY']

    app.url_map.strict_slashes = False

    make_celery(app)
    db.init_app(app)
    oauth.init_app(app)
    mako.init_app(app)
    cache.init_app(app)
    sess.init_app(app)
    sockets.init_app(app)

    if not DEBUG:
        sentry = Sentry(dsn=SENTRY_DSN)
        sentry.init_app(app)

    for bp_name in api_blueprints:
        bp = import_string('%s.api.%s:bp' % (__package__, bp_name))
        app.register_blueprint(bp)

    # action APIs are all websockets
    from citadel.api.action import ws
    sockets.register_blueprint(ws)

    @app.before_request
    def init_global_vars():
        g.start = request.args.get('start', type=int, default=0)
        g.limit = request.args.get('limit', type=int, default=20)
        g.zone = request.values.get('zone') or session.get(
            'zone') or DEFAULT_ZONE

    @app.errorhandler(422)
    def handle_unprocessable_entity(err):
        # webargs attaches additional metadata to the `data` attribute
        exc = getattr(err, 'exc')
        if exc:
            # Get validations from the ValidationError object
            messages = exc.messages
        else:
            messages = ['Invalid request']
        return jsonify({
            'messages': messages,
        }), 422

    @app.route('/')
    def hello_world():
        return 'Hello world'

    return app
示例#23
0
def init_app(app):
    if 'SENTRY_DSN' in app.config:
        try:
            from raven.contrib.celery import (
                register_signal, register_logger_signal
            )
            from raven.contrib.flask import Sentry
        except:
            log.error('raven[flask] is required to use sentry')
            return

        sentry = Sentry()
        tags = app.config['SENTRY_TAGS'] = app.config.get('SENTRY_TAGS', {})

        app.config.setdefault('SENTRY_USER_ATTRS',
                              ['slug', 'email', 'fullname'])
        app.config.setdefault('SENTRY_LOGGING', 'WARNING')

        log_level_name = app.config.get('SENTRY_LOGGING')
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = app.config.get('RAVEN_IGNORE_EXCEPTIONS', [])
        if HTTPException not in exceptions:
            exceptions.append(HTTPException)
        if PermissionDenied not in exceptions:
            exceptions.append(PermissionDenied)
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = exceptions

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        # Versions Management: uData and plugins versions as tags.
        packages = ['udata']
        packages += ['udata_{0}'.format(p) for p in app.config['PLUGINS']]

        for package in packages:
            version = pkg_resources.get_distribution(package).version
            if version:
                tags[package] = version

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#24
0
文件: app.py 项目: 18mr/call-congress
def create_app(configuration=None, app_name=None, blueprints=None):
    """Create the main Flask app."""

    if app_name is None:
        app_name = config.DefaultConfig.APP_NAME
    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    app = Flask(app_name)
    # configure app from object or environment
    configure_app(app, configuration)
        
    # set production security headers
    if app.config['ENVIRONMENT'] == "Production":
        # append media-src to include flask-store domain
        store_domain = urlparse.urlparse(app.config['STORE_DOMAIN']).netloc,
        CALLPOWER_CSP['media-src'].extend(store_domain)
        talisman.init_app(app,
            force_https=True,
            content_security_policy=CALLPOWER_CSP
        )

    if app.config.get('SENTRY_DSN'):
        from raven.contrib.flask import Sentry
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])
        sentry_report_uri = 'https://sentry.io/api/%s/csp-report/?sentry_key=%s' % (
            sentry.client.remote.project, sentry.client.remote.public_key
        )
        talisman.content_security_policy_report_uri = sentry_report_uri

    # init extensions once we have app context
    init_extensions(app)
    # then blueprints, for url/view routing
    register_blueprints(app, blueprints)

    configure_logging(app)
    configure_error_pages(app)

    # then extension specific configurations
    configure_babel(app)
    configure_login(app)
    configure_assets(app)
    configure_restless(app)

    # finally instance specific configurations
    context_processors(app)
    instance_defaults(app)

    app.logger.info('Call Power started')
    return app
示例#25
0
def init_app(app):
    if 'SENTRY_DSN' in app.config:
        try:
            from raven.contrib.celery import (register_signal,
                                              register_logger_signal)
            from raven.contrib.flask import Sentry
        except:
            log.error('raven[flask] is required to use sentry')
            return

        sentry = Sentry()
        tags = app.config['SENTRY_TAGS'] = app.config.get('SENTRY_TAGS', {})

        app.config.setdefault('SENTRY_USER_ATTRS',
                              ['slug', 'email', 'fullname'])
        app.config.setdefault('SENTRY_LOGGING', 'WARNING')

        log_level_name = app.config.get('SENTRY_LOGGING')
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = app.config.get('RAVEN_IGNORE_EXCEPTIONS', [])
        if HTTPException not in exceptions:
            exceptions.append(HTTPException)
        if PermissionDenied not in exceptions:
            exceptions.append(PermissionDenied)
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = exceptions

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        # Versions Management: uData and plugins versions as tags.
        packages = ['udata']
        packages += ['udata_{0}'.format(p) for p in app.config['PLUGINS']]

        for package in packages:
            version = pkg_resources.get_distribution(package).version
            if version:
                tags[package] = version

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#26
0
def create_app(name=None):
    app = Flask(name)

    if os.environ.get('PRODUCTION'):
        app.config.from_object(ProductionConfig)
        print "running with ProductionConfig"
    else:
        app.config.from_object(DefaultConfig)
        print "running with DefaultConfig"

    # sentry
    if app.config.get('SENTRY_DSN'):
        sentry = Sentry()
        sentry.init_app(app)
        app.sentry = sentry

    # assets
    assets = Environment(app)
    assets.url = app.static_url_path
    scss_bundle = Bundle('css/*.scss', 'css/*.css',
        filters=['scss', 'cssmin'], depends='css/*.scss', output='css/all.css')
    assets.register('scss_all', scss_bundle)
    js_bundle = Bundle('js/*.js', filters='rjsmin', output='js/all.js')
    assets.register('js_all', js_bundle)
    Compress(app)

    # cache
    if app.config['DEBUG']:
        cache_type = 'null'
    else:
        cache_type = 'simple'

    cache = Cache(config={'CACHE_TYPE': cache_type})
    cache.init_app(app)
    app.cache = cache

    # CDN
    cdn = CDN()
    cdn.init_app(app)

    # workaround flask-assets / flask-cdn integration
    if app.config.get('CDN_HTTPS'):
        cdn_scheme = 'https'
    else:
        cdn_scheme = 'http'
    if app.config.get('FLASK_ASSETS_USE_CDN') and app.config.get('CDN_DOMAIN'):
        app.jinja_env.globals['FLASK_CDN'] = '%s://%s' % (cdn_scheme, app.config['CDN_DOMAIN'])

    return app
示例#27
0
def init_app(application, db):
    """Initialize the main app with config information and routes."""
    application.config.from_object(current_config())

    # Configure logging
    logging.config.dictConfig(application.config["LOGGING"])
    db.init_app(application)
    Migrate(application, db)

    # Configure Sentry
    if application.config["SENTRY_DSN"]:
        sentry = Sentry(dsn=application.config["SENTRY_DSN"],
                        logging=True,
                        level=logging.ERROR)
        sentry.init_app(application)

    # Add routes and resources.
    resources.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Add JWT middleware
    jwt.init_app(application)

    # Register error handlers
    errorhandlers.init_app(application)

    # Please keep in mind that it is a security issue to use such a middleware
    # in a non-proxy setup because it will blindly trust the incoming headers
    # which might be forged by malicious clients.
    # We require this in order to serve the HTML version of the OpenAPI docs
    # via https.
    application.wsgi_app = ProxyFix(application.wsgi_app)

    # Add Flask CLI command to install fixtures in the database
    app.logger.debug("Registering CLI commands")

    @application.cli.command()
    def make_fixtures():
        with open("fixtures/models.json") as json_data:
            fixtures = json.load(json_data)
        for fixture in fixtures["rest-api-fixtures"]:
            model = Model(**fixture)
            db.session.add(model)
        db.session.commit()

    app.logger.info("App initialization complete")
示例#28
0
文件: app.py 项目: vtemian/kruncher
def create_app(config=None):
  app = Flask(__name__)

  config = get_config()
  app.config.from_object(config)

  db.init_app(app)

  app = register_endpoints(app)

  @app.errorhandler(404)
  def page_not_found(e):
    import urllib
    output = ""
    for rule in app.url_map.iter_rules():

        options = {}
        for arg in rule.arguments:
            options[arg] = "[{0}]".format(arg)

        methods = ','.join(rule.methods)
        url = url_for(rule.endpoint, **options)
        line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
        line = "<strong>%s</strong> %s %s" % (rule.endpoint, methods, urllib.unquote(url))
        output += "<li>" + line + "</li>"

    return """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

<h3>Current routes:</h3>
<ul>
%s
</ul>
    """ % output, 404

  if 'LOGGING' in app.config:
    configure_logging(app.config['LOGGING'])

  if 'SENTRY_DSN' in app.config:
    sentry = Sentry(dsn=app.config['SENTRY_DSN'], logging=True,
                    level=logging.ERROR)
    sentry.init_app(app)
    app.wsgi = SentryMiddleware(app.wsgi_app, sentry.client)

  return app
示例#29
0
文件: app.py 项目: DD-DeCaF/warehouse
def init_app(application):
    """Initialize the main app with config information and routes."""
    from warehouse import models, resources

    logging.config.dictConfig(application.config["LOGGING"])
    application.wsgi_app = ProxyFix(application.wsgi_app)

    # Configure Sentry
    if application.config["SENTRY_DSN"]:
        sentry = Sentry(dsn=application.config["SENTRY_DSN"],
                        logging=True,
                        level=logging.WARNING)
        sentry.init_app(application)

    # Add JWT middleware
    jwt.init_app(application)

    # Add custom error handlers
    errorhandlers.init_app(application)

    # Add routes and resources
    resources.init_app(application)

    # Add the flask-admin interface
    @application.before_request
    def restrict_admin():
        if request.path.startswith(
                admin.url) and not basic_auth.authenticate():
            return basic_auth.challenge()

    admin.add_view(ModelView(models.Organism, db.session))
    admin.add_view(ModelView(models.Strain, db.session))
    admin.add_view(ModelView(models.Experiment, db.session))
    admin.add_view(ModelView(models.Medium, db.session))
    admin.add_view(ModelView(models.MediumCompound, db.session))
    admin.add_view(ModelView(models.Condition, db.session))
    admin.add_view(ModelView(models.Sample, db.session))
    admin.add_view(ModelView(models.Fluxomics, db.session))
    admin.add_view(ModelView(models.Metabolomics, db.session))
    admin.add_view(ModelView(models.Proteomics, db.session))
    admin.add_view(ModelView(models.UptakeSecretionRates, db.session))
    admin.add_view(ModelView(models.MolarYields, db.session))
    admin.add_view(ModelView(models.Growth, db.session))

    # Add CORS information for all resources.
    CORS(application)
示例#30
0
def init_app(app):
    if 'SENTRY_DSN' in app.config:
        try:
            from raven.contrib.celery import (register_signal,
                                              register_logger_signal)
            from raven.contrib.flask import Sentry
        except ImportError:
            log.error('raven[flask] is required to use sentry')
            return

        sentry = Sentry()
        tags = app.config['SENTRY_TAGS'] = app.config.get('SENTRY_TAGS', {})

        app.config.setdefault('SENTRY_USER_ATTRS',
                              ['slug', 'email', 'fullname'])
        app.config.setdefault('SENTRY_LOGGING', 'WARNING')

        log_level_name = app.config.get('SENTRY_LOGGING')
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = set(app.config.get('RAVEN_IGNORE_EXCEPTIONS', []))
        for exception in IGNORED_EXCEPTIONS:
            exceptions.add(exception)
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = list(exceptions)

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        # Versions Management: uData and plugins versions as tags.
        for dist in entrypoints.get_plugins_dists(app):
            if dist.version:
                tags[dist.project_name] = dist.version
        # Do not forget udata itself
        tags['udata'] = pkg_resources.get_distribution('udata').version

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#31
0
def setup_sentry(app: Flask):
    sentry_dsn = app.config.get('SENTRY_DSN')
    if not sentry_dsn:
        client = DummyClient()
    else:
        client = Client(
            sentry_dsn,
            transport=GeventedHTTPTransport,
            release=app.config.get('CURRENT_RELEASE'),
            environment=app.config.get('CURRENT_ENVIRONMENT'),
        )

    sentry = Sentry(client=client, logging=True, level=logging.WARNING)
    handler = SentryHandler(client, level=logging.WARNING)
    setup_sentry_logging(handler)
    sentry.init_app(app)

    return sentry, client
示例#32
0
def init_logging(app):
    """Initialize the app's logging mechanisms

    - Configure the sentry client, if a DSN is given
    - Apply the default config dict (`defaults.DEFAULT_CONFIG`)
    - If given and existent, apply the additional config file
    """

    # Configure Sentry client (raven)
    if app.config['SENTRY_DSN']:
        logger.debug("Sentry DSN: %s", app.config['SENTRY_DSN'])
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])

        def register_sentry_handler():
            handler = SentryHandler()

            handler.client = app.extensions['sentry'].client
            setup_logging(handler)

            return handler
    else:
        logger.debug("No sentry DSN specified")

        def register_sentry_handler():
            return logging.NullHandler()

    # Apply default config dict
    config = replace_empty_handler_callables(DEFAULT_CONFIG,
                                             register_sentry_handler)
    logging.config.dictConfig(config)

    if app.config.get('LOG_CONFIG') is not None:
        config = replace_empty_handler_callables(app.config['LOG_CONFIG'],
                                                 register_sentry_handler)
        logging.config.dictConfig(config)

    logger.debug('Initialized logging',
                 extra={
                     'data': {
                         'DEFAULT_CONFIG': DEFAULT_CONFIG,
                         'EXTRA_CONFIG': app.config.get('LOG_CONFIG')
                     }
                 })
示例#33
0
def config():
    logging.basicConfig(level=logging.DEBUG)
    # load captcha defaults
    app_flask.config.from_object("flask.ext.captcha.settings")

    app_flask.config.from_object("settings")
    app.config_from_object("settings")


    settings_file = os.environ.get('AGORA_ELECTION_SETTINGS', None)
    if settings_file is not None:
        if not os.path.isabs(settings_file):
            os.environ['AGORA_ELECTION_SETTINGS'] = os.path.abspath(settings_file)
        logging.debug("AGORA_ELECTION_SETTINGS "
                      "= %s" % os.environ['AGORA_ELECTION_SETTINGS'])
        app_flask.config.from_envvar('AGORA_ELECTION_SETTINGS', silent=False)

    # an optimization
    election_url = app_flask.config['AGORA_ELECTION_DATA_URL']

    if election_url.startswith("http"):
        import requests
        bauth = app_flask.config.get('AGORA_ELECTION_DATA_BASIC_AUTH', None)
        election_json = requests.get(election_url, verify=False, auth=bauth).json()
        extra_data_json =  requests.get(election_url + "extra_data/",
                                        verify=False, auth=bauth).json()
    else:
        with open(election_url, 'r', encoding="utf-8") as f:
            election_json = json.loads(f.read())
            # NOTE: do not support extra_data in this mode
            extra_data_json = dict()

    edata = app_flask.config.get('AGORA_ELECTION_DATA', {})
    edata['election'] = election_json
    edata['election_extra_data'] = extra_data_json
    app_flask.config['AGORA_ELECTION_DATA_STR'] = Markup(json.dumps(
        app_flask.config.get('AGORA_ELECTION_DATA', {})))

    # config captcha
    app_captcha.init_app(app_flask)
    app_mail.init_app(app_flask)
    sentry = Sentry()
    sentry.init_app(app=app_flask)
    app_captcha.init_app(app_flask)
示例#34
0
def create_app(configfile=None, logconf_file=None):
    from .frontend import frontend

    # We are using the "Application Factory"-pattern here, which is described
    # in detail inside the Flask docs:
    # http://flask.pocoo.org/docs/patterns/appfactories/

    ## log-configuration must come before Flask-config.
    #
    os.environ.get('%s_LOGCONF_FILE' % __name__)
    init_logging(logconf_file=logconf_file, not_using_numpy=True)

    app = Flask(__name__)  #, instance_relative_config=True)

    app.config.from_object('webstamp.default_config')
    app.config.from_envvar('WEBSTAMP_CONFIG')

    app.config['POLYVERSION'] = __version__
    app.config['POLYTIME'] = __updated__

    ## Automatically discover DSN key:
    #  https://docs.sentry.io/clients/python/integrations/flask/#setup
    #
    if 'SENTRY_DSN' in app.config:
        sentry = Sentry()
        sentry_log_level = app.config.get('SENTRY_LOG_LEVEL')
        sentry.init_app(app,
                        logging=bool(sentry_log_level),
                        level=sentry_log_level)

    # Install our Bootstrap extension
    Bootstrap(app)

    # Our application uses blueprints as well; these go well with the
    # application factory. We already imported the blueprint, now we just need
    # to register it:
    app.register_blueprint(frontend)

    # Because we're security-conscious developers, we also hard-code disabling
    # the CDN support (this might become a default in later versions):
    #app.config['BOOTSTRAP_SERVE_LOCAL'] = True

    return app
示例#35
0
def init_app(application):
    """Initialize the main app with config information and routes."""
    if os.environ["ENVIRONMENT"] == "production":
        from memote_webservice.settings import Production

        application.config.from_object(Production())
    elif os.environ["ENVIRONMENT"] == "testing":
        from memote_webservice.settings import Testing

        application.config.from_object(Testing())
    else:
        from memote_webservice.settings import Development

        application.config.from_object(Development())

    # Configure logging
    logging.config.dictConfig(application.config["LOGGING"])

    # Configure Sentry
    if application.config["SENTRY_DSN"]:
        sentry = Sentry(dsn=application.config["SENTRY_DSN"],
                        logging=True,
                        level=logging.ERROR)
        sentry.init_app(application)

    # Add routes and resources.
    from memote_webservice import resources

    resources.init_app(application)

    # Add CORS information for all resources.
    CORS(application)

    # Register error handlers
    errorhandlers.init_app(application)

    # Please keep in mind that it is a security issue to use such a middleware
    # in a non-proxy setup because it will blindly trust the incoming headers
    # which might be forged by malicious clients.
    application.wsgi_app = ProxyFix(application.wsgi_app)

    LOGGER.debug("Successfully initialized the app.")
示例#36
0
文件: sentry.py 项目: odtvince/udata
def init_app(app):
    if app.config['SENTRY_DSN']:
        try:
            from raven.contrib.celery import (
                register_signal, register_logger_signal
            )
            from raven.contrib.flask import Sentry
        except ImportError:
            log.error('raven is required to use Sentry')
            return

        sentry = Sentry()
        tags = app.config['SENTRY_TAGS']
        log_level_name = app.config['SENTRY_LOGGING']
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = set(app.config['SENTRY_IGNORE_EXCEPTIONS'])
        for exception in IGNORED_EXCEPTIONS:
            exceptions.add(exception)
        app.config['SENTRY_IGNORE_EXCEPTIONS'] = list(exceptions)

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        # Versions Management: uData and plugins versions as tags.
        for dist in entrypoints.get_plugins_dists(app):
            if dist.version:
                tags[dist.project_name] = dist.version
        # Do not forget udata itself
        tags['udata'] = pkg_resources.get_distribution('udata').version

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#37
0
def init_app(app):
    if 'SENTRY_DSN' in app.config:
        try:
            from raven.contrib.celery import (
                register_signal, register_logger_signal
            )
            from raven.contrib.flask import Sentry
        except:
            log.error('raven[flask] is required to use sentry')
            return

        sentry = Sentry()

        app.config.setdefault('SENTRY_USER_ATTRS',
                              ['slug', 'email', 'fullname'])
        app.config.setdefault('SENTRY_LOGGING', 'WARNING')

        log_level_name = app.config.get('SENTRY_LOGGING')
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = app.config.get('RAVEN_IGNORE_EXCEPTIONS', [])
        if HTTPException not in exceptions:
            exceptions.append(HTTPException)
        if PermissionDenied not in exceptions:
            exceptions.append(PermissionDenied)
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = exceptions

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#38
0
def init_logging(app):
    """Initialize the app's logging mechanisms

    - Configure the sentry client, if a DSN is given
    - Apply the default config dict (`defaults.DEFAULT_CONFIG`)
    - If given and existent, apply the additional config file
    """

    # Configure Sentry client (raven)
    if app.config['SENTRY_DSN']:
        logger.debug("Sentry DSN: %s", app.config['SENTRY_DSN'])
        sentry = Sentry()
        sentry.init_app(app, dsn=app.config['SENTRY_DSN'])

        def register_sentry_handler():
            handler = SentryHandler()

            handler.client = app.extensions['sentry'].client
            setup_logging(handler)

            return handler
    else:
        logger.debug("No sentry DSN specified")

        def register_sentry_handler():
            return logging.NullHandler()

    # Apply default config dict
    config = replace_empty_handler_callables(DEFAULT_CONFIG,
                                             register_sentry_handler)
    logging.config.dictConfig(config)

    if app.config.get('LOG_CONFIG') is not None:
        config = replace_empty_handler_callables(app.config['LOG_CONFIG'],
                                                 register_sentry_handler)
        logging.config.dictConfig(config)

    logger.debug('Initialized logging', extra={'data': {
        'DEFAULT_CONFIG': DEFAULT_CONFIG,
        'EXTRA_CONFIG': app.config.get('LOG_CONFIG')
    }})
示例#39
0
文件: sentry.py 项目: simudream/udata
def init_app(app):
    if 'SENTRY_DSN' in app.config:
        try:
            from raven.contrib.celery import (register_signal,
                                              register_logger_signal)
            from raven.contrib.flask import Sentry
        except:
            log.error('raven[flask] is required to use sentry')
            return

        sentry = Sentry()

        app.config.setdefault('SENTRY_USER_ATTRS',
                              ['slug', 'email', 'fullname'])
        app.config.setdefault('SENTRY_LOGGING', 'WARNING')

        log_level_name = app.config.get('SENTRY_LOGGING')
        if log_level_name:
            log_level = getattr(logging, log_level_name.upper())
            if log_level:
                sentry.logging = True
                sentry.level = log_level

        # Do not send HTTPExceptions
        exceptions = app.config.get('RAVEN_IGNORE_EXCEPTIONS', [])
        if HTTPException not in exceptions:
            exceptions.append(HTTPException)
        if PermissionDenied not in exceptions:
            exceptions.append(PermissionDenied)
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = exceptions

        app.config['SENTRY_PUBLIC_DSN'] = public_dsn(app.config['SENTRY_DSN'])

        sentry.init_app(app)

        # register a custom filter to filter out duplicate logs
        register_logger_signal(sentry.client, loglevel=sentry.level)

        # hook into the Celery error handler
        register_signal(sentry.client)
示例#40
0
文件: manage.py 项目: eea/eea.seris
def create_app():
    import views

    app = flask.Flask(__name__, instance_relative_config=True)
    app.config.update(default_config)
    app.config.from_pyfile('settings.py', silent=True)

    _my_extensions = app.jinja_options["extensions"] + ["jinja2.ext.do"]
    app.jinja_options = dict(app.jinja_options, extensions=_my_extensions)

    database.initialize_app(app)
    views.register_on(app)

    if app.config["HTTP_PROXIED"]:
        from revproxy import ReverseProxied
        app.wsgi_app = ReverseProxied(app.wsgi_app, app.config)

    if app.config.get("SENTRY_DSN"):
        sentry = Sentry()
        sentry.init_app(app)

    return app
示例#41
0
def make_application():
    from flask import Flask
    from cronq.config import Config
    import cronq.web
    import os

    flask_app = Flask(__name__, static_url_path='/static')

    if Config.BUGSNAG_API_KEY:
        import bugsnag
        from bugsnag.flask import handle_exceptions
        bugsnag.configure(api_key=Config.BUGSNAG_API_KEY)
        handle_exceptions(flask_app)
    elif Config.SENTRY_DSN:
        from raven.contrib.flask import Sentry
        sentry = Sentry()
        sentry.init_app(flask_app, dsn=Config.SENTRY_DSN)

    flask_app.config.from_object('cronq.config.Config')
    flask_app.register_blueprint(cronq.web.blueprint_http)
    cronq.web.blueprint_http.config = flask_app.config

    return flask_app
示例#42
0
def make_application():
    from flask import Flask
    from cronq.config import Config
    import cronq.web
    import os

    flask_app = Flask(__name__,
                      static_url_path='/static')

    if Config.BUGSNAG_API_KEY:
        import bugsnag
        from bugsnag.flask import handle_exceptions
        bugsnag.configure(api_key=Config.BUGSNAG_API_KEY)
        handle_exceptions(flask_app)
    elif Config.SENTRY_DSN:
        from raven.contrib.flask import Sentry
        sentry = Sentry()
        sentry.init_app(flask_app, dsn=Config.SENTRY_DSN)

    flask_app.config.from_object('cronq.config.Config')
    flask_app.register_blueprint(cronq.web.blueprint_http)
    cronq.web.blueprint_http.config = flask_app.config

    return flask_app
示例#43
0
logger = logging.getLogger(__name__)	# the mediameter logger

# setup logging
sentry = Sentry(dsn=config.get('sentry', 'dsn'))
handler = SentryHandler(config.get('sentry', 'dsn'))
setup_logging(handler)
logging.basicConfig(level=logging.INFO)
mc_logger = logging.getLogger('mediacloud')
requests_logger = logging.getLogger('requests')

logger.info("---------------------------------------------------------------------------------------")

# Flask app
flapp = flask.Flask(__name__)
sentry.init_app(flapp)
flapp.secret_key = 'put secret key here'
assets = Environment(flapp)

# Create media cloud api
mc_key = config.get('mediacloud','key')
mc = mcapi.AdminMediaCloud(mc_key)
logger.info("Connected to MediaCloud with default key %s" % (mc_key))
#logging.getLogger('MediaCloud').setLevel(logging.DEBUG)

# Create user login manager
login_manager = flask_login.LoginManager()
login_manager.init_app(flapp)

# Connect to db
host = config.get('database', 'host')
示例#44
0
import os

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
from flask.ext.sqlalchemy import SQLAlchemy
from raven.contrib.flask import Sentry

app = Flask(__name__)
app.config.from_envvar('LUNAPORT_AGENT_CFG')
app.wsgi_app = ProxyFix(app.wsgi_app)  # Fix for old proxyes
db = SQLAlchemy(app)

if os.environ.get('LUNAPORT_ENV') == 'production':
    sentry = Sentry(app, dsn=app.config.get('SENTRY_DSN'))
    sentry.init_app(app)

from plugg_views import User, Job
from helpers import auth_required

user_ident = auth_required(User.UserIdent.as_view('user_ident'))
user_view = auth_required(User.User.as_view('user'))
job_view = auth_required(Test.Test.as_view('test'))

app.add_url_rule('/api/v1.0/userident/', view_func=user_ident, methods=['GET'])
app.add_url_rule('/api/v1.0/user/', defaults={'login': None}, view_func=user_view, methods=['GET'])
app.add_url_rule('/api/v1.0/user/', view_func=user_view, methods=['POST'])
app.add_url_rule('/api/v1.0/user/<login>', view_func=user_view, methods=['GET', 'PATCH'])

app.add_url_rule('/api/v1.0/job/', defaults={'job_id': None}, view_func=test_view, methods=['GET'])
app.add_url_rule('/api/v1.0/job/', view_func=test_view, methods=['POST'])
示例#45
0
def setup_sentry(app, settings):
    sentry = Sentry(dsn=settings.SENTRY_DSN)
    sentry.init_app(app)
示例#46
0
# TODO, figure out how to load gevent monkey patch cleanly in production
# try:
#     from gevent.monkey import patch_all
#     patch_all()
# except ImportError:
#     print "unable to apply gevent monkey.patch_all"

import os

from werkzeug.contrib.fixers import ProxyFix

from app import app as application

if application.config.get('SENTRY_DSN'):
    from raven.contrib.flask import Sentry
    sentry = Sentry()
    sentry.init_app(application, dsn=application.config.get('SENTRY_DSN'))

application.wsgi_app = ProxyFix(application.wsgi_app)
示例#47
0
def create_app(mode, configs=None, log_level=None, **kwargs):
    # Allow configuration information to be specified with enviroment vars
    env_configs = {}
    for key in os.environ:
        if key.startswith('SIMPLECOIN_CONFIG'):
            env_configs[key] = os.environ[key]

    env_configs = [env_configs[value] for value in sorted(env_configs)]

    configs = ['defaults.toml'] + (env_configs or []) + (configs or [])
    if len(configs) == 1:
        print("Unable to start with only the default config values! {}"
              .format(configs))
        exit(2)

    config_vars = {}
    for config in configs:
        if isinstance(config, basestring):
            if os.path.isabs(config):
                config_path = config
            else:
                config_path = os.path.join(root, config)
            config = open(config_path)

        updates = toml.loads(config.read())
        toml.toml_merge_dict(config_vars, updates)

    # Initialize our flask application
    # =======================================================================
    app = Flask(__name__, static_folder='../static', static_url_path='/static')
    app.jinja_loader = FileSystemLoader(os.path.join(root, 'templates'))

    # Objectizes all configurations
    # =======================================================================
    ConfigChecker(config_vars, app)

    # Setup logging
    # =======================================================================
    del app.logger.handlers[0]
    app.logger.setLevel(logging.NOTSET)
    log_format = logging.Formatter('%(asctime)s [%(name)s] [%(levelname)s]: %(message)s')
    log_level = getattr(logging, str(log_level), app.config['log_level'])

    logger = logging.getLogger()
    logger.setLevel(log_level)
    handler = logging.StreamHandler(stream=sys.stdout)
    handler.setFormatter(log_format)
    logger.addHandler(handler)

    # Handle optionally adding log file writers for each different run mode
    # =======================================================================
    if mode == "manage" and app.config['manage_log_file']:
        hdlr = logging.FileHandler(app.config['manage_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)
    if mode == "scheduler" and app.config['scheduler_log_file']:
        hdlr = logging.FileHandler(app.config['scheduler_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)
    if mode == "webserver" and app.config['webserver_log_file']:
        hdlr = logging.FileHandler(app.config['webserver_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)

    logging.getLogger("gunicorn.access").setLevel(logging.WARN)
    logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.INFO)

    # Add the debug toolbar if we're in debug mode
    # =======================================================================
    if app.config['DEBUG'] and mode == "webserver":
        # Log all stdout and stderr when in debug mode for convenience
        class LoggerWriter:
            def __init__(self, logger, level):
                self.logger = logger
                self.level = level

            def write(self, message):
                if message != '\n':
                    self.logger.log(self.level, message)

        sys.stdout = LoggerWriter(app.logger, logging.DEBUG)
        sys.stderr = LoggerWriter(app.logger, logging.DEBUG)

    # Register the powerpool datastore + Cache
    # =======================================================================
    db.init_app(app)

    def configure_redis(config):
        typ = config.pop('type')
        if typ == "mock_redis":
            from mockredis import mock_redis_client
            return mock_redis_client()
        return Redis(**config)

    cache_config = app.config.get('main_cache', dict(type='live'))
    cache_redis = configure_redis(cache_config)

    ds_config = app.config.get('redis_conn', dict(type='live'))
    ds_redis = configure_redis(ds_config)

    # Take advantage of the fact that werkzeug lets the host kwargs be a Redis
    # compatible object
    cache.init_app(app, config=dict(CACHE_TYPE='redis', CACHE_REDIS_HOST=cache_redis))
    app.redis = ds_redis

    sentry = False
    if app.config.get('sentry'):
        try:
            from raven.contrib.flask import Sentry
            sentry = Sentry()
        except Exception:
            app.logger.error("Unable to initialize sentry!")

    # Helpful global vars
    # =======================================================================
    app.SATOSHI = Decimal('0.00000001')
    app.MAX_DECIMALS = 28

    # Configure app for running manage.py functions
    # =======================================================================
    if mode == "manage" or mode == "webserver":
        # Dynamically add all the filters in the filters.py file
        for name, func in inspect.getmembers(filters, inspect.isfunction):
            app.jinja_env.filters[name] = func

    if mode == "manage":
        # Initialize the migration settings
        Migrate(app, db)
        # Disable for management mode
        if sentry:
            sentry = False

    # Configure app for serving web content
    # =======================================================================
    elif mode == "webserver":
        # try and fetch the git version information
        try:
            output = subprocess.check_output("git show -s --format='%ci %h'",
                                             shell=True).strip().rsplit(" ", 1)
            app.config['hash'] = output[1]
            app.config['revdate'] = output[0]
        # celery won't work with this, so set some default
        except Exception:
            app.config['hash'] = ''
            app.config['revdate'] = ''

        app.logger.info("Starting up SimpleCoin!\n{}".format("=" * 100))

    # Configure app for running scheduler.py functions + instantiate scheduler
    # =======================================================================
    elif mode == "scheduler":
        if sentry and 'SENTRY_NAME' in app.config:
            app.config['SENTRY_NAME'] = app.config['SENTRY_NAME'] + "_scheduler"

        app.logger.info("=" * 80)
        app.logger.info("SimpleCoin cron scheduler starting up...")
        setproctitle.setproctitle("simplecoin_scheduler")

        # Make app accessible from out monkey patched code. Messy....
        ThreadPool.app = app
        sched = Scheduler(standalone=True)
        # monkey patch the thread pool for flask contexts
        ThreadPool._old_run_jobs = ThreadPool._run_jobs
        def _run_jobs(self, core):
            self.app.logger.debug("Starting patched threadpool worker!")
            with self.app.app_context():
                ThreadPool._old_run_jobs(self, core)
        ThreadPool._run_jobs = _run_jobs
        # All these tasks actually change the database, and shouldn't
        # be run by the staging server
        if not app.config.get('stage', False):
            sched.add_cron_job(sch.compress_slices, minute='0,15,30,45',
                               second=35)
            # every minute at 55 seconds after the minute
            sched.add_cron_job(sch.generate_credits, second=55)
            sched.add_cron_job(sch.create_trade_req, args=("sell",), minute=1,
                               hour="0,6,12,18")
            sched.add_cron_job(sch.create_trade_req, args=("buy",), minute=1,
                               hour="0,6,12,18")
            # every minute at 55 seconds after the minute
            sched.add_cron_job(sch.collect_minutes, second=35)
            sched.add_cron_job(sch.collect_ppagent_data, second=40)
            # every five minutes 20 seconds after the minute
            sched.add_cron_job(sch.compress_minute,
                               minute='0,5,10,15,20,25,30,35,40,45,50,55',
                               second=20)
            # every hour 2.5 minutes after the hour
            sched.add_cron_job(sch.compress_five_minute, minute=2, second=30)
            # every minute 2 seconds after the minute
            sched.add_cron_job(sch.update_block_state, second=2)
            # every day
            sched.add_cron_job(sch.update_block_state, hour=0, second=0, minute=3)
        else:
            app.logger.info("Stage mode has been set in the configuration, not "
                            "running scheduled database altering cron tasks")

        sched.add_cron_job(sch.update_online_workers,
                           minute='0,5,10,15,20,25,30,35,40,45,50,55',
                           second=30)
        sched.add_cron_job(sch.cache_user_donation, minute='0,15,30,45',
                           second=15)
        sched.add_cron_job(sch.server_status, second=15)
        # every 15 minutes 2 seconds after the minute
        sched.add_cron_job(sch.leaderboard,
                           minute='0,5,10,15,20,25,30,35,40,45,50,55',
                           second=30)

        app.scheduler = sched

    if sentry:
        sentry.init_app(app, logging=True, level=logging.ERROR)

    # Route registration
    # =======================================================================
    from . import views, models, api, rpc_views
    app.register_blueprint(views.main)
    app.register_blueprint(rpc_views.rpc_views)
    app.register_blueprint(api.api, url_prefix='/api')

    return app
示例#48
0
application = whitenoise.WhiteNoise(
    app.wsgi_app,
    root="website",
    autorefresh=settings.DEBUG,
)


# Database
db = SQLAlchemy()
db.init_app(app)
migrate = Migrate(app, db)


# Sentry
sentry = Sentry()
sentry.init_app(app, app.config["SENTRY_DSN"])


# Register views
app.register_blueprint(view_handler)


# Meta
def main():  # pragma: nocover
    from werkzeug.serving import run_simple
    run_simple(
        settings.HOST,
        settings.PORT,
        application,
        use_reloader=settings.DEBUG,
        use_debugger=settings.DEBUG,
示例#49
0
# TODO, figure out how to load gevent monkey patch cleanly in production
try:
    from gevent.monkey import patch_all
    patch_all()
    import gevent_psycopg2
    gevent_psycopg2.monkey_patch()
except ImportError:
    print "unable to apply gevent monkey.patch_all"

import os

from werkzeug.contrib.fixers import ProxyFix

from app import create_app
from extensions import assets

assets._named_bundles = {}
application = create_app()
# requires application context
assets.auto_build = False

if os.environ.get('SENTRY_DSN'):
    from raven.contrib.flask import Sentry
    sentry = Sentry()
    sentry.init_app(application)

application.wsgi_app = ProxyFix(application.wsgi_app)
示例#50
0
def instance(app):
    """:rtype: Sentry"""
    sentry = Sentry()
    if not app.debug and not app.testing:
        sentry.init_app(app, **app.config['SENTRY_IO']['flask'])
    return sentry
示例#51
0
def create_app(mode, configs=None, log_level=None, **kwargs):
    # Allow configuration information to be specified with enviroment vars
    env_configs = {}
    for key in os.environ:
        if key.startswith('SIMPLECOIN_CONFIG'):
            env_configs[key] = os.environ[key]

    env_configs = [env_configs[value] for value in sorted(env_configs)]

    configs = ['defaults.toml'] + (env_configs or []) + (configs or [])
    if len(configs) == 1:
        print("Unable to start with only the default config values! {}"
              .format(configs))
        exit(2)

    config_vars = {}
    for config in configs:
        if isinstance(config, basestring):
            if os.path.isabs(config):
                config_path = config
            else:
                config_path = os.path.join(root, config)
            config = open(config_path)

        updates = toml.loads(config.read())
        toml.toml_merge_dict(config_vars, updates)

    # Initialize our flask application
    # =======================================================================
    app = Flask(__name__, static_folder='../static', static_url_path='/static')
    app.jinja_loader = FileSystemLoader(os.path.join(root, 'templates'))

    # Objectizes all configurations
    # =======================================================================
    ConfigChecker(config_vars, app)

    # Setup logging
    # =======================================================================
    del app.logger.handlers[0]
    app.logger.setLevel(logging.NOTSET)
    log_format = logging.Formatter('%(asctime)s [%(name)s] [%(levelname)s]: %(message)s')
    log_level = getattr(logging, str(log_level), app.config['log_level'])

    logger = logging.getLogger()
    logger.setLevel(log_level)
    handler = logging.StreamHandler(stream=sys.stdout)
    handler.setFormatter(log_format)
    logger.addHandler(handler)

    # Handle optionally adding log file writers for each different run mode
    # =======================================================================
    if mode == "manage" and app.config['manage_log_file']:
        hdlr = logging.FileHandler(app.config['manage_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)
    if mode == "scheduler" and app.config['scheduler_log_file']:
        hdlr = logging.FileHandler(app.config['scheduler_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)
    if mode == "webserver" and app.config['webserver_log_file']:
        hdlr = logging.FileHandler(app.config['webserver_log_file'])
        hdlr.setFormatter(log_format)
        logger.addHandler(hdlr)

    logging.getLogger("gunicorn.access").setLevel(logging.WARN)
    logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.INFO)

    # Add the debug toolbar if we're in debug mode
    # =======================================================================
    if app.config['DEBUG'] and mode == "webserver":
        # Log all stdout and stderr when in debug mode for convenience
        class LoggerWriter:
            def __init__(self, logger, level):
                self.logger = logger
                self.level = level

            def write(self, message):
                if message != '\n':
                    self.logger.log(self.level, message)

        sys.stdout = LoggerWriter(app.logger, logging.DEBUG)
        sys.stderr = LoggerWriter(app.logger, logging.DEBUG)

    # Register the powerpool datastore + Cache
    # =======================================================================
    db.init_app(app)
    babel.init_app(app)
    app.config['BABEL_DEFAULT_LOCALE'] = app.config.get('default_locale')

    def configure_redis(config):
        typ = config.pop('type')
        if typ == "mock_redis":
            from mockredis import mock_redis_client
            return mock_redis_client()
        return Redis(**config)

    cache_config = app.config.get('main_cache', dict(type='live'))
    cache_redis = configure_redis(cache_config)

    ds_config = app.config.get('redis_conn', dict(type='live'))
    ds_redis = configure_redis(ds_config)

    # Take advantage of the fact that werkzeug lets the host kwargs be a Redis
    # compatible object
    cache.init_app(app, config=dict(CACHE_TYPE='redis', CACHE_REDIS_HOST=cache_redis))
    app.redis = ds_redis

    sentry = False
    if app.config.get('sentry'):
        try:
            from raven.contrib.flask import Sentry
            sentry = Sentry()
        except Exception:
            app.logger.error("Unable to initialize sentry!")

    # Helpful global vars
    # =======================================================================
    app.SATOSHI = Decimal('0.00000001')
    app.MAX_DECIMALS = 28

    # Configure app for running manage.py functions
    # =======================================================================
    if mode == "manage" or mode == "webserver":
        # Dynamically add all the filters in the filters.py file
        for name, func in inspect.getmembers(filters, inspect.isfunction):
            app.jinja_env.filters[name] = func

    if mode == "manage":
        # Initialize the migration settings
        Migrate(app, db)
        # Disable for management mode
        if sentry:
            sentry = False

    # Configure app for serving web content
    # =======================================================================
    elif mode == "webserver":
        # try and fetch the git version information
        try:
            output = subprocess.check_output("git show -s --format='%ci %h'",
                                             shell=True).strip().rsplit(" ", 1)
            app.config['hash'] = output[1]
            app.config['revdate'] = output[0]
        # celery won't work with this, so set some default
        except Exception:
            app.config['hash'] = ''
            app.config['revdate'] = ''

        app.logger.info("Starting up SimpleCoin!\n{}".format("=" * 100))

    # Configure app for running scheduler.py functions + instantiate scheduler
    # =======================================================================
    elif mode == "scheduler":
        if sentry and 'SENTRY_NAME' in app.config:
            app.config['SENTRY_NAME'] = app.config['SENTRY_NAME'] + "_scheduler"

        app.logger.info("=" * 80)
        app.logger.info("SimpleCoin cron scheduler starting up...")
        setproctitle.setproctitle("simplecoin_scheduler")

        sched = Scheduler(standalone=True)

        # monkey patch the scheduler to wrap each job call in its own flask
        # context. Kind of sloppy way to pass in the app context...
        Scheduler.app = app
        Scheduler._old_run_job = Scheduler._run_job

        def _run_job(self, *args, **kwargs):
            with self.app.app_context():
                Scheduler._old_run_job(self, *args, **kwargs)
        Scheduler._run_job = _run_job

        stage_tasks = set(["cache_profitability", "leaderboard",
                           "server_status", "update_network",
                           "cache_user_donation", "update_online_workers"])
        for task_config in app.config['tasks']:
            if not task_config.get('enabled', False):
                continue
            if app.config['stage'] and task_config['name'] not in stage_tasks:
                app.logger.debug(
                    "Skipping scheduling {} because in stage mode!"
                    .format(task_config['name']))
                continue

            stripped_config = task_config.copy()
            del stripped_config['enabled']
            task = getattr(sch, task_config['name'])
            sched.add_cron_job(task, **stripped_config)

        app.scheduler = sched

    if sentry:
        sentry.init_app(app, logging=True, level=logging.ERROR)

    # Route registration
    # =======================================================================
    from . import views, models, api, rpc_views
    app.register_blueprint(views.main)
    app.register_blueprint(rpc_views.rpc_views)
    app.register_blueprint(api.api, url_prefix='/api')

    return app