Archived
fix: 修复了一些bug并将剩余的同步代码改为异步执行
This commit is contained in:
6 files changed
+29
-24
No files matched your search
+5
-5
@@ -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'
|
||||
```
|
||||
|
||||
> 此接口返回文件
|
||||
+11
-8
@@ -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(),
|
||||
|
||||
+1
-1
@@ -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
|
||||
+2
-1
@@ -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
|
||||
|
||||
+7
-7
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user