代码拉取完成,页面将自动刷新
from gevent import monkey
from gevent.pywsgi import WSGIServer
from werkzeug.middleware.proxy_fix import ProxyFix
monkey.patch_all()
from multiprocessing import cpu_count, Process
from flask import Flask, render_template
from loguru import logger
from os.path import exists, join, abspath
import sys
from json import dumps, loads
import time
import config
import mcMonitor.McServerQuery as McServerQuery
@logger.catch
def create_app():
# 获取静态资源路径
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return join(sys._MEIPASS, relative_path)
return join(abspath("."), relative_path)
app = Flask(__name__, static_url_path="", static_folder=resource_path("static"), template_folder=resource_path("templates"))
register_blueprints(app)
custom_error(app)
# CORS(app, supports_credentials=True)
app.config['DEBUG'] = config.AppConfig.DEBUG
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024
app.wsgi_app = ProxyFix(app.wsgi_app)
return app
@logger.catch
def register_blueprints(app):
app.register_blueprint(McServerQuery.McServerQuery, url_prefix="/")
@logger.catch
def custom_error(app):
@app.errorhandler(404)
def miss(e):
return render_template('404.html')
@app.errorhandler(500)
def error(e):
return render_template('500.html')
@app.errorhandler(413)
def error(e):
return render_template('413.html')
@logger.catch
def run(MULTI_PROCESS):
if not MULTI_PROCESS:
logger.success(f"服务启动 http://{'127.0.0.1' if config.AppConfig.HOST == '0.0.0.0' else config.AppConfig.HOST}:{config.AppConfig.PORT}/")
WSGIServer((config.AppConfig.HOST, config.AppConfig.PORT), create_app()).serve_forever()
else:
mulserver = WSGIServer((config.AppConfig.HOST, config.AppConfig.PORT), create_app())
mulserver.start()
def server_forever():
mulserver.start_accepting()
mulserver._stop_event.wait()
for i in range(cpu_count()):
p = Process(target=server_forever)
p.start()
@logger.catch
def check_config():
if exists("config.json"):
with open("config.json", "r", encoding="utf8") as fp:
data = fp.read()
try:
data_obj = loads(data)
config.ServerList.info = data_obj["McServerList"]
config.ServerList.server_conf = data_obj["ServerConfig"]
config.AppConfig.HOST = data_obj["ServerConfig"]["ListenHost"]
config.AppConfig.PORT = data_obj["ServerConfig"]["ListenPort"]
config.AppConfig.DEBUG = data_obj["ServerConfig"]["DEBUG"]
config.AppConfig.SECRET_KEY = data_obj["ServerConfig"]["SECRET_KEY"]
# config.AppConfig.HIGH_CONCURRENCY = data_obj["ServerConfig"]["HIGH_CONCURRENCY"]
return True
except Exception as e:
logger.error("config.json配置文件格式错误。")
return False
else:
logger.info("未读取到配置文件,您可能为第一次启动。")
with open("config.json", "w", encoding="utf8") as fp:
fp.write(dumps({
"ServerConfig": config.ServerList.server_conf,
"McServerList": config.ServerList.info
}, ensure_ascii=False, indent=4))
logger.success("已自动生成初始配置文件,请编辑保存后再启动。")
time.sleep(3)
return False
if __name__ == "__main__":
logger.add("McBetterMonitor.log", level="ERROR")
if check_config():
if config.AppConfig.HIGH_CONCURRENCY:
# 多进程 + 协程
run(True)
else:
# 单进程 + 协程
run(False)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。