mani.py on_event->lifespan; error->return code -1
This commit is contained in:
@@ -31,8 +31,8 @@ async def test(
|
||||
|
||||
|
||||
@r.get(
|
||||
"/apps",
|
||||
response_model=List[AppList],
|
||||
"/apps",tags=["App"],
|
||||
response_model=ApiReturnPage[AppList],
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def apps_list(
|
||||
@@ -44,7 +44,7 @@ async def apps_list(
|
||||
filtered_apps = []
|
||||
domain = dbdomain.get_default_domain(db,user.id) #get_activedomain(db, user.id)
|
||||
if not domain:
|
||||
return filtered_apps
|
||||
return ApiReturnPage(data = filtered_apps)
|
||||
|
||||
platformapps = get_apps(db,domain.url)
|
||||
kintoneevn = config.KINTONE_ENV(domain)
|
||||
@@ -68,10 +68,11 @@ async def apps_list(
|
||||
if papp.appid in kintone_apps_dict:
|
||||
papp.appname = kintone_apps_dict[papp.appid]["name"]
|
||||
filtered_apps.append(papp)
|
||||
return filtered_apps
|
||||
return ApiReturnPage(data = filtered_apps)
|
||||
except Exception as e:
|
||||
raise APIException('platform:apps',request.url._url,f"Error occurred while get apps:",e)
|
||||
|
||||
|
||||
@r.post("/apps", response_model=AppList, response_model_exclude_none=True)
|
||||
async def apps_update(
|
||||
request: Request,
|
||||
|
||||
@@ -13,6 +13,11 @@ class ApiReturnModel(BaseModel,Generic[T]):
|
||||
msg:str ="OK"
|
||||
data:T
|
||||
|
||||
class ApiReturnError(BaseModel):
|
||||
code:int = -1
|
||||
msg:str =""
|
||||
|
||||
|
||||
class Params(BaseModel, AbstractParams):
|
||||
page:int = Query(1,get=1, description="Page number")
|
||||
size:int = Query(20,get=0, le=100,description="Page size")
|
||||
|
||||
@@ -64,7 +64,7 @@ class dbdomain(crudbase):
|
||||
if db_domain.is_active == True and domain.is_active == False:
|
||||
db_userdomains = dbuserdomain.get_default_domains(db,domain.id)
|
||||
for userdomain in db_userdomains:
|
||||
userdomain.active = False
|
||||
userdomain.is_default = False
|
||||
db.add(userdomain)
|
||||
db_domain.is_active=domain.is_active
|
||||
db_domain.kintoneuser=domain.kintoneuser
|
||||
|
||||
@@ -15,15 +15,22 @@ from app import tasks
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import logging
|
||||
from app.core.apiexception import APIException, writedblog
|
||||
from app.core.common import ApiReturnError
|
||||
from app.db.crud import create_log
|
||||
from fastapi.responses import JSONResponse
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
startup_event()
|
||||
yield
|
||||
|
||||
app = FastAPI(
|
||||
title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api"
|
||||
title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api",lifespan=lifespan
|
||||
)
|
||||
|
||||
origins = [
|
||||
@@ -47,12 +54,11 @@ add_pagination(app)
|
||||
# request.state.db.close()
|
||||
# return response
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
def startup_event():
|
||||
log_dir="log"
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
|
||||
logger = logging.getLogger("uvicorn.access")
|
||||
handler = logging.handlers.RotatingFileHandler(f"{log_dir}/api.log",mode="a",maxBytes = 100*1024, backupCount = 3)
|
||||
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
|
||||
@@ -64,7 +70,7 @@ async def api_exception_handler(request: Request, exc: APIException):
|
||||
loop.run_in_executor(None,writedblog,exc)
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"detail": f"{exc.detail}"},
|
||||
content= ApiReturnError(msg = f"{exc.detail}").model_dump(),
|
||||
)
|
||||
|
||||
@app.get("/api/v1")
|
||||
|
||||
Reference in New Issue
Block a user