-
Notifications
You must be signed in to change notification settings - Fork 88
/
__init__.py
151 lines (133 loc) · 6.55 KB
/
__init__.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import ast
import importlib
import logging
import logging.config
import os
import sys
from typing import Any, Dict # noqa: F401
from flasgger import Swagger
from flask import Blueprint, Flask
from flask_cors import CORS
from flask_restful import Api
from metadata_service.api.badge import BadgeAPI
from metadata_service.api.column import (ColumnBadgeAPI, ColumnDescriptionAPI,
ColumnLineageAPI)
from metadata_service.api.dashboard import (DashboardBadgeAPI,
DashboardDescriptionAPI,
DashboardDetailAPI,
DashboardTagAPI)
from metadata_service.api.healthcheck import healthcheck
from metadata_service.api.popular_tables import PopularTablesAPI
from metadata_service.api.system import Neo4jDetailAPI, StatisticsMetricsAPI
from metadata_service.api.table import (TableBadgeAPI, TableDashboardAPI,
TableDescriptionAPI, TableDetailAPI,
TableLineageAPI, TableOwnerAPI,
TableTagAPI)
from metadata_service.api.tag import TagAPI
from metadata_service.api.user import (UserDetailAPI, UserFollowAPI,
UserFollowsAPI, UserOwnAPI, UserOwnsAPI,
UserReadsAPI)
# For customized flask use below arguments to override.
FLASK_APP_MODULE_NAME = os.getenv('FLASK_APP_MODULE_NAME')
FLASK_APP_CLASS_NAME = os.getenv('FLASK_APP_CLASS_NAME')
FLASK_APP_KWARGS_DICT_STR = os.getenv('FLASK_APP_KWARGS_DICT')
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# Environment Variable to enable cors
CORS_ENABLED = os.environ.get('CORS_ENABLED', False)
def create_app(*, config_module_class: str) -> Flask:
"""
Creates app in function so that flask with flask extensions can be
initialized with specific config. Here it defines the route of APIs
so that it can be seen in one place where implementation is separated.
Config is being fetched via module.class name where module.class name
can be passed through environment variable.
This is to make config fetched through runtime PYTHON_PATH so that
Config class can be easily injected.
More on: http://flask.pocoo.org/docs/1.0/config/
:param config_module_class: name of the config (TODO: Implement config.py)
:return: Flask
"""
if FLASK_APP_MODULE_NAME and FLASK_APP_CLASS_NAME:
print('Using requested Flask module {module_name} and class {class_name}'
.format(module_name=FLASK_APP_MODULE_NAME, class_name=FLASK_APP_CLASS_NAME), file=sys.stderr)
class_obj = getattr(importlib.import_module(FLASK_APP_MODULE_NAME), FLASK_APP_CLASS_NAME)
flask_kwargs_dict = {} # type: Dict[str, Any]
if FLASK_APP_KWARGS_DICT_STR:
print('Using kwargs {kwargs} to instantiate Flask'.format(kwargs=FLASK_APP_KWARGS_DICT_STR),
file=sys.stderr)
flask_kwargs_dict = ast.literal_eval(FLASK_APP_KWARGS_DICT_STR)
app = class_obj(__name__, **flask_kwargs_dict)
else:
app = Flask(__name__)
if CORS_ENABLED:
CORS(app)
config_module_class = \
os.getenv('METADATA_SVC_CONFIG_MODULE_CLASS') or config_module_class
app.config.from_object(config_module_class)
if app.config.get('LOG_CONFIG_FILE'):
logging.config.fileConfig(app.config.get('LOG_CONFIG_FILE'), disable_existing_loggers=False)
else:
logging.basicConfig(format=app.config.get('LOG_FORMAT'), datefmt=app.config.get('LOG_DATE_FORMAT'))
logging.getLogger().setLevel(app.config.get('LOG_LEVEL'))
logging.info('Created app with config name {}'.format(config_module_class))
logging.info('Using backend {}'.format(app.config.get('PROXY_CLIENT')))
api_bp = Blueprint('api', __name__)
api_bp.add_url_rule('/healthcheck', 'healthcheck', healthcheck)
api = Api(api_bp)
api.add_resource(PopularTablesAPI,
'/popular_tables/',
'/popular_tables/<path:user_id>')
api.add_resource(TableDetailAPI, '/table/<path:table_uri>')
api.add_resource(TableDescriptionAPI,
'/table/<path:id>/description')
api.add_resource(TableTagAPI,
'/table/<path:id>/tag/<tag>')
api.add_resource(TableBadgeAPI,
'/table/<path:id>/badge/<badge>')
api.add_resource(TableLineageAPI,
'/table/<path:id>/lineage')
api.add_resource(TableOwnerAPI,
'/table/<path:table_uri>/owner/<owner>')
api.add_resource(TableDashboardAPI,
'/table/<path:id>/dashboard/')
api.add_resource(ColumnDescriptionAPI,
'/table/<path:table_uri>/column/<column_name>/description')
api.add_resource(ColumnBadgeAPI,
'/table/<path:table_uri>/column/<column_name>/badge/<badge>')
api.add_resource(ColumnLineageAPI,
'/table/<path:table_uri>/column/<column_name>/lineage')
api.add_resource(Neo4jDetailAPI,
'/latest_updated_ts')
api.add_resource(StatisticsMetricsAPI,
'/system/statistics')
api.add_resource(TagAPI,
'/tags/')
api.add_resource(BadgeAPI,
'/badges/')
api.add_resource(UserDetailAPI,
'/user',
'/user/<path:id>')
api.add_resource(UserFollowsAPI,
'/user/<path:user_id>/follow/')
api.add_resource(UserFollowAPI,
'/user/<path:user_id>/follow/<resource_type>/<path:resource_id>')
api.add_resource(UserOwnsAPI,
'/user/<path:user_id>/own/')
api.add_resource(UserOwnAPI,
'/user/<path:user_id>/own/<resource_type>/<path:table_uri>')
api.add_resource(UserReadsAPI,
'/user/<path:user_id>/read/')
api.add_resource(DashboardDetailAPI,
'/dashboard/<path:id>')
api.add_resource(DashboardDescriptionAPI,
'/dashboard/<path:id>/description')
api.add_resource(DashboardTagAPI,
'/dashboard/<path:id>/tag/<tag>')
api.add_resource(DashboardBadgeAPI,
'/dashboard/<path:id>/badge/<badge>')
app.register_blueprint(api_bp)
if app.config.get('SWAGGER_ENABLED'):
Swagger(app, template_file=os.path.join(ROOT_DIR, app.config.get('SWAGGER_TEMPLATE_PATH')), parse=True)
return app