Example #1
0
def create_app(config={}):
    app = Flask("aleph")
    app.config.from_object(default_settings)
    if config.get("TESTING"):
        app.config.from_envvar("ALEPH_TEST_SETTINGS", silent=True)
    else:
        app.config.from_envvar("ALEPH_SETTINGS", silent=True)
    app.config.update(config)
    app_name = app.config.get("APP_NAME")

    if not app.debug and app.config.get("MAIL_ADMINS"):
        credentials = (app.config.get("MAIL_USERNAME"), app.config.get("MAIL_PASSWORD"))
        mail_handler = SMTPHandler(
            app.config.get("MAIL_SERVER"),
            app.config.get("MAIL_FROM"),
            app.config.get("MAIL_ADMINS"),
            "[%s] Crash report" % app_name,
            credentials=credentials,
            secure=(),
        )
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

    if "postgres" not in app.config.get("SQLALCHEMY_DATABASE_URI", ""):
        raise RuntimeError("aleph database must be PostgreSQL!")

    app.config["CELERY_DEFAULT_QUEUE"] = WORKER_QUEUE
    app.config["CELERY_DEFAULT_ROUTING_KEY"] = WORKER_ROUTING_KEY
    app.config["CELERY_QUEUES"] = (
        Queue(WORKER_QUEUE, routing_key=WORKER_ROUTING_KEY),
        Queue(USER_QUEUE, routing_key=USER_ROUTING_KEY),
    )
    celery.conf.update(app.config)
    celery.conf.update({"BROKER_URL": app.config["CELERY_BROKER_URL"]})

    migrate.init_app(app, db, directory=app.config.get("ALEMBIC_DIR"))
    oauth.init_app(app)
    mail.init_app(app)
    db.init_app(app)
    assets.init_app(app)

    # This executes all registered init-time plugins so that other
    # applications can register their behaviour.
    for plugin in get_init():
        plugin(app=app)

    return app
Example #2
0
app_name = app.config.get("APP_NAME")

db = SQLAlchemy(app)
migrate = Migrate(app, db, directory=app.config.get("ALEMBIC_DIR"))

oauth = OAuth()
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "ui"

es = ElasticSearch(app.config.get("ELASTICSEARCH_URL"))
es_index = app.config.get("ELASTICSEARCH_INDEX", app_name)

queue_name = app_name + "_q"
app.config["CELERY_DEFAULT_QUEUE"] = queue_name
app.config["CELERY_QUEUES"] = (Queue(queue_name, Exchange(queue_name), routing_key=queue_name),)

celery = Celery(app_name, broker=app.config["CELERY_BROKER_URL"])
celery.config_from_object(app.config)

assets = Environment(app)

archive = open_archive(app.config.get("ARCHIVE_TYPE"), **app.config.get("ARCHIVE_CONFIG"))


def url_for(*a, **kw):
    try:
        kw["_external"] = True
        return _url_for(*a, **kw)
    except RuntimeError:
        return None