aboutsummaryrefslogtreecommitdiff
path: root/qc_app/__init__.py
blob: b40ec462c870806496cdb7516eac1b3855d968c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""The Quality-Control Web Application entry point"""

import os

from flask import Flask

from .entry import entrybp
from .upload import upload
from .parse import parsebp
from .samples import samples
from .dbinsert import dbinsertbp
from .errors import register_error_handlers

def override_settings_with_envvars(
        app: Flask, ignore: tuple[str, ...]=tuple()) -> None:
    """Override settings in `app` with those in ENVVARS"""
    for setting in (key for key in app.config if key not in ignore):
        app.config[setting] = os.environ.get(setting) or app.config[setting]

def create_app():
    """The application factory"""
    app = Flask(__name__)
    app.config.from_pyfile(os.path.join(os.getcwd(), "etc/default_config.py"))
    if "QCAPP_CONF" in os.environ:
        app.config.from_envvar("QCAPP_CONF") # Override defaults with instance path

    override_settings_with_envvars(app, ignore=tuple())

    if "QCAPP_SECRETS" in os.environ:
        app.config.from_envvar("QCAPP_SECRETS")

    # setup blueprints
    app.register_blueprint(entrybp, url_prefix="/")
    app.register_blueprint(parsebp, url_prefix="/parse")
    app.register_blueprint(upload, url_prefix="/upload")
    app.register_blueprint(dbinsertbp, url_prefix="/dbinsert")
    app.register_blueprint(samples, url_prefix="/samples")

    register_error_handlers(app)
    return app