Skip to content

Commit d37a583

Browse files
committed
refactor: rft plan API
1 parent 7c31570 commit d37a583

File tree

8 files changed

+109
-67
lines changed

8 files changed

+109
-67
lines changed

.env.sample

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ DATABASE_PASSWD=""
66

77
ICP_BEIAN=""
88
ICP_URL="https://beian.miit.gov.cn/"
9-
ICP_ENTITY=""
9+
ICP_ENTITY=""
10+
11+
STATIC_APP_DIR="/app/static"

main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
from fastapi import FastAPI
22
from fastapi.staticfiles import StaticFiles
33

4+
from src.config import settings
5+
46
from src.anno import router as anno_router
5-
from src.plan import router as plan_router
7+
from src.plan.summary import router as plan_router
8+
from src.plan.details import router as plan_details_router
69
from src.health_check import router as health_check_router
710
from src.project import router as project_router
811
from src.ICP import router as icp_router
912

1013
app = FastAPI()
1114

12-
app.mount("/static", StaticFiles(directory="/app/static"), name="static")
15+
if settings.static_app_dir:
16+
app.mount("/static", StaticFiles(directory=settings.static_app_dir), name="static")
1317

1418
app.include_router(anno_router)
1519
app.include_router(plan_router)
20+
app.include_router(plan_details_router)
1621
app.include_router(health_check_router)
1722
app.include_router(project_router)
1823
app.include_router(icp_router)

src/config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Settings(BaseSettings):
1010
icp_beian: str
1111
icp_url: str
1212
icp_entity: str
13+
static_app_dir: str
1314

1415
class Config:
1516
env_file = ".env"

src/database/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class Meta:
4646

4747
class Plan(Model):
4848
plan_index = IntegerField()
49-
platform = CharField()
50-
plan_id = CharField()
5149
type_id = CharField()
52-
popular = IntegerField()
53-
available = BooleanField()
50+
plan_id = CharField()
5451
title = TextField()
5552
price = CharField()
5653
original_price = CharField()
54+
popular = IntegerField()
55+
available = BooleanField()
56+
afdian_id = CharField()
57+
yimapay_id = CharField()
5758

5859
class Meta:
5960
database = db

src/plan/__init__.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +0,0 @@
1-
from fastapi import APIRouter
2-
from loguru import logger
3-
from time import time
4-
5-
from src.database import Plan
6-
7-
router = APIRouter()
8-
9-
CacheExpiration = 60 # 秒
10-
plan_cache = None
11-
12-
13-
@router.get("/plan")
14-
async def query_anno(type_id: str = "GameTools"):
15-
logger.debug(f"type_id: {type_id}")
16-
17-
if not type_id:
18-
logger.error(f"type_id is required")
19-
return {"ec": 400, "msg": "type_id is required"}
20-
21-
now = time()
22-
global plan_cache
23-
if not plan_cache or (now - plan_cache[1] > CacheExpiration):
24-
plan_cache = (Plan.select().where(Plan.available == True).order_by(Plan.plan_index), now)
25-
26-
data = {
27-
"home": [],
28-
"more": [],
29-
}
30-
31-
for p in plan_cache[0]:
32-
if p.type_id == type_id:
33-
data["home"].append(
34-
{
35-
"platform": p.platform,
36-
"plan_id": p.plan_id,
37-
"type_id": p.type_id,
38-
"popular": p.popular,
39-
"title": p.title,
40-
"price": p.price,
41-
"original_price": p.original_price,
42-
}
43-
)
44-
else:
45-
data["more"].append(
46-
{
47-
"platform": p.platform,
48-
"plan_id": p.plan_id,
49-
"type_id": p.type_id,
50-
"popular": p.popular,
51-
"title": p.title,
52-
"price": p.price,
53-
"original_price": p.original_price,
54-
}
55-
)
56-
57-
if not data["home"]:
58-
data["home"], data["more"] = data["more"], data["home"]
59-
60-
return {"ec": 200, "data": data}

src/plan/cache.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from time import time
2+
from src.database import Plan
3+
4+
CacheExpiration = 60 # 秒
5+
_plan_cache = None
6+
7+
def get_plan_cache():
8+
now = time()
9+
10+
global _plan_cache
11+
if not _plan_cache or (now - _plan_cache[1] > CacheExpiration):
12+
_plan_cache = (Plan.select().where(Plan.available == True).order_by(Plan.plan_index), now)
13+
14+
return _plan_cache[0]

src/plan/details.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from fastapi import APIRouter
2+
from loguru import logger
3+
4+
from .cache import get_plan_cache
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/plan/{plan_id}")
10+
async def query_details(plan_id: str):
11+
logger.debug(f"plan_id: {plan_id}")
12+
13+
if not plan_id:
14+
logger.error(f"plan_id is required")
15+
return {"ec": 400, "msg": "plan_id is required"}
16+
17+
p = next((p for p in get_plan_cache() if p.plan_id == plan_id), None)
18+
if not plan_id:
19+
logger.error(f"plan_id not found")
20+
return {"ec": 404, "msg": "plan_id not found"}
21+
22+
return {
23+
"ec": 200,
24+
"data": {
25+
"title": p.title,
26+
"price": p.price,
27+
"original_price": p.original_price,
28+
"popular": p.popular,
29+
"afdian_id": p.afdian_id,
30+
"yimapay_id": p.yimapay_id,
31+
},
32+
}

src/plan/summary.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from fastapi import APIRouter
2+
from loguru import logger
3+
from time import time
4+
5+
from .cache import get_plan_cache
6+
7+
router = APIRouter()
8+
9+
@router.get("/plan")
10+
async def query_plan(type_id: str = "GameTools"):
11+
logger.debug(f"type_id: {type_id}")
12+
13+
if not type_id:
14+
logger.error(f"type_id is required")
15+
return {"ec": 400, "msg": "type_id is required"}
16+
17+
data = {
18+
"home": [],
19+
"more": [],
20+
}
21+
22+
for p in get_plan_cache():
23+
if p.type_id == type_id:
24+
data["home"].append(
25+
{
26+
"title": p.title,
27+
"price": p.price,
28+
"original_price": p.original_price,
29+
"popular": p.popular,
30+
"plan_id": p.plan_id,
31+
}
32+
)
33+
else:
34+
data["more"].append(
35+
{
36+
"title": p.title,
37+
"price": p.price,
38+
"original_price": p.original_price,
39+
"popular": p.popular,
40+
"plan_id": p.plan_id,
41+
}
42+
)
43+
44+
if not data["home"]:
45+
data["home"], data["more"] = data["more"], data["home"]
46+
47+
return {"ec": 200, "data": data}

0 commit comments

Comments
 (0)