更新文档

This commit is contained in:
wojiaoyishang
2025-01-27 19:48:27 +08:00
parent 155af93c77
commit 5153daf7da
39 changed files with 767 additions and 593 deletions
+2 -1
View File
@@ -10,6 +10,7 @@ from .main import helloworld_blueprint
dir_path = os.path.dirname(__file__).replace("\\", "/")
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
def event_init(app: Flask):
"""初始化完成时会调用这里"""
app.register_blueprint(helloworld_blueprint)
app.register_blueprint(helloworld_blueprint)
+5 -3
View File
@@ -1,10 +1,12 @@
from flask import render_template, Blueprint
# 创建蓝图
helloworld_blueprint = Blueprint('hello_world', __name__, template_folder='templates', static_folder="static",
url_prefix="/hello_world")
helloworld_blueprint = Blueprint('hello_world', __name__,
template_folder='templates',
static_folder="static",
url_prefix="/hello_world")
@helloworld_blueprint.route("/")
def index():
return render_template("helloworld_index.html")
+5 -22
View File
@@ -4,38 +4,21 @@
import os
import logging
from flask import Flask, request
from . import console
# 获取插件所在的目录(结尾没有分割符号)
dir_path = os.path.dirname(__file__).replace("\\", "/")
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
def event_init(app: Flask):
"""初始化完成时会调用这里"""
# 移除原有的输出日志
app.logger = None
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# 更改IP地址,只有在最新版的flask中才能生效
@app.before_request
def before_request():
request.remote_addr = get_user_ip(request)
# 使用自定义的日志输出
@app.after_request
def after_request(rep):
if rep.status_code == 200:
console.success(f"{request.remote_addr} -- {request.full_path} 200")
elif rep.status_code == 404:
console.error(f"{request.remote_addr} -- {request.full_path} 404")
elif rep.status_code == 500:
console.warning(f"{request.remote_addr} -- {request.full_path} 500")
else:
console.info(f"{request.remote_addr} -- {request.full_path} {rep.status_code}")
return rep
def get_user_ip(request):
"""获取用户真实IP"""
if 'HTTP_X_FORWARDED_FOR' in request.headers:
@@ -54,4 +37,4 @@ def get_user_ip(request):
return request.headers['REMOTE_ADDR']
elif 'X-Forwarded-For' in request.headers:
return request.headers['X-Forwarded-For']
return request.remote_addr
return request.remote_addr
-89
View File
@@ -1,89 +0,0 @@
"""
输出控制台日志
"""
import sys
import time
import ctypes
NONE = "\033[m"
RED = "\033[0;32;31m"
LIGHT_RED = "\033[1;31m"
GREEN = "\033[0;32;32m"
LIGHT_GREEN = "\033[1;32m"
BLUE = "\033[0;32;34m"
LIGHT_BLUE = "\033[1;34m"
DARY_GRAY = "\033[1;30m"
CYAN = "\033[0;36m"
LIGHT_CYAN = "\033[1;36m"
PURPLE = "\033[0;35m"
LIGHT_PURPLE = "\033[1;35m"
BROWN = "\033[0;33m"
YELLOW = "\033[1;33m"
LIGHT_GRAY = "\033[0;37m"
WHITE = "\033[1;37m"
# 开启 Windows 下对于 ESC控制符 的支持
if sys.platform == "win32":
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
def _print(level, msg):
time_ = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
level_name = {10: "Plain",
11: "Log",
12: "Info",
13: "Debug",
14: "Success",
15: "Warning",
16: "Error"}
color = {10: NONE,
11: LIGHT_CYAN,
12: LIGHT_BLUE,
13: PURPLE,
14: GREEN,
15: YELLOW,
16: RED}
print(f'{color.get(level, NONE)}[{time_}]({level_name.get(level, "Plain")}):', msg, f"{NONE}")
def plain(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(10, msg)
def log(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(11, msg)
def info(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(12, msg)
def debug(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(13, msg)
def success(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(14, msg)
def warn(*args):
warning(*args)
def warning(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(15, msg)
def error(*args, sep=' '):
msg = sep.join(str(_) for _ in args)
_print(16, msg)