fix: 修复了一些bug并将剩余的同步代码改为异步执行

This commit is contained in:
RTAkland
2022-12-22 18:10:02 +08:00
parent ad055cf6fe
commit ce009e0203
6 files changed
+29 -24

No files matched your search

+5 -5
View File
@@ -54,12 +54,12 @@ $ curl -X GET 'http://127.0.0.1/api/query/test'
} }
``` ```
## query-all ## query/all
* 参数: 无 * 参数: 无
```bash ```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` * 参数: `name`
```bash ```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,`清手动添加*** * ***注:返回的base64文本开头没有添加`data:image/gif;base64,`清手动添加***
@@ -117,7 +117,7 @@ $ curl -X GET 'http://127.0.0.1/api/query-theme/lewd'
* 参数: 无 * 参数: 无
```bash ```bash
$ curl -X GET 'http://127.0.0.1/api/export/' $ curl -X GET 'http://127.0.0.1/api/export'
``` ```
> 此接口返回文件 > 此接口返回文件
+11 -8
View File
@@ -19,21 +19,24 @@ async def _time():
@api.get('/query/{name}') @api.get('/query/{name}')
async def query(name: str): async def query(name: str):
data = Database().query(name) data = await Database().query(name)
name = data[0]
times = data[1]
response = { response = {
'code': 200, 'code': 200,
'time': await _time(), 'time': await _time(),
'data': { 'data': {
'name': data[0], 'name': name,
'times': data[1] 'times': times
} }
} }
return response return response
@api.get('/query-all/') @api.get('/query/all')
async def query_all(limit: int = 30): async def query_all(limit: int = 30):
data = Database().query_all()[:limit] result = await Database().query_all()
data = result[:limit]
response = { response = {
'code': 200, 'code': 200,
'time': await _time(), 'time': await _time(),
@@ -47,14 +50,14 @@ async def query_all(limit: int = 30):
return response return response
@api.get('/export/') @api.get('/export')
async def export(): async def export():
return FileResponse('./src/db/data.sqlite') return FileResponse('./src/db/data.sqlite')
@api.get('/query-theme/{name}') @api.get('/query/theme/{name}')
async def query_theme(name: str): async def query_theme(name: str):
data = Database().query_image(name) data = await Database().query_image(name)
response = { response = {
'code': 200, 'code': 200,
'time': await _time(), 'time': await _time(),
+1 -1
View File
@@ -14,4 +14,4 @@ class Config:
mysql -> user:pwd@host:port/db mysql -> user:pwd@host:port/db
""" """
database = os.getenv('COUNTER_DB') or "sqlite3" # Database type 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
+2 -1
View File
@@ -24,7 +24,8 @@ if Config.DETA and Config.database == 'sqlite3':
if not os.path.exists('/tmp/data.sqlite'): if not os.path.exists('/tmp/data.sqlite'):
download_file('/tmp/data.sqlite') download_file('/tmp/data.sqlite')
elif not Config.DETA and Config.database == 'sqlite3': 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': if Config.database == 'sqlite3':
from src.db.db import SQLite as Database from src.db.db import SQLite as Database
+7 -7
View File
@@ -24,30 +24,30 @@ class BaseSQL:
self.cursor.close() self.cursor.close()
self.conn.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) self.cursor.execute('select * from data where id="%s";' % _id)
result = self.cursor.fetchone() result = self.cursor.fetchone()
if result is None: if result is None:
self.insert(_id) await self.insert(_id)
return tuple([_id, 0]) return tuple([_id, 0])
self.update(_id, result[1]) await self.update(_id, result[1])
return result 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) self.cursor.execute('insert into data (id, times) values ("%s", 1);' % _id)
return True return True
def update(self, _id: str, times: int) -> bool: async def update(self, _id: str, times: int) -> bool:
times += 1 times += 1
self.cursor.execute('update data set times=%s where id="%s";' % (times, _id)) self.cursor.execute('update data set times=%s where id="%s";' % (times, _id))
return True return True
def query_all(self) -> list: async def query_all(self) -> list:
self.cursor.execute('select * from data;') self.cursor.execute('select * from data;')
result = self.cursor.fetchall() result = self.cursor.fetchall()
return result return result
def query_image(self, theme: str) -> list: async def query_image(self, theme: str) -> list:
self.cursor.execute('select * from %s;' % theme) self.cursor.execute('select * from %s;' % theme)
result = self.cursor.fetchall() result = self.cursor.fetchall()
return result return result
+3 -2
View File
@@ -18,7 +18,8 @@ async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict:
:param theme: theme for counter :param theme: theme for counter
:return: :return:
""" """
times = Database().query(_id)[1] result = await Database().query(_id)
times = result[1]
str_number = str(times) # 将整形转换为字符串 str_number = str(times) # 将整形转换为字符串
len_number = len(str_number) # 再获取字符串长度 len_number = len(str_number) # 再获取字符串长度
g_length = length * '0' # 根据输入的位数来生成0的数量 g_length = length * '0' # 根据输入的位数来生成0的数量
@@ -26,7 +27,7 @@ async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict:
context = [] context = []
headers = {'cache-control': 'max-age=0, no-cache, no-store, must-revalidate', headers = {'cache-control': 'max-age=0, no-cache, no-store, must-revalidate',
'Content-Type': 'image/svg+xml; charset=utf-8'} 'Content-Type': 'image/svg+xml; charset=utf-8'}
data = Database().query_image(theme) data = await Database().query_image(theme)
height = data[0][-1] height = data[0][-1]
width = data[0][-2] width = data[0][-2]
counter = 0 counter = 0