Passed
Push — try-connexion3-alpha ( 6821e1...4902a8 )
by Juho
03:05
created

annif.create_app()   B

Complexity

Conditions 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nop 1
dl 0
loc 36
rs 8.9332
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
3
import logging
4
import os
5
import os.path
6
7
import connexion
8
from flask_cors import CORS
9
10
# from annif.openapi.validation import CustomRequestBodyValidator
11
12
logging.basicConfig()
13
logger = logging.getLogger("annif")
14
logger.setLevel(level=logging.INFO)
15
16
import annif.backend  # noqa
17
import annif.registry  # noqa
18
19
20
def create_app(config_name=None):
21
    # 'cxapp' here is the Connexion application that has a normal Flask app
22
    # as a property (cxapp.app)
23
24
    specdir = os.path.join(os.path.dirname(__file__), "openapi")
25
    cxapp = connexion.FlaskApp(__name__, specification_dir=specdir)
26
    if config_name is None:
27
        config_name = os.environ.get("ANNIF_CONFIG")
28
    if config_name is None:
29
        if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
30
            config_name = "annif.default_config.Config"
31
        else:
32
            config_name = "annif.default_config.ProductionConfig"
33
    logger.debug("creating app with configuration %s", config_name)
34
    cxapp.app.config.from_object(config_name)
35
    cxapp.app.config.from_envvar("ANNIF_SETTINGS", silent=True)
36
37
    # validator_map = {
38
    #     "body": CustomRequestBodyValidator,
39
    # }
40
    cxapp.add_api("annif.yaml")  # validator_map=validator_map)
41
42
    # add CORS support
43
    CORS(cxapp.app)
44
45
    if cxapp.app.config["INITIALIZE_PROJECTS"]:
46
        annif.registry.initialize_projects(cxapp.app)
47
        logger.info("finished initializing projects")
48
49
    # register the views via blueprints
50
    from annif.views import bp
51
52
    cxapp.app.register_blueprint(bp)
53
54
    # return the Connexion app
55
    return cxapp
56