This commit is contained in:
xtaodada 2023-04-23 16:01:46 +08:00
parent 265c335b0f
commit 7ecba57e85
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 14 additions and 4 deletions

View File

@ -2,17 +2,23 @@ import secrets
from defs import app
from models.services.tip import TipAction
from typing import Dict, Optional
from typing import Dict, Optional, List
system_random = secrets.SystemRandom()
async def get_random_tips() -> Optional[Dict]:
async def get_random_tips() -> List[Optional[Dict]]:
tips = await TipAction.get_tips()
if not tips:
return None
num = system_random.randint(0, len(tips) - 1)
return tips[num].dict_tip()
if len(tips) < 4:
return [tip.dict_tip() for tip in tips]
data = []
while len(data) < 4:
num = system_random.randint(0, len(tips) - 1)
if tips[num] not in data:
data.append(tips[num])
return [tip.dict_tip() for tip in data]
@app.get("/get_tips")

View File

@ -2,6 +2,7 @@ import asyncio
from fastapi import FastAPI
from starlette.middleware.trustedhost import TrustedHostMiddleware
from fastapi.middleware.cors import CORSMiddleware
from models.sqlite import Sqlite
@ -10,6 +11,9 @@ app = FastAPI()
app.add_middleware(
TrustedHostMiddleware, allowed_hosts=["127.0.0.1"]
)
app.add_middleware(
CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
)
sqlite = Sqlite()
need_auth_routes = []
need_auth_uid_only_routes = []