ogn-python/app/__init__.py

62 wiersze
1.5 KiB
Python
Czysty Zwykły widok Historia

2020-10-27 19:46:14 +00:00
import os
2019-02-10 12:10:19 +00:00
from flask import Flask
2019-02-10 13:00:35 +00:00
from flask_bootstrap import Bootstrap
2019-02-10 12:10:19 +00:00
from flask_sqlalchemy import SQLAlchemy
2019-04-27 11:33:36 +00:00
from flask_migrate import Migrate
2019-04-02 21:48:24 +00:00
from flask_caching import Cache
2019-03-10 14:58:10 +00:00
from celery import Celery
2020-05-30 12:28:30 +00:00
from flask_redis import FlaskRedis
from config import configs
2019-02-25 19:00:51 +00:00
2019-09-12 20:53:42 +00:00
bootstrap = Bootstrap()
db = SQLAlchemy()
2019-09-14 06:27:35 +00:00
migrate = Migrate()
2019-09-12 20:53:42 +00:00
cache = Cache()
2020-05-30 12:28:30 +00:00
redis_client = FlaskRedis()
celery = Celery()
2019-09-14 06:27:35 +00:00
2020-05-30 12:28:30 +00:00
def create_app(config_name='default'):
2019-09-12 20:53:42 +00:00
# Initialize Flask
app = Flask(__name__)
2019-02-10 12:10:19 +00:00
2019-09-12 20:53:42 +00:00
# Load the configuration
2020-05-30 12:28:30 +00:00
configuration = configs[config_name]
app.config.from_object(configuration)
2019-09-12 20:53:42 +00:00
app.config.from_envvar("OGN_CONFIG_MODULE", silent=True)
2020-11-22 07:55:19 +00:00
2019-09-12 20:53:42 +00:00
# Initialize other things
bootstrap.init_app(app)
db.init_app(app)
2019-09-14 06:27:35 +00:00
migrate.init_app(app, db)
2019-09-12 20:53:42 +00:00
cache.init_app(app)
2020-05-30 12:28:30 +00:00
redis_client.init_app(app)
2020-11-22 07:55:19 +00:00
2020-05-30 12:28:30 +00:00
init_celery(app)
2020-10-27 19:46:14 +00:00
register_blueprints(app)
2020-11-22 07:55:19 +00:00
2020-10-27 19:46:14 +00:00
return app
2020-11-22 07:55:19 +00:00
2020-10-27 19:46:14 +00:00
def register_blueprints(app):
2019-09-12 20:53:42 +00:00
from app.main import bp as bp_main
app.register_blueprint(bp_main)
2019-09-12 19:29:29 +00:00
2020-11-22 07:55:19 +00:00
2020-10-27 19:46:14 +00:00
def init_celery(app=None):
app = app or create_app(os.getenv('FLASK_CONFIG') or 'default')
celery.conf.broker_url = app.config['BROKER_URL']
2020-05-30 12:28:30 +00:00
celery.conf.result_backend = app.config['CELERY_RESULT_BACKEND']
celery.conf.update(app.config)
class ContextTask(celery.Task):
"""Make celery tasks work with Flask app context"""
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery