__init__.py 3.14 KB
Newer Older
1 2 3
"""
Entry file for the delayed jobs app
"""
4 5
from flask import Flask
from flask_cors import CORS
David Mendez's avatar
David Mendez committed
6

7 8 9 10
from app.blueprints.admin.controllers.admin_tasks_controller import ADMIN_TASKS_BLUEPRINT
from app.blueprints.admin.controllers.authorisation_controller import ADMIN_AUTH_BLUEPRINT
from app.blueprints.job_status.controllers.job_status_controller import JOB_STATUS_BLUEPRINT
from app.blueprints.job_submission.controllers.job_submissions_controller import SUBMISSION_BLUEPRINT
11
from app.blueprints.custom_statistics.controllers.custom_statistics_controller import CUSTOM_STATISTICS_BLUEPRINT
12 13
from app.blueprints.job_submission.services import job_submission_service
from app.blueprints.swagger_description.swagger_description_blueprint import SWAGGER_BLUEPRINT
David Mendez's avatar
David Mendez committed
14
from app.config import RUN_CONFIG
15
from app.config import RunEnvs
16
from app.db import DB
17
from app.models import delayed_job_models
18
from app.cache import CACHE
19
from app.rate_limiter import RATE_LIMITER
20

21

David Mendez's avatar
David Mendez committed
22
def create_app():
David Mendez's avatar
David Mendez committed
23 24 25 26
    """
    Creates the flask app
    :return: Delayed jobs flask app
    """
David Mendez's avatar
David Mendez committed
27

28
    base_path = RUN_CONFIG.get('base_path', '')
29 30 31 32 33
    outputs_base_path = RUN_CONFIG.get('outputs_base_path', 'outputs')
    flask_app = Flask(__name__,
                      static_url_path=f'{base_path}/{outputs_base_path}',
                      static_folder=job_submission_service.JOBS_OUTPUT_DIR)

David Mendez's avatar
David Mendez committed
34
    # flask_app.config['SERVER_NAME'] = RUN_CONFIG.get('server_public_host')
David Mendez's avatar
David Mendez committed
35 36
    flask_app.config['SQLALCHEMY_DATABASE_URI'] = RUN_CONFIG.get('sql_alchemy').get('database_uri')
    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = RUN_CONFIG.get('sql_alchemy').get('track_modifications')
37
    flask_app.config['SECRET_KEY'] = RUN_CONFIG.get('server_secret_key')
38 39 40 41 42

    enable_cors = RUN_CONFIG.get('enable_cors', False)

    if enable_cors:
        CORS(flask_app)
David Mendez's avatar
David Mendez committed
43 44

    with flask_app.app_context():
David Mendez's avatar
David Mendez committed
45
        DB.init_app(flask_app)
46
        CACHE.init_app(flask_app)
David Mendez's avatar
David Mendez committed
47
        RATE_LIMITER.init_app(flask_app)
48

49 50 51
        for handler in flask_app.logger.handlers:
            RATE_LIMITER.logger.addHandler(handler)

52 53
        create_tables = RUN_CONFIG.get('sql_alchemy').get('create_tables', False)
        if create_tables:
54
            print('CREATING TABLES!')
David Mendez's avatar
David Mendez committed
55
            DB.create_all()
56
            print('TABLES CREATED!')
57

58 59
        generate_default_config = RUN_CONFIG.get('generate_default_config', False)
        if generate_default_config:
60
            print('GENERATING DEFAULT JOB CONFIG')
61
            delayed_job_models.generate_default_job_configs()
62
            print('DEFAULT JOB CONFIG GENERATED!')
63

64
        flask_app.register_blueprint(SWAGGER_BLUEPRINT, url_prefix=f'{base_path}/swagger')
65
        flask_app.register_blueprint(SUBMISSION_BLUEPRINT, url_prefix=f'{base_path}/submit')
David Mendez's avatar
David Mendez committed
66
        flask_app.register_blueprint(JOB_STATUS_BLUEPRINT, url_prefix=f'{base_path}/status')
67
        flask_app.register_blueprint(ADMIN_AUTH_BLUEPRINT, url_prefix=f'{base_path}/admin')
68
        flask_app.register_blueprint(ADMIN_TASKS_BLUEPRINT, url_prefix=f'{base_path}/admin')
69
        flask_app.register_blueprint(CUSTOM_STATISTICS_BLUEPRINT, url_prefix=f'{base_path}/custom_statistics')
70

David Mendez's avatar
David Mendez committed
71 72
        return flask_app

73

David Mendez's avatar
David Mendez committed
74
if __name__ == '__main__':
David Mendez's avatar
David Mendez committed
75
    flask_app = create_app()