1 Star 3 Fork 0

Gu-f/MC Better Monitor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.py 3.67 KB
一键复制 编辑 原始数据 按行查看 历史
Gu-f 提交于 2021-12-31 00:49 . [fix]: 修复linux下生成二进制错误
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)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gu-f/mc-better-monitor.git
git@gitee.com:gu-f/mc-better-monitor.git
gu-f
mc-better-monitor
MC Better Monitor
master

搜索帮助