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 fastapi.exceptions import HTTPException |
||||
from server import app |
from exceptions.exception import AuthException, PermissionException |
||||
from exceptions.auth_exception import AuthException |
|
||||
from exceptions.permission_exception import PermissionException |
|
||||
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder |
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder |
||||
|
|
||||
|
|
||||
# 自定义token检验异常 |
def handle_exception(app: FastAPI): |
||||
@app.exception_handler(AuthException) |
""" |
||||
async def auth_exception_handler(request: Request, exc: AuthException): |
全局异常处理 |
||||
return ResponseUtil.unauthorized(data=exc.data, msg=exc.message) |
""" |
||||
|
# 自定义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) |
||||
|
|
||||
# 自定义权限检验异常 |
# 处理其他http请求异常 |
||||
@app.exception_handler(PermissionException) |
@app.exception_handler(HTTPException) |
||||
async def permission_exception_handler(request: Request, exc: PermissionException): |
async def http_exception_handler(request: Request, exc: HTTPException): |
||||
return ResponseUtil.forbidden(data=exc.data, msg=exc.message) |
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 fastapi.middleware.cors import CORSMiddleware |
||||
from server import app |
|
||||
|
|
||||
|
|
||||
# 前端页面url |
def add_cors_middleware(app: FastAPI): |
||||
origins = [ |
# 前端页面url |
||||
"http://localhost:80", |
origins = [ |
||||
"http://127.0.0.1:80", |
"http://localhost:80", |
||||
] |
"http://127.0.0.1:80", |
||||
|
] |
||||
|
|
||||
# 后台api允许跨域 |
# 后台api允许跨域 |
||||
app.add_middleware( |
app.add_middleware( |
||||
CORSMiddleware, |
CORSMiddleware, |
||||
allow_origins=origins, |
allow_origins=origins, |
||||
allow_credentials=True, |
allow_credentials=True, |
||||
allow_methods=["*"], |
allow_methods=["*"], |
||||
allow_headers=["*"], |
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 fastapi.staticfiles import StaticFiles |
||||
from server import app |
|
||||
from config.env import UploadConfig |
from config.env import UploadConfig |
||||
|
|
||||
|
|
||||
# 挂载静态文件路径 |
def mount_staticfiles(app: FastAPI): |
||||
app.mount(f"{UploadConfig.UPLOAD_PREFIX}", StaticFiles(directory=f"{UploadConfig.UPLOAD_PATH}"), name="profile") |
""" |
||||
|
挂载静态文件 |
||||
|
""" |
||||
|
app.mount(f"{UploadConfig.UPLOAD_PREFIX}", StaticFiles(directory=f"{UploadConfig.UPLOAD_PATH}"), name="profile") |
||||
|
Loading…
Reference in new issue