nopaque/app/__init__.py

164 lines
5.7 KiB
Python
Raw Permalink Normal View History

2024-05-04 13:14:21 +00:00
from apifairy import APIFairy
from config import Config
from docker import DockerClient
from flask import Flask
2024-09-26 12:45:05 +00:00
from flask.logging import default_handler
from flask_apscheduler import APScheduler
2022-05-05 13:10:03 +00:00
from flask_assets import Environment
from flask_login import LoginManager
2019-07-08 08:53:54 +00:00
from flask_mail import Mail
2024-05-04 13:14:21 +00:00
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_paranoid import Paranoid
2019-08-21 12:41:38 +00:00
from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy
2021-12-08 10:25:33 +00:00
from flask_hashids import Hashids
2024-09-26 12:45:05 +00:00
from logging import Formatter, StreamHandler
from werkzeug.middleware.proxy_fix import ProxyFix
2024-09-25 08:45:53 +00:00
docker_client = DockerClient.from_env()
2024-05-04 13:14:21 +00:00
apifairy = APIFairy()
assets = Environment()
db = SQLAlchemy()
hashids = Hashids()
login = LoginManager()
2024-05-04 13:14:21 +00:00
ma = Marshmallow()
mail = Mail()
migrate = Migrate(compare_type=True)
paranoid = Paranoid()
scheduler = APScheduler()
socketio = SocketIO()
2019-07-10 11:41:48 +00:00
2019-07-03 08:31:23 +00:00
2021-12-08 10:25:33 +00:00
def create_app(config: Config = Config) -> Flask:
2024-09-25 08:45:53 +00:00
''' Creates an initialized Flask object. '''
2023-05-15 10:00:13 +00:00
app = Flask(__name__)
2021-12-08 10:25:33 +00:00
app.config.from_object(config)
2024-09-25 08:45:53 +00:00
2024-09-26 12:45:05 +00:00
# region Logging
log_formatter = Formatter(
fmt=app.config['NOPAQUE_LOG_FORMAT'],
datefmt=app.config['NOPAQUE_LOG_DATE_FORMAT']
)
2024-09-25 08:45:53 +00:00
2024-09-26 12:45:05 +00:00
log_handler = StreamHandler()
log_handler.setFormatter(log_formatter)
log_handler.setLevel(app.config['NOPAQUE_LOG_LEVEL'])
2024-09-25 08:45:53 +00:00
2024-09-26 12:45:05 +00:00
app.logger.setLevel('DEBUG')
2024-09-25 08:45:53 +00:00
app.logger.removeHandler(default_handler)
2024-09-26 12:45:05 +00:00
app.logger.addHandler(log_handler)
# endregion Logging
2024-09-25 08:45:53 +00:00
2024-09-26 12:45:05 +00:00
# region Middlewares
if app.config['NOPAQUE_PROXY_FIX_ENABLED']:
2024-09-25 08:45:53 +00:00
app.wsgi_app = ProxyFix(
app.wsgi_app,
2024-09-26 12:45:05 +00:00
x_for=app.config['NOPAQUE_PROXY_FIX_X_FOR'],
x_host=app.config['NOPAQUE_PROXY_FIX_X_HOST'],
x_port=app.config['NOPAQUE_PROXY_FIX_X_PORT'],
x_prefix=app.config['NOPAQUE_PROXY_FIX_X_PREFIX'],
x_proto=app.config['NOPAQUE_PROXY_FIX_X_PROTO']
2024-09-25 08:45:53 +00:00
)
2024-09-26 12:45:05 +00:00
# endregion Middlewares
2024-09-25 08:45:53 +00:00
2024-09-26 12:45:05 +00:00
# region Extensions
docker_client.login(
2024-09-26 12:45:05 +00:00
app.config['NOPAQUE_DOCKER_REGISTRY_USERNAME'],
password=app.config['NOPAQUE_DOCKER_REGISTRY_PASSWORD'],
registry=app.config['NOPAQUE_DOCKER_REGISTRY']
)
2019-07-03 13:40:45 +00:00
2024-09-25 08:45:53 +00:00
from .models import AnonymousUser, User
2024-05-04 13:14:21 +00:00
apifairy.init_app(app)
assets.init_app(app)
db.init_app(app)
2021-12-08 10:25:33 +00:00
hashids.init_app(app)
login.init_app(app)
2024-09-25 08:45:53 +00:00
login.anonymous_user = AnonymousUser
login.login_view = 'auth.login'
2024-09-26 12:45:05 +00:00
login.user_loader(lambda user_id: User.query.get(int(user_id)))
2024-05-04 13:14:21 +00:00
ma.init_app(app)
2019-07-08 08:53:54 +00:00
mail.init_app(app)
migrate.init_app(app, db)
paranoid.init_app(app)
paranoid.redirect_view = '/'
2024-09-25 08:45:53 +00:00
scheduler.init_app(app)
2024-09-26 12:45:05 +00:00
socketio.init_app(app, message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI'])
# endregion Extensions
2024-09-26 12:45:05 +00:00
# region Blueprints
2024-09-30 11:30:13 +00:00
from .blueprints.admin import bp as admin_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(admin_blueprint, url_prefix='/admin')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.api import bp as api_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(api_blueprint, url_prefix='/api')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.auth import bp as auth_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(auth_blueprint)
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.contributions import bp as contributions_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(contributions_blueprint, url_prefix='/contributions')
2024-09-25 15:46:53 +00:00
2024-11-14 13:36:18 +00:00
from .blueprints.spacy_nlp_pipeline_models import bp as spacy_nlp_pipeline_models_blueprint
app.register_blueprint(spacy_nlp_pipeline_models_blueprint, url_prefix='/spacy-nlp-pipeline-models')
from .blueprints.tesseract_ocr_pipeline_models import bp as tesseract_ocr_pipeline_models_blueprint
app.register_blueprint(tesseract_ocr_pipeline_models_blueprint, url_prefix='/tesseract-ocr-pipeline-models')
from.blueprints.transkribus_htr_pipeline_models import bp as transkribus_htr_pipeline_models_blueprint
app.register_blueprint(transkribus_htr_pipeline_models_blueprint, url_prefix='/transkribus-htr-pipeline-models')
2024-09-30 11:30:13 +00:00
from .blueprints.corpora import bp as corpora_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(corpora_blueprint, cli_group='corpus', url_prefix='/corpora')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.errors import bp as errors_bp
2024-09-25 08:45:53 +00:00
app.register_blueprint(errors_bp)
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.jobs import bp as jobs_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(jobs_blueprint, url_prefix='/jobs')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.main import bp as main_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(main_blueprint, cli_group=None)
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.services import bp as services_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(services_blueprint, url_prefix='/services')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.settings import bp as settings_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(settings_blueprint, url_prefix='/settings')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.users import bp as users_blueprint
2024-09-25 08:45:53 +00:00
app.register_blueprint(users_blueprint, cli_group='user', url_prefix='/users')
2024-09-25 15:46:53 +00:00
2024-09-30 11:30:13 +00:00
from .blueprints.workshops import bp as workshops_blueprint
2023-07-25 13:18:57 +00:00
app.register_blueprint(workshops_blueprint, url_prefix='/workshops')
2024-09-26 12:45:05 +00:00
# endregion Blueprints
# region SocketIO Namespaces
from .namespaces.cqi_over_sio import CQiOverSocketIONamespace
socketio.on_namespace(CQiOverSocketIONamespace('/cqi_over_sio'))
from .namespaces.users import UsersNamespace
socketio.on_namespace(UsersNamespace('/users'))
2024-09-26 12:45:05 +00:00
# endregion SocketIO Namespaces
# region Database event Listeners
from .models.event_listeners import register_event_listeners
register_event_listeners()
# endregion Database event Listeners
# region Add scheduler jobs
if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']:
2024-09-30 11:30:13 +00:00
from .jobs import handle_corpora
scheduler.add_job('handle_corpora', handle_corpora, seconds=3, trigger='interval')
2024-09-30 11:30:13 +00:00
from .jobs import handle_jobs
scheduler.add_job('handle_jobs', handle_jobs, seconds=3, trigger='interval')
2024-09-26 12:45:05 +00:00
# endregion Add scheduler jobs
return app