forked from baas-pro/baas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (81 loc) · 3.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import logging
import multiprocessing
import os
import sys
import threading
import time
import requests
from flask import Flask
import launcher
from common import process, config, app
from web.baas import baas
from web.configs import configs
is_exit = False
def run_flask():
global is_exit
f = Flask(__name__, static_folder='web/static', static_url_path='/static')
f.register_blueprint(baas)
f.register_blueprint(configs)
ac = config.get_app_config()
try:
f.run(debug=False, port=ac['port'], host='0.0.0.0')
except UnicodeDecodeError as e:
# Handle decoding errors specifically
is_exit = True
print("服务启动失败", e)
for i in range(3):
print(
"电脑设备名称不能有中文或特殊符号,点击Win开始->设置->系统->关于->重命名这台电脑!使用纯英文命名然后重启电脑!")
sys.exit(-1)
except Exception as e:
is_exit = True
print("服务启动失败: ", str(e))
sys.exit(-1)
def check_flask_startup():
global is_exit
time.sleep(1)
ac = config.get_app_config()
url = f"http://127.0.0.1:{ac['port']}/ping"
for i in range(3):
if is_exit:
return
try:
response = requests.get(url, timeout=2)
if response.status_code == 200:
launcher.print_title("启动成功", "")
app.open_baas()
break
if i == 2:
launcher.print_title("启动失败",
f"Baas启动失败...当前运行端口为:{ac['port']}\n如果端口冲突可以打开configs/app.txt 修改port 为 7111 或 7112依次类推")
except Exception as e:
if i == 2:
launcher.print_title("启动失败",
f"Baas启动失败...当前运行端口为:{ac['port']}\n如果端口冲突可以打开configs/app.txt 修改port 为 7111 或 7112依次类推")
print(e)
time.sleep(1)
def check_source():
"""
windows平台检查启动来源
"""
if hasattr(sys, 'frozen') and sys.platform != 'darwin':
source_arg_value = next((arg.split('=')[1] for arg in sys.argv if arg.startswith('source=')), None)
if source_arg_value != "launcher":
print("必须从启动器Baas_Windows启动Baas脚本")
print("如果你是第一次遇到这个错误,请从QQ群重新下载最新启动器覆盖原来的启动器。")
print("不需要重新下载和安装")
launcher.wait()
sys.exit(1)
if __name__ == '__main__':
main_process_pid = os.getpid()
multiprocessing.freeze_support()
process.manager = multiprocessing.Manager()
process.processes_task = process.manager.dict()
if os.getpid() == main_process_pid:
check_source()
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
check_thread = threading.Thread(target=check_flask_startup)
check_thread.daemon = True
check_thread.start()
run_flask()