40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
# _*_ coding: utf-8 _*_
|
|
# @Time :2022/5/26 09:10
|
|
# @Email :508737091@qq.com
|
|
# @Author :qiangyanwen
|
|
# @File :main.py.py
|
|
|
|
import uvicorn
|
|
|
|
from app import create_app
|
|
from config import Debug
|
|
from middleware.http_middleware import get_body
|
|
from utils.system import host
|
|
from config import settings
|
|
from fastapi.requests import Request
|
|
from fastapi import status
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
app = create_app()
|
|
|
|
|
|
@app.middleware("http")
|
|
async def errors_handling(request: Request, call_next):
|
|
body = await request.body()
|
|
try:
|
|
await get_body(request)
|
|
return await call_next(request)
|
|
except Exception as exc:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_200_OK,
|
|
content=jsonable_encoder({
|
|
"code": 110,
|
|
"msg": str(exc),
|
|
"request_data": body,
|
|
})
|
|
)
|
|
|
|
if __name__ == '__main__':
|
|
uvicorn.run(app="main:app", host=host, port=settings.PROJECT.PORT, reload=Debug)
|