From ce009e0203b39bfcab64387f61150a2108da1661 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 22 Dec 2022 18:10:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug=E5=B9=B6=E5=B0=86=E5=89=A9=E4=BD=99=E7=9A=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81=E6=94=B9=E4=B8=BA=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 10 +++++----- src/api/view.py | 19 +++++++++++-------- src/config.py | 2 +- src/db/__init__.py | 3 ++- src/db/db.py | 14 +++++++------- src/utils/response.py | 5 +++-- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6bacade..a18e0dd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -54,12 +54,12 @@ $ curl -X GET 'http://127.0.0.1/api/query/test' } ``` -## query-all +## query/all * 参数: 无 ```bash -$ curl -X GET 'http://127.0.0.1/api/query-all/' +$ curl -X GET 'http://127.0.0.1/api/query/all' ``` ### 返回数据 @@ -77,12 +77,12 @@ $ curl -X GET 'http://127.0.0.1/api/query-all/' } ``` -## query-theme +## query/theme * 参数: `name` ```bash -$ curl -X GET 'http://127.0.0.1/api/query-theme/lewd' +$ curl -X GET 'http://127.0.0.1/api/query/theme/lewd' ``` * ***注:返回的base64文本开头没有添加`data:image/gif;base64,`清手动添加*** @@ -117,7 +117,7 @@ $ curl -X GET 'http://127.0.0.1/api/query-theme/lewd' * 参数: 无 ```bash -$ curl -X GET 'http://127.0.0.1/api/export/' +$ curl -X GET 'http://127.0.0.1/api/export' ``` > 此接口返回文件 diff --git a/src/api/view.py b/src/api/view.py index aae0445..3a43dc5 100644 --- a/src/api/view.py +++ b/src/api/view.py @@ -19,21 +19,24 @@ async def _time(): @api.get('/query/{name}') async def query(name: str): - data = Database().query(name) + data = await Database().query(name) + name = data[0] + times = data[1] response = { 'code': 200, 'time': await _time(), 'data': { - 'name': data[0], - 'times': data[1] + 'name': name, + 'times': times } } return response -@api.get('/query-all/') +@api.get('/query/all') async def query_all(limit: int = 30): - data = Database().query_all()[:limit] + result = await Database().query_all() + data = result[:limit] response = { 'code': 200, 'time': await _time(), @@ -47,14 +50,14 @@ async def query_all(limit: int = 30): return response -@api.get('/export/') +@api.get('/export') async def export(): return FileResponse('./src/db/data.sqlite') -@api.get('/query-theme/{name}') +@api.get('/query/theme/{name}') async def query_theme(name: str): - data = Database().query_image(name) + data = await Database().query_image(name) response = { 'code': 200, 'time': await _time(), diff --git a/src/config.py b/src/config.py index c5e737c..311c225 100644 --- a/src/config.py +++ b/src/config.py @@ -14,4 +14,4 @@ class Config: mysql -> user:pwd@host:port/db """ database = os.getenv('COUNTER_DB') or "sqlite3" # Database type - DETA = eval(os.getenv('DETA_RUNTIME').title()) # mark Deta + DETA = False if not os.getenv('DETA_RUNTIME') else True # mark deta diff --git a/src/db/__init__.py b/src/db/__init__.py index 7df81d5..c820241 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -24,7 +24,8 @@ if Config.DETA and Config.database == 'sqlite3': if not os.path.exists('/tmp/data.sqlite'): download_file('/tmp/data.sqlite') elif not Config.DETA and Config.database == 'sqlite3': - download_file('./src/db/data.sqlite') + if not os.path.exists('./src/db/data.sqlite'): + download_file('./src/db/data.sqlite') if Config.database == 'sqlite3': from src.db.db import SQLite as Database diff --git a/src/db/db.py b/src/db/db.py index 429ca54..ad88921 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -24,30 +24,30 @@ class BaseSQL: self.cursor.close() self.conn.close() - def query(self, _id: str) -> tuple: + async def query(self, _id: str) -> tuple: self.cursor.execute('select * from data where id="%s";' % _id) result = self.cursor.fetchone() if result is None: - self.insert(_id) + await self.insert(_id) return tuple([_id, 0]) - self.update(_id, result[1]) + await self.update(_id, result[1]) return result - def insert(self, _id: str) -> bool: + async def insert(self, _id: str) -> bool: self.cursor.execute('insert into data (id, times) values ("%s", 1);' % _id) return True - def update(self, _id: str, times: int) -> bool: + async def update(self, _id: str, times: int) -> bool: times += 1 self.cursor.execute('update data set times=%s where id="%s";' % (times, _id)) return True - def query_all(self) -> list: + async def query_all(self) -> list: self.cursor.execute('select * from data;') result = self.cursor.fetchall() return result - def query_image(self, theme: str) -> list: + async def query_image(self, theme: str) -> list: self.cursor.execute('select * from %s;' % theme) result = self.cursor.fetchall() return result diff --git a/src/utils/response.py b/src/utils/response.py index 9c604d1..62d48f7 100644 --- a/src/utils/response.py +++ b/src/utils/response.py @@ -18,7 +18,8 @@ async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict: :param theme: theme for counter :return: """ - times = Database().query(_id)[1] + result = await Database().query(_id) + times = result[1] str_number = str(times) # 将整形转换为字符串 len_number = len(str_number) # 再获取字符串长度 g_length = length * '0' # 根据输入的位数来生成0的数量 @@ -26,7 +27,7 @@ async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict: context = [] headers = {'cache-control': 'max-age=0, no-cache, no-store, must-revalidate', 'Content-Type': 'image/svg+xml; charset=utf-8'} - data = Database().query_image(theme) + data = await Database().query_image(theme) height = data[0][-1] width = data[0][-2] counter = 0