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-03-19 08:48:59 +08:00
|
|
|
from gevent import pywsgi
|
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-28 21:18:05 +08:00
|
|
|
from flask import render_template
|
2022-02-16 13:13:03 +08:00
|
|
|
from bin.utils.logger import logger
|
2022-03-13 10:52:16 +08:00
|
|
|
from bin.utils.features import Features
|
2022-02-23 22:03:25 +08:00
|
|
|
from bin.utils.settings import Settings
|
2022-03-13 10:52:16 +08:00
|
|
|
from bin.utils.error import ErrorProcess
|
|
|
|
|
from bin.utils.view import view_template
|
|
|
|
|
|
2022-02-26 14:12:57 +08:00
|
|
|
conf = Settings()
|
|
|
|
|
|
2022-03-02 21:51:38 +08:00
|
|
|
if conf.type.lower() == 'mysql':
|
2022-02-28 21:18:05 +08:00
|
|
|
from bin.db.db import MySQL as db
|
|
|
|
|
else:
|
|
|
|
|
from bin.db.db import SQLite as db
|
2022-02-08 19:42:36 +08:00
|
|
|
|
2022-03-01 20:51:50 +08:00
|
|
|
app = Flask(__name__, static_url_path='')
|
2022-02-28 21:18:05 +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-18 19:31:09 +08:00
|
|
|
@app.before_request
|
|
|
|
|
def requests_log() -> None:
|
|
|
|
|
"""
|
|
|
|
|
向日志文件内请求记录
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if request.path != '/favicon.ico': # 不记录favicon.ico的请求记录
|
2022-02-28 21:18:05 +08:00
|
|
|
logger.info(f'{request.remote_addr} {request.method} {request.base_url}')
|
2022-02-19 11:05:18 +08:00
|
|
|
|
2022-02-18 19:31:09 +08:00
|
|
|
|
2022-03-05 17:01:51 +08:00
|
|
|
@app.route('/count/<string:name>', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
|
|
|
|
def main(name: str) -> Response or str:
|
2022-02-08 19:42:36 +08:00
|
|
|
"""
|
|
|
|
|
API 页面函数
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
args = request.args
|
2022-03-01 22:33:08 +08:00
|
|
|
theme = args.get('theme', type=str)
|
|
|
|
|
length = args.get('length', type=int)
|
2022-02-28 21:18:05 +08:00
|
|
|
if not bool(theme): # 判断主题是否存在查询参数内如果不存在则使用配置文件内的默认主题
|
|
|
|
|
theme = conf.default_style
|
|
|
|
|
elif theme == 'ls':
|
|
|
|
|
return Features(db).theme_list()
|
|
|
|
|
if not bool(length):
|
|
|
|
|
length = 7
|
|
|
|
|
if not db().exists_name(name):
|
|
|
|
|
db().insert(name)
|
|
|
|
|
count = db().fetch(name)
|
2022-03-01 22:33:08 +08:00
|
|
|
if not db().exists_table(theme):
|
2022-02-28 21:18:05 +08:00
|
|
|
return ErrorProcess(db).theme_error(theme)
|
|
|
|
|
if 7 <= int(length) <= 10: # 限定自定义长度阈值
|
|
|
|
|
view_number = '0' * (int(length) - len(str(count[1]))) + str(count[1])
|
|
|
|
|
response = make_response(view_template(theme, int(length), name, view_number, db)) # 设置响应体 和 响应头
|
|
|
|
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
|
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
return response
|
2022-02-08 19:42:36 +08:00
|
|
|
else:
|
2022-02-28 21:18:05 +08:00
|
|
|
return ErrorProcess(db).length_error(length)
|
2022-02-11 16:26:11 +08:00
|
|
|
|
|
|
|
|
|
2022-02-08 19:42:36 +08:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2022-02-28 21:18:05 +08:00
|
|
|
def home() -> str:
|
2022-02-08 19:42:36 +08:00
|
|
|
"""
|
2022-02-28 21:18:05 +08:00
|
|
|
主页面
|
2022-02-08 19:42:36 +08:00
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-28 21:18:05 +08:00
|
|
|
return render_template('index.html', remote_address=request.remote_addr)
|
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-03-02 21:51:38 +08:00
|
|
|
logger.critical(f'{conf.port} 端口被占用 已退出')
|
2022-02-16 17:07:17 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
logger.info('程序已退出')
|