insistence
1 year ago
6 changed files with 75 additions and 38 deletions
@ -1,27 +1,27 @@ |
|||
from fastapi import Request |
|||
from fastapi import FastAPI, Request |
|||
from fastapi.exceptions import HTTPException |
|||
from server import app |
|||
from exceptions.auth_exception import AuthException |
|||
from exceptions.permission_exception import PermissionException |
|||
from exceptions.exception import AuthException, PermissionException |
|||
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder |
|||
|
|||
|
|||
# 自定义token检验异常 |
|||
@app.exception_handler(AuthException) |
|||
async def auth_exception_handler(request: Request, exc: AuthException): |
|||
return ResponseUtil.unauthorized(data=exc.data, msg=exc.message) |
|||
def handle_exception(app: FastAPI): |
|||
""" |
|||
全局异常处理 |
|||
""" |
|||
# 自定义token检验异常 |
|||
@app.exception_handler(AuthException) |
|||
async def auth_exception_handler(request: Request, exc: AuthException): |
|||
return ResponseUtil.unauthorized(data=exc.data, msg=exc.message) |
|||
|
|||
# 自定义权限检验异常 |
|||
@app.exception_handler(PermissionException) |
|||
async def permission_exception_handler(request: Request, exc: PermissionException): |
|||
return ResponseUtil.forbidden(data=exc.data, msg=exc.message) |
|||
|
|||
# 自定义权限检验异常 |
|||
@app.exception_handler(PermissionException) |
|||
async def permission_exception_handler(request: Request, exc: PermissionException): |
|||
return ResponseUtil.forbidden(data=exc.data, msg=exc.message) |
|||
|
|||
|
|||
# 处理其他http请求异常 |
|||
@app.exception_handler(HTTPException) |
|||
async def http_exception_handler(request: Request, exc: HTTPException): |
|||
return JSONResponse( |
|||
content=jsonable_encoder({"code": exc.status_code, "msg": exc.detail}), |
|||
status_code=exc.status_code |
|||
) |
|||
# 处理其他http请求异常 |
|||
@app.exception_handler(HTTPException) |
|||
async def http_exception_handler(request: Request, exc: HTTPException): |
|||
return JSONResponse( |
|||
content=jsonable_encoder({"code": exc.status_code, "msg": exc.detail}), |
|||
status_code=exc.status_code |
|||
) |
|||
|
@ -1,18 +1,19 @@ |
|||
from fastapi import FastAPI |
|||
from fastapi.middleware.cors import CORSMiddleware |
|||
from server import app |
|||
|
|||
|
|||
# 前端页面url |
|||
origins = [ |
|||
"http://localhost:80", |
|||
"http://127.0.0.1:80", |
|||
] |
|||
def add_cors_middleware(app: FastAPI): |
|||
# 前端页面url |
|||
origins = [ |
|||
"http://localhost:80", |
|||
"http://127.0.0.1:80", |
|||
] |
|||
|
|||
# 后台api允许跨域 |
|||
app.add_middleware( |
|||
CORSMiddleware, |
|||
allow_origins=origins, |
|||
allow_credentials=True, |
|||
allow_methods=["*"], |
|||
allow_headers=["*"], |
|||
) |
|||
# 后台api允许跨域 |
|||
app.add_middleware( |
|||
CORSMiddleware, |
|||
allow_origins=origins, |
|||
allow_credentials=True, |
|||
allow_methods=["*"], |
|||
allow_headers=["*"], |
|||
) |
|||
|
@ -0,0 +1,10 @@ |
|||
from fastapi import FastAPI |
|||
from middlewares.cors_middleware import add_cors_middleware |
|||
|
|||
|
|||
def handle_middleware(app: FastAPI): |
|||
""" |
|||
全局中间件处理 |
|||
""" |
|||
# 加载跨域中间件 |
|||
add_cors_middleware(app) |
@ -0,0 +1,10 @@ |
|||
from fastapi import FastAPI |
|||
from sub_applications.staticfiles import mount_staticfiles |
|||
|
|||
|
|||
def handle_sub_applications(app: FastAPI): |
|||
""" |
|||
全局处理子应用挂载 |
|||
""" |
|||
# 挂载静态文件 |
|||
mount_staticfiles(app) |
@ -1,7 +1,10 @@ |
|||
from fastapi import FastAPI |
|||
from fastapi.staticfiles import StaticFiles |
|||
from server import app |
|||
from config.env import UploadConfig |
|||
|
|||
|
|||
# 挂载静态文件路径 |
|||
app.mount(f"{UploadConfig.UPLOAD_PREFIX}", StaticFiles(directory=f"{UploadConfig.UPLOAD_PATH}"), name="profile") |
|||
def mount_staticfiles(app: FastAPI): |
|||
""" |
|||
挂载静态文件 |
|||
""" |
|||
app.mount(f"{UploadConfig.UPLOAD_PREFIX}", StaticFiles(directory=f"{UploadConfig.UPLOAD_PATH}"), name="profile") |
|||
|
Loading…
Reference in new issue