-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
121 lines (98 loc) · 3.42 KB
/
app.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
# -*- coding: utf-8 -*-
#
# This file is part of SKALE Admin
#
# Copyright (C) 2019 SKALE Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import binascii
import logging
import os
import time
from http import HTTPStatus
import werkzeug
from flask import Flask, g
from core.node_config import NodeConfig
from tools.configs import FLASK_SECRET_KEY_FILE, SGX_SERVER_URL
from tools.configs.flask import (
FLASK_APP_HOST,
FLASK_APP_PORT,
FLASK_DEBUG_MODE
)
from tools.configs.web3 import ENDPOINT
from tools.docker_utils import DockerUtils
from tools.helper import wait_until_admin_inited
from tools.logger import init_api_logger
from tools.resources import get_database, REDIS_URI
from tools.str_formatters import arguments_list_string
from web.routes.node import node_bp
from web.routes.schains import schains_bp
from web.routes.wallet import wallet_bp
from web.routes.ssl import ssl_bp
from web.routes.health import health_bp
from web.helper import construct_err_response
REQ_ID_SIZE = 10
init_api_logger()
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.register_blueprint(node_bp)
app.register_blueprint(schains_bp)
app.register_blueprint(wallet_bp)
app.register_blueprint(ssl_bp)
app.register_blueprint(health_bp)
@app.before_request
def before_request():
wait_until_admin_inited()
g.request_start_time = time.time()
g.config = NodeConfig()
g.request_id = binascii.b2a_hex(
os.urandom(REQ_ID_SIZE // 2)
).decode('utf-8')
g.db = get_database()
g.db.connect(reuse_if_open=True)
g.docker_utils = DockerUtils()
logger.info(f'Processing request {g.request_id}')
@app.teardown_request
def teardown_request(response):
elapsed = int(time.time() - g.request_start_time)
logger.info(f'Request finished {g.request_id}, time elapsed: {elapsed}s')
if not g.db.is_closed():
g.db.close()
return response
@app.errorhandler(RecursionError)
def recursion_error_handler(e):
return construct_err_response(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
msg='Unexpected RecursionError in API, try again'
)
@app.errorhandler(werkzeug.exceptions.InternalServerError)
def any_error_handler(e):
original = getattr(e, "original_exception", None)
logger.exception('Request failed with error %s', original)
return construct_err_response(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
msg=str(e)
)
app.secret_key = FLASK_SECRET_KEY_FILE
app.use_reloader = False
logger.info('Starting api ...')
def main():
logger.info(arguments_list_string({
'Endpoint': ENDPOINT,
'Redis uri': REDIS_URI,
'SGX Server': SGX_SERVER_URL or 'Not connected'
}, 'Starting Flask server'))
app.run(debug=FLASK_DEBUG_MODE, port=FLASK_APP_PORT, host=FLASK_APP_HOST)
if __name__ == '__main__':
main()