From aab79ea2d2d0d038baec17cd12eba6f96df66c6c Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Wed, 31 May 2023 13:20:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20fastapi=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/car.py | 8 +++++--- main.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/handler/car.py b/handler/car.py index 0378bba..1f35374 100644 --- a/handler/car.py +++ b/handler/car.py @@ -17,11 +17,13 @@ def get_list(db: Session = Depends(database), size: int = Query(default=10), keyword: str = Query(default=""), 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) 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: query = query.order_by(JiaKaoBaoDian.wrong_rate.desc()) @@ -29,7 +31,7 @@ def get_list(db: Session = Depends(database), offset = (page - 1) * size data = query.offset(offset).limit(size).all() - logger.logger.info("data num %d", len(data)) + logger.logger.info("data num {}", len(data)) return response({ "data": model_list(data), "page": page, diff --git a/main.py b/main.py index c4d2114..b47f710 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import logger import uvicorn from fastapi import FastAPI @@ -9,5 +10,16 @@ app = FastAPI(docs_url="/docs", redoc_url=None) app.include_router(health.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__': uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)