Skip to content

Commit c6978d9

Browse files
authored
Merge pull request #6 from MirrorChyan/dev
2 parents a0def7e + 711f25d commit c6978d9

File tree

9 files changed

+170
-60
lines changed

9 files changed

+170
-60
lines changed

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ DATABASE_HOST="127.0.0.1"
33
DATABASE_PORT=3306
44
DATABASE_USER=""
55
DATABASE_PASSWD=""
6+
7+
STATIC_APP_DIR="/app/static"

main.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
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
11+
from src.ICP import router as icp_router
812

913
app = FastAPI()
1014

11-
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")
1217

1318
app.include_router(anno_router)
1419
app.include_router(plan_router)
20+
app.include_router(plan_details_router)
1521
app.include_router(health_check_router)
1622
app.include_router(project_router)
23+
app.include_router(icp_router)
1724

src/ICP/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from fastapi import APIRouter, Request
2+
from loguru import logger
3+
4+
from src.database import ICP
5+
6+
router = APIRouter()
7+
8+
AllICP = ICP.select()
9+
10+
11+
@router.get("/icp")
12+
async def query_icp(domain: str):
13+
icp = next(
14+
(icp for icp in AllICP if icp.domain in domain),
15+
None,
16+
)
17+
18+
if icp is None:
19+
logger.error(f"domain not found: {domain}")
20+
return {
21+
"domain": domain,
22+
"icp_beian": "",
23+
"icp_url": "",
24+
"icp_entity": "",
25+
}
26+
27+
return {
28+
"domain": domain,
29+
"icp_beian": icp.beian,
30+
"icp_url": icp.url,
31+
"icp_entity": icp.entity,
32+
}

src/config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Settings(BaseSettings):
77
database_port: int
88
database_user: str
99
database_passwd: str
10+
11+
static_app_dir: str
1012

1113
class Config:
1214
env_file = ".env"

src/database/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ class Meta:
4646

4747
class Plan(Model):
4848
plan_index = IntegerField()
49-
platform = CharField()
50-
plan_id = CharField()
5149
type_id = CharField()
50+
plan_id = CharField()
51+
title = TextField()
52+
price = CharField()
53+
original_price = CharField()
5254
popular = IntegerField()
5355
available = BooleanField()
56+
afdian_id = CharField()
57+
yimapay_id = CharField()
5458

5559
class Meta:
5660
database = db
@@ -72,6 +76,17 @@ class Meta:
7276
database = db
7377
table_name = "project"
7478

79+
class ICP(Model):
80+
domain = CharField()
81+
beian = CharField()
82+
entity = CharField()
83+
url = CharField()
84+
85+
class Meta:
86+
database = db
87+
table_name = "icp"
88+
7589
Anno.create_table()
7690
Plan.create_table()
77-
Project.create_table()
91+
Project.create_table()
92+
ICP.create_table()

src/plan/__init__.py

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

src/plan/details.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 p:
19+
logger.error(f"plan_id not found")
20+
return {"ec": 404, "msg": "plan_id not found"}
21+
22+
23+
afdian_info = [s.strip() for s in p.afdian_id.split(",")]
24+
if len(afdian_info) != 2:
25+
logger.error(f"afdian_info is invalid")
26+
return {"ec": 500, "msg": "afdian_info is invalid"}
27+
28+
return {
29+
"ec": 200,
30+
"data": {
31+
"title": p.title,
32+
"type_id": p.type_id,
33+
"price": p.price,
34+
"original_price": p.original_price,
35+
"popular": p.popular,
36+
# "afdian_id": p.afdian_id,
37+
"afdian_info": {
38+
"plan_id": afdian_info[0],
39+
"sku_id": afdian_info[1],
40+
},
41+
"yimapay_id": p.yimapay_id,
42+
},
43+
}

src/plan/summary.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 not p.available:
24+
continue
25+
26+
if p.type_id == type_id:
27+
data["home"].append(
28+
{
29+
"title": p.title,
30+
"price": p.price,
31+
"original_price": p.original_price,
32+
"popular": p.popular,
33+
"plan_id": p.plan_id,
34+
}
35+
)
36+
else:
37+
data["more"].append(
38+
{
39+
"title": p.title,
40+
"price": p.price,
41+
"original_price": p.original_price,
42+
"popular": p.popular,
43+
"plan_id": p.plan_id,
44+
}
45+
)
46+
47+
if not data["home"]:
48+
data["home"], data["more"] = data["more"], data["home"]
49+
50+
return {"ec": 200, "data": data}

0 commit comments

Comments
 (0)