2022-02-08 19:42:36 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -- coding:utf-8 --
|
|
|
|
|
# @Author: markushammered@gmail.com
|
|
|
|
|
# @Development Tool: PyCharm
|
|
|
|
|
# @Create Time: 2022/2/3
|
|
|
|
|
# @File Name: app.py
|
|
|
|
|
|
|
|
|
|
|
2022-02-18 19:31:09 +08:00
|
|
|
import os
|
2022-02-08 19:42:36 +08:00
|
|
|
import time
|
2022-02-18 19:31:09 +08:00
|
|
|
from typing import Any
|
2022-02-08 19:42:36 +08:00
|
|
|
from flask import Flask
|
|
|
|
|
from flask import Response
|
2022-02-11 16:52:24 +08:00
|
|
|
from flask import request
|
2022-02-27 11:07:09 +08:00
|
|
|
from flask import make_response
|
2022-02-18 19:31:09 +08:00
|
|
|
from flask import send_from_directory
|
2022-02-11 16:52:24 +08:00
|
|
|
from gevent import pywsgi
|
2022-02-11 16:26:11 +08:00
|
|
|
from bin.utils.error import ErrorProcess
|
2022-02-18 19:31:09 +08:00
|
|
|
from bin.utils.view import view_template
|
2022-02-16 13:13:03 +08:00
|
|
|
from bin.utils.logger import logger
|
2022-02-23 22:03:25 +08:00
|
|
|
from bin.utils.packing_logs import make_targz
|
|
|
|
|
from bin.utils.settings import Settings
|
2022-02-25 18:27:28 +08:00
|
|
|
from bin.db.db import SQLite
|
2022-02-08 19:42:36 +08:00
|
|
|
|
2022-02-11 16:26:11 +08:00
|
|
|
app = Flask(__name__, static_url_path='')
|
2022-02-09 17:30:58 +08:00
|
|
|
app.config['JSON_SORT_KEYS'] = False # 设置JSON消息不根据字母顺序重新排序
|
|
|
|
|
app.config['JSON_AS_ASCII'] = False # 设置JSON消息显示中文
|
2022-02-08 19:42:36 +08:00
|
|
|
|
2022-02-26 14:12:57 +08:00
|
|
|
conf = Settings()
|
|
|
|
|
|
2022-02-08 19:42:36 +08:00
|
|
|
|
2022-02-18 19:31:09 +08:00
|
|
|
def build_page(name: str, length: int, theme: str) -> list[bool or Response] or list[bool or Any] or list[bool or str]:
|
2022-02-08 19:42:36 +08:00
|
|
|
"""
|
|
|
|
|
渲染最终的页面
|
|
|
|
|
:param theme:
|
|
|
|
|
:param name:
|
|
|
|
|
:param length:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-25 18:27:28 +08:00
|
|
|
count = SQLite().fetch_data(name)
|
2022-02-11 17:58:55 +08:00
|
|
|
if len(str(count)) > length: # 判断在数据库内的长度是否超过了设定的(或预设的)长度
|
2022-02-09 17:30:58 +08:00
|
|
|
return [False, ErrorProcess().too_lang_to_count(name)]
|
2022-02-11 17:58:55 +08:00
|
|
|
if 7 <= length <= 10: # 判断设定的长度是否超过阈值
|
2022-02-08 19:42:36 +08:00
|
|
|
zero_count = '0' * (length - len(str(count))) + str(count)
|
2022-02-18 19:31:09 +08:00
|
|
|
status, template = view_template(theme, length, name, zero_count)
|
2022-02-18 20:18:38 +08:00
|
|
|
if status is True:
|
2022-02-18 19:31:09 +08:00
|
|
|
return [True, template]
|
2022-02-18 20:30:17 +08:00
|
|
|
else:
|
2022-02-09 16:20:53 +08:00
|
|
|
return [False, 'BadTheme']
|
2022-02-08 19:42:36 +08:00
|
|
|
else:
|
|
|
|
|
return [False, 'BadLength']
|
|
|
|
|
|
|
|
|
|
|
2022-02-18 19:31:09 +08:00
|
|
|
@app.before_request
|
|
|
|
|
def requests_log() -> None:
|
|
|
|
|
"""
|
|
|
|
|
向日志文件内请求记录
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if request.path != '/favicon.ico': # 不记录favicon.ico的请求记录
|
2022-02-18 20:18:38 +08:00
|
|
|
logger.info(f'{request.host} {request.method} {request.full_path}')
|
2022-02-18 19:31:09 +08:00
|
|
|
|
|
|
|
|
file_list = os.listdir('./static/cache')
|
|
|
|
|
file_list.remove('.gitkeep')
|
|
|
|
|
for i in file_list:
|
|
|
|
|
os.remove(f'./static/cache/{i}')
|
|
|
|
|
|
2022-02-19 11:05:18 +08:00
|
|
|
origin_log_list = os.listdir('./bin/log')
|
|
|
|
|
origin_log_list.remove('.gitkeep')
|
2022-02-19 11:07:43 +08:00
|
|
|
if len(origin_log_list) > 100:
|
2022-02-19 11:05:18 +08:00
|
|
|
for d in origin_log_list:
|
|
|
|
|
try:
|
|
|
|
|
os.remove(f'./bin/log/{d}')
|
|
|
|
|
except PermissionError:
|
|
|
|
|
pass
|
|
|
|
|
|
2022-02-18 19:31:09 +08:00
|
|
|
|
2022-02-27 11:07:09 +08:00
|
|
|
@app.route('/db', methods=['GET', 'POST'])
|
2022-02-19 11:48:15 +08:00
|
|
|
def export_db():
|
|
|
|
|
"""
|
|
|
|
|
导出数据库
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
dir_path = os.path.join(app.root_path, 'bin/db')
|
2022-02-27 11:07:09 +08:00
|
|
|
return send_from_directory(dir_path, 'data.db', as_attachment=True)
|
2022-02-19 11:48:15 +08:00
|
|
|
|
|
|
|
|
|
2022-02-27 11:07:09 +08:00
|
|
|
@app.route('/log', methods=['GET', 'POST'])
|
2022-02-18 19:31:09 +08:00
|
|
|
def view_log() -> Response:
|
|
|
|
|
"""
|
|
|
|
|
下载日志
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
make_targz() # 打包tar.gz 文件
|
|
|
|
|
log_file = f'{time.strftime("%Y-%m-%d %H")}.tar.gz'
|
|
|
|
|
logger.warning(f'{request.host} {request.method} {request.path} -> Packed log files: {log_file}')
|
|
|
|
|
dir_path = os.path.join(app.root_path, 'static/cache')
|
|
|
|
|
return send_from_directory(dir_path, log_file, as_attachment=True)
|
|
|
|
|
|
|
|
|
|
|
2022-02-08 19:42:36 +08:00
|
|
|
@app.route('/get', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
2022-02-11 17:58:55 +08:00
|
|
|
def main() -> Response or str:
|
2022-02-08 19:42:36 +08:00
|
|
|
"""
|
|
|
|
|
API 页面函数
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
args = request.args
|
|
|
|
|
name = str(args.get('name')).replace('None', 'null')
|
|
|
|
|
theme = args.get('theme')
|
2022-02-18 20:30:17 +08:00
|
|
|
length = str(args.get('length')).replace('None', '7')
|
2022-02-18 20:18:38 +08:00
|
|
|
try:
|
|
|
|
|
length = int(length)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return ErrorProcess().error_length(length)
|
2022-02-09 16:20:53 +08:00
|
|
|
if theme == 'ls':
|
2022-02-09 17:30:58 +08:00
|
|
|
return ErrorProcess().get_theme_list()
|
2022-02-08 19:42:36 +08:00
|
|
|
if name and name != 'null':
|
|
|
|
|
if not theme:
|
2022-02-25 18:48:11 +08:00
|
|
|
theme = conf.default_style # 设置默认主题
|
2022-02-11 17:58:55 +08:00
|
|
|
build_page_result = build_page(name, length, theme) # 开始处理整体页面
|
2022-02-08 19:42:36 +08:00
|
|
|
if build_page_result[0]:
|
2022-02-11 17:58:55 +08:00
|
|
|
response = make_response(build_page_result[1]) # 设置响应体 和 响应头
|
2022-02-08 19:42:36 +08:00
|
|
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
2022-02-16 16:24:44 +08:00
|
|
|
# 防止浏览器和markdown编辑器缓存图片
|
2022-02-08 19:42:36 +08:00
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
response.headers['date'] = time.ctime()
|
|
|
|
|
return response
|
2022-02-16 16:24:44 +08:00
|
|
|
elif build_page_result[1] == 'BadLength': # 输入的长度错误
|
2022-02-09 17:30:58 +08:00
|
|
|
return ErrorProcess().error_length(length)
|
2022-02-16 16:24:44 +08:00
|
|
|
elif build_page_result[1] == 'BadTheme': # 输入的主题错误
|
2022-02-09 17:30:58 +08:00
|
|
|
return ErrorProcess().error_theme(theme)
|
2022-02-16 16:24:44 +08:00
|
|
|
else: # 长度过长无法计数重置数据库内的已有数据
|
2022-02-19 10:45:00 +08:00
|
|
|
logger.critical(f'用户: {name} 数据已被重置')
|
2022-02-08 19:42:36 +08:00
|
|
|
return build_page_result[1]
|
|
|
|
|
else:
|
2022-02-09 17:30:58 +08:00
|
|
|
return ErrorProcess().arg_not_be_full()
|
2022-02-08 19:42:36 +08:00
|
|
|
|
|
|
|
|
|
2022-02-11 16:26:11 +08:00
|
|
|
@app.route('/favicon.ico', methods=['GET', 'POST'])
|
|
|
|
|
def favicon() -> Response:
|
|
|
|
|
"""
|
|
|
|
|
主页图标
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
return app.send_static_file('favicon.ico')
|
|
|
|
|
|
|
|
|
|
|
2022-02-08 19:42:36 +08:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2022-02-10 13:35:32 +08:00
|
|
|
def index() -> Response:
|
2022-02-08 19:42:36 +08:00
|
|
|
"""
|
|
|
|
|
索引页面
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-11 16:26:11 +08:00
|
|
|
return app.send_static_file('index.html')
|
2022-02-08 19:42:36 +08:00
|
|
|
|
|
|
|
|
|
2022-02-16 17:07:17 +08:00
|
|
|
if __name__ == '__main__':
|
2022-02-23 22:03:25 +08:00
|
|
|
logger.info(f'服务器已在 http://127.0.0.1:{conf.port} 运行')
|
2022-02-16 17:07:17 +08:00
|
|
|
try:
|
2022-02-23 22:03:25 +08:00
|
|
|
server = pywsgi.WSGIServer((conf.host, conf.port), app, log=None) # log=None 关闭日志输出, 使用自写的日志器记录
|
2022-02-16 17:07:17 +08:00
|
|
|
server.serve_forever()
|
|
|
|
|
except OSError:
|
2022-02-23 22:03:25 +08:00
|
|
|
logger.error(f'{conf.port} 端口被占用')
|
2022-02-16 17:07:17 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
logger.info('程序已退出')
|