Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
2 changes: 2 additions & 0 deletions ruoyi-fastapi-backend/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ APP_DEMO_MODE = false
APP_DISABLE_SWAGGER = false
# 应用是否禁用ReDoc文档
APP_DISABLE_REDOC = false
# 应用是否启用手动导入路由(默认为自动扫描routers目录)
APP_MANUAL_IMPORT_ROUTERS = false

# -------- Jwt配置 --------
# Jwt秘钥
Expand Down
2 changes: 2 additions & 0 deletions ruoyi-fastapi-backend/.env.dockermy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ APP_DEMO_MODE = false
APP_DISABLE_SWAGGER = true
# 应用是否禁用ReDoc文档
APP_DISABLE_REDOC = true
# 应用是否启用手动导入路由(默认为自动扫描routers目录)
APP_MANUAL_IMPORT_ROUTERS = false

# -------- Jwt配置 --------
# Jwt秘钥
Expand Down
2 changes: 2 additions & 0 deletions ruoyi-fastapi-backend/.env.dockerpg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ APP_SAME_TIME_LOGIN = true
APP_DISABLE_SWAGGER = true
# 应用是否禁用ReDoc文档
APP_DISABLE_REDOC = true
# 应用是否启用手动导入路由(默认为自动扫描routers目录)
APP_MANUAL_IMPORT_ROUTERS = false

# -------- Jwt配置 --------
# Jwt秘钥
Expand Down
2 changes: 2 additions & 0 deletions ruoyi-fastapi-backend/.env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ APP_DEMO_MODE = false
APP_DISABLE_SWAGGER = true
# 应用是否禁用ReDoc文档
APP_DISABLE_REDOC = true
# 应用是否启用手动导入路由(默认为自动扫描routers目录)
APP_MANUAL_IMPORT_ROUTERS = false

# -------- Jwt配置 --------
# Jwt秘钥
Expand Down
43 changes: 42 additions & 1 deletion ruoyi-fastapi-backend/common/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from starlette.types import ASGIApp, Lifespan
from typing_extensions import deprecated

from config.env import AppConfig


class APIRouterPro(APIRouter):
"""
Expand Down Expand Up @@ -308,7 +310,41 @@ def _find_controller_files(self) -> list[str]:
:return: py文件路径列表
"""
pattern = os.path.join(self.project_root, '*', 'controller', '[!_]*.py')
return sorted(glob.glob(pattern))
files = glob.glob(pattern)
# 去重并保持顺序 VSCode 调试模式 + Uvicorn 热重载 环境下,可能返回重复的文件路径。 :os.path.join(self.project_root, '*', 'controller', '[!_]*.py') 中的 * 可能匹配到符号链接指向的同一物理目录,导致同一个文件通过不同路径(真实路径 vs 链接路径)被重复匹配。
return sorted(dict.fromkeys(files))

def _exclude_controller_file_and_manual_import_routers(
self, all_file_names: list[str]
) -> tuple[list[str], list[tuple[str, APIRouter]]]:
"""
排除指定的controller文件,并手动导入路由实例(性能比较烂的朋友在debug+reload的时候可以快一点)

:param file_name: 要排除的controller文件名
:return: 路由实例列表
"""
exclude_file_names = [
'ai_chat_controller.py',
'ai_model_controller.py',
]

if not exclude_file_names:
return all_file_names, []

all_file_names_exclude = [] # 过滤后的文件名列表
all_file_names_exclude = [
fname for fname in all_file_names if not any(exclude in fname for exclude in exclude_file_names)
]

routers = []
# 手动导入路由实例
from module_ai.controller import ai_chat_controller, ai_model_controller # noqa: PLC0415

# 这个文件太大了,电脑性能很差的用户动态导入会很久5s+
routers.append(('module_ai.controller.ai_chat_controller', ai_chat_controller.ai_chat_controller))
routers.append(('module_ai.controller.ai_model_controller', ai_model_controller.ai_model_controller))

return all_file_names_exclude, routers

def _import_module_and_get_routers(self, controller_files: list[str]) -> list[tuple[str, APIRouter]]:
"""
Expand All @@ -318,6 +354,11 @@ def _import_module_and_get_routers(self, controller_files: list[str]) -> list[tu
:return: 路由实例列表
"""
routers = []

if AppConfig.app_manual_import_routers:
controller_files, manual_routers = self._exclude_controller_file_and_manual_import_routers(controller_files)
routers.extend(manual_routers)

for file_path in controller_files:
# 计算模块路径
relative_path = os.path.relpath(file_path, self.project_root)
Expand Down
1 change: 1 addition & 0 deletions ruoyi-fastapi-backend/config/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AppSettings(BaseSettings):
app_demo_mode: bool = False
app_disable_swagger: bool = False
app_disable_redoc: bool = False
app_manual_import_routers: bool = False


class JwtSettings(BaseSettings):
Expand Down