fix: 修复子应用、中间件、异常无法正常挂载的问题
This commit is contained in:
@@ -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=["*"],
|
||||||
)
|
)
|
||||||
|
10
ruoyi-fastapi-backend/middlewares/handle.py
Normal file
10
ruoyi-fastapi-backend/middlewares/handle.py
Normal file
@@ -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)
|
@@ -1,5 +1,8 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
from sub_applications.handle import handle_sub_applications
|
||||||
|
from middlewares.handle import handle_middleware
|
||||||
|
from exceptions.handle import handle_exception
|
||||||
from module_admin.controller.login_controller import loginController
|
from module_admin.controller.login_controller import loginController
|
||||||
from module_admin.controller.captcha_controller import captchaController
|
from module_admin.controller.captcha_controller import captchaController
|
||||||
from module_admin.controller.user_controller import userController
|
from module_admin.controller.user_controller import userController
|
||||||
@@ -24,6 +27,7 @@ from utils.log_util import logger
|
|||||||
from utils.common_util import worship
|
from utils.common_util import worship
|
||||||
|
|
||||||
|
|
||||||
|
# 生命周期事件
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
logger.info(f"{AppConfig.app_name}开始启动")
|
logger.info(f"{AppConfig.app_name}开始启动")
|
||||||
@@ -39,6 +43,7 @@ async def lifespan(app: FastAPI):
|
|||||||
await SchedulerUtil.close_system_scheduler()
|
await SchedulerUtil.close_system_scheduler()
|
||||||
|
|
||||||
|
|
||||||
|
# 初始化FastAPI对象
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title=AppConfig.app_name,
|
title=AppConfig.app_name,
|
||||||
description=f'{AppConfig.app_name}接口文档',
|
description=f'{AppConfig.app_name}接口文档',
|
||||||
@@ -46,7 +51,15 @@ app = FastAPI(
|
|||||||
lifespan=lifespan
|
lifespan=lifespan
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 挂载子应用
|
||||||
|
handle_sub_applications(app)
|
||||||
|
# 加载中间件处理方法
|
||||||
|
handle_middleware(app)
|
||||||
|
# 加载全局异常处理方法
|
||||||
|
handle_exception(app)
|
||||||
|
|
||||||
|
|
||||||
|
# 加载路由列表
|
||||||
controller_list = [
|
controller_list = [
|
||||||
{'router': loginController, 'tags': ['登录模块']},
|
{'router': loginController, 'tags': ['登录模块']},
|
||||||
{'router': captchaController, 'tags': ['验证码模块']},
|
{'router': captchaController, 'tags': ['验证码模块']},
|
||||||
|
10
ruoyi-fastapi-backend/sub_applications/handle.py
Normal file
10
ruoyi-fastapi-backend/sub_applications/handle.py
Normal file
@@ -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")
|
||||||
|
Reference in New Issue
Block a user