使用 fastapi 重构

This commit is contained in:
shikong 2023-05-31 13:20:04 +08:00
parent 06385ba4fe
commit aab79ea2d2
2 changed files with 17 additions and 3 deletions

View File

@ -17,11 +17,13 @@ def get_list(db: Session = Depends(database),
size: int = Query(default=10), size: int = Query(default=10),
keyword: str = Query(default=""), keyword: str = Query(default=""),
order_by_wrong_rate: bool = Query(default=True)): order_by_wrong_rate: bool = Query(default=True)):
logger.logger.info("page => %d, size => %d, keyword => %s", page, size, keyword) logger.logger.info("page => {}, size => {}, keyword => {}", page, size, keyword)
query = db.query(JiaKaoBaoDian) query = db.query(JiaKaoBaoDian)
if len(keyword) > 0: if len(keyword) > 0:
query = query.filter(JiaKaoBaoDian.question.like("%%%s%%" % keyword)) query = query.filter(
JiaKaoBaoDian.question.like("%%%s%%" % keyword),
JiaKaoBaoDian.illiteracy_explain.like("%%%s%%" % keyword),)
if order_by_wrong_rate: if order_by_wrong_rate:
query = query.order_by(JiaKaoBaoDian.wrong_rate.desc()) query = query.order_by(JiaKaoBaoDian.wrong_rate.desc())
@ -29,7 +31,7 @@ def get_list(db: Session = Depends(database),
offset = (page - 1) * size offset = (page - 1) * size
data = query.offset(offset).limit(size).all() data = query.offset(offset).limit(size).all()
logger.logger.info("data num %d", len(data)) logger.logger.info("data num {}", len(data))
return response({ return response({
"data": model_list(data), "data": model_list(data),
"page": page, "page": page,

12
main.py
View File

@ -1,3 +1,4 @@
import logger
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
@ -9,5 +10,16 @@ app = FastAPI(docs_url="/docs", redoc_url=None)
app.include_router(health.router) app.include_router(health.router)
app.include_router(car.router) app.include_router(car.router)
@app.on_event("startup")
async def startup():
logger.logger.info("服务启动")
@app.on_event("shutdown")
async def shutdown():
logger.logger.info("服务关闭")
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True) uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)