-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
78 lines (68 loc) · 2.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# main.py
from typing import List
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from datetime import datetime
from sqlalchemy.orm import Session
from api.db.database import engine, SessionLocal, Base, get_db # 导入SessionLocal变量
from api.db.models import PostModel # 导入数据库模型
app = FastAPI()
# 配置 CORS
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic 模型
class Post(BaseModel):
id: int
title: str
content: str
image_url: str
category: str
timestamp: datetime
author: str
url: str
class Config:
orm_mode = True
Base.metadata.create_all(bind=engine)
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/api/getpost", response_model=List[Post])
def get_posts(db: Session = Depends(get_db)):
posts = db.query(PostModel).order_by(PostModel.timestamp).all()
return posts
# 示例数据插入(仅在启动时运行一次)
def insert_sample_data():
db = SessionLocal()
if db.query(PostModel).first() is None: # 检查数据库中是否已经有数据
sample_posts = [
PostModel(
title="Post 1",
content="This is the content of post 1",
image_url="https://www.loliapi.com/acg/",
category="分类1",
author="Author 1",
url="https://example.com/post1"
),
PostModel(
title="Post 2",
content="This is the content of post 2",
image_url="https://www.loliapi.com/bg/",
category="分类2",
author="Author 2",
url="https://example.com/post2"
),
]
db.add_all(sample_posts)
db.commit()
db.close()
# 启动时插入示例数据
@app.on_event("startup")
def on_startup():
insert_sample_data()