-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (63 loc) · 1.95 KB
/
main.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
from flask import Flask
import sys
import logging
# api modules
from internal.http.api import API
from internal.http.views import Views
# storage connections
from internal.storage.minio import MinioConnector
from internal.storage.mysql import MySQL
# config module
from internal.config.config import Config
# set logging module
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
)
# load app configs
cfg = Config()
error, flag = cfg.load()
if flag: # if error occurs
logging.error(error)
sys.exit(-1)
# open connection to storages
# mysql
dbConnection = MySQL(
host=cfg.mysql['host'],
port=cfg.mysql['port'],
user=cfg.mysql['user'],
password=cfg.mysql['pass'],
database=cfg.mysql['name']
)
# minio
minioConnection = MinioConnector(
host=cfg.minio['host'],
access=cfg.minio['access'],
secret=cfg.minio['secret'],
secure=False,
)
# create a new flask application
app = Flask(__name__,
static_url_path='/',
static_folder='web/static',
template_folder='web/template')
# register blueprints
app.register_blueprint(API(dbConnection, minioConnection, f'{cfg.host}:{cfg.port}', cfg.private).get_blue_print())
app.register_blueprint(Views().get_blue_print())
if __name__ == "__main__":
# check mysql connection
if not dbConnection.ping():
logging.error("mysql connection failed!")
sys.exit(-2)
# check minio connection
errorM, flag = minioConnection.ping()
if flag:
logging.error(errorM)
sys.exit(-3)
# migrate database if needed
if cfg.mysql['migrate']:
from internal.utils.migrate import migrate
migrate(dbConnection.get_cursor())
logging.info(f"operator started on port: {cfg.port} ...")
app.run("127.0.0.1", cfg.port, debug=cfg.debug)