Passed
Push — master ( aa5409...f8814e )
by Mingyu
01:23
created

app   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 31
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A connect_databases() 0 3 1
A register_blueprints() 0 11 1
A register_extensions() 0 7 1
A create_app() 0 13 2
1
from flask import Flask
2
from redis import Redis
3
4
from mongoengine import connect
5
6
from app import extensions
7
from app.views import api_v1_blueprint
8
9
# -- API load
10
from app.views import sample
11
# --
12
13
14
def register_extensions(app):
15
    extensions.swagger.template = app.config['SWAGGER_TEMPLATE']
16
17
    extensions.cors.init_app(app)
18
    extensions.jwt.init_app(app)
19
    extensions.validator.init_app(app)
20
    extensions.swagger.init_app(app)
21
22
23
def connect_databases(app):
24
    connect(**app.config['MONGODB_SETTINGS'])
25
    app.config['REDIS_CLIENT'] = Redis(**app.config['REDIS_SETTINGS'])
26
27
28
def register_blueprints(app):
29
    handle_exception_func = app.handle_exception
30
    handle_user_exception_func = app.handle_user_exception
31
    # register_blueprint 시 defer되었던 함수들이 호출되며, flask-restful.Api._init_app()이 호출되는데
32
    # 해당 메소드가 app 객체의 에러 핸들러를 오버라이딩해서, 별도로 적용한 handler의 HTTPException 관련 로직이 동작하지 않음
33
    # 따라서 두 함수를 임시 저장해 두고, register_blueprint 이후 함수를 재할당하도록 함
34
35
    app.register_blueprint(api_v1_blueprint)
36
37
    app.handle_exception = handle_exception_func
38
    app.handle_user_exception = handle_user_exception_func
39
40
41
def create_app(*config_cls) -> Flask:
42
    print('[INFO] Flask application initialized with {}'.format([config.__name__ for config in config_cls]))
43
44
    app = Flask(__name__)
45
46
    for config in config_cls:
47
        app.config.from_object(config)
48
49
    register_extensions(app)
50
    connect_databases(app)
51
    register_blueprints(app)
52
53
    return app
54