diff --git a/app.py b/app.py index b4a5e95..f7eaa96 100644 --- a/app.py +++ b/app.py @@ -7,19 +7,16 @@ import time +from gevent import pywsgi from flask import Flask from flask import request from flask import Response from flask import make_response from flask import send_from_directory -from bin.render_ import render_temp_ -from bin.b64img import re_sort_number_image -from bin.length import make_html -from wsgiref.simple_server import make_server -from db.db import ( - fetch_data, - update_data -) +from bin.core.render_ import render_temp_ +from bin.core.b64img import re_sort_number_image +from bin.core.length import make_html +from db.db import (fetch_data, update_data) app = Flask(__name__) @@ -39,13 +36,15 @@ def error_length(length: int) -> Response: return response -def too_lang_to_count() -> Response: +def too_lang_to_count(name: str) -> Response: """ 数据过长重置数据 + :param name: :return: """ + update_data(name, 0) response = make_response({'code': 200, - 'message': f'当前长度超过了预设的值'}) + 'message': f'当前长度超过了最大可计数长度:10. 已将重置该名称的计数器'}) response.headers['Content-Type'] = 'application/json; charset=utf-8' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['date'] = time.ctime() @@ -66,7 +65,7 @@ def arg_not_be_full() -> Response: return response -def build_page(name: str, length: int) -> Response | str | bool: +def build_page(name: str, length: int) -> list[Response] | list[bool | str] | list[bool]: """ 渲染最终的页面 :param name: @@ -75,14 +74,14 @@ def build_page(name: str, length: int) -> Response | str | bool: """ count = fetch_data(name) if len(str(count)) > length: - return too_lang_to_count() + return [False, too_lang_to_count(name)] if 7 <= length <= 10: make_html(length) zero_count = '0' * (length - len(str(count))) + str(count) sorted_image = re_sort_number_image(zero_count) - return render_temp_(length, name, sorted_image) + return [True, render_temp_(length, name, sorted_image)] else: - return False + return [False, 'BadLength'] @app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法 @@ -102,14 +101,16 @@ def api_page() -> Response | str: name = request.args['name'] if name != '': # 数据为空返回参数不完整界面 resp = build_page(name, length) - if resp: - response = make_response(resp) + if resp[0]: + response = make_response(resp[1]) response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['date'] = time.ctime() return response - else: + elif resp[1] == 'BadLength': return error_length(length) + else: + return resp[1] else: return arg_not_be_full() else: @@ -126,5 +127,10 @@ def index(): if __name__ == '__main__': - server = make_server('', 5000, app) - server.serve_forever() + # app.run(threaded=True) + print('服务器已在http://127.0.0.1:5000 运行. \n除了请求次数和名称会被保存到本地数据库中其余任何数据都不会被保存') + try: + server = pywsgi.WSGIServer(('0.0.0.0', 5000), app) + server.serve_forever() + except OSError: + print('5000 端口被占用') diff --git a/bin/conf/config.yml b/bin/conf/config.yml new file mode 100644 index 0000000..ebee452 --- /dev/null +++ b/bin/conf/config.yml @@ -0,0 +1,12 @@ +output: + # 日志等级 DEBUG INFO WARNING ERROR CRITICAL + level: INFO + # 日志是否开启颜色 True False + color: True + +servers: + # 地址 string + host: 0.0.0.0 + # 端口 integer + port: 5000 + diff --git a/bin/__init__.py b/bin/core/__init__.py similarity index 84% rename from bin/__init__.py rename to bin/core/__init__.py index 116422e..3944db4 100644 --- a/bin/__init__.py +++ b/bin/core/__init__.py @@ -2,5 +2,5 @@ # -- coding:utf-8 -- # @Author: markushammered@gmail.com # @Development Tool: PyCharm -# @Create Time: 2022/2/3 +# @Create Time: 2022/2/5 # @File Name: __init__.py.py diff --git a/bin/b64img.py b/bin/core/b64img.py similarity index 100% rename from bin/b64img.py rename to bin/core/b64img.py diff --git a/bin/length.py b/bin/core/length.py similarity index 100% rename from bin/length.py rename to bin/core/length.py diff --git a/bin/render_.py b/bin/core/render_.py similarity index 100% rename from bin/render_.py rename to bin/core/render_.py diff --git a/db/data.db b/db/data.db index 8f6994d..4af75fe 100644 Binary files a/db/data.db and b/db/data.db differ diff --git a/requirements.txt b/requirements.txt index 7aa5132..bfef310 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ requests~=2.27.1 -Flask~=2.0.2 \ No newline at end of file +Flask~=2.0.2 +colorlog~=6.6.0 +gevent~=21.12.0 \ No newline at end of file diff --git a/static/background.jpg b/static/background.jpg new file mode 100644 index 0000000..36f9c2c Binary files /dev/null and b/static/background.jpg differ diff --git a/static/index.html b/static/index.html index c8622b2..9bfeba6 100644 --- a/static/index.html +++ b/static/index.html @@ -7,6 +7,11 @@ .intro{ text-align: center; } + body{ + background-image: url("./static/background.jpg"); + background-repeat: no-repeat; + background-size: cover; + }