删除动态路由的/api/action前缀,重构路由的注册逻辑确保静态路由优先注册

This commit is contained in:
jingrow 2025-10-12 01:05:00 +08:00
parent 3e7459a141
commit 4674fc8b32
3 changed files with 18 additions and 12 deletions

View File

@ -57,7 +57,7 @@ async def authenticate_request(request: Request, allow_guest: bool) -> bool:
logger.warning("认证失败: 未提供有效的认证信息")
return False
@router.post("/api/action/{module_path:path}")
@router.post("/{module_path:path}")
async def handle_action(request: Request, module_path: str):
"""
支持白名单函数调用

View File

@ -10,7 +10,22 @@ def include_routers_from_package(app: FastAPI, package: str, prefix: str = "") -
"""
registered = 0
pkg = importlib.import_module(package)
for module_info in pkgutil.iter_modules(pkg.__path__, package + "."):
# 收集所有模块信息
modules = list(pkgutil.iter_modules(pkg.__path__, package + "."))
# 分离静态和动态路由模块(静态路由优先注册)
static_modules = []
dynamic_modules = []
for module_info in modules:
if 'jingrow_action' in module_info.name:
dynamic_modules.append(module_info)
else:
static_modules.append(module_info)
# 按顺序注册:静态路由 -> 动态路由
for module_info in static_modules + dynamic_modules:
module_name = module_info.name
try:
mod = importlib.import_module(module_name)
@ -21,6 +36,7 @@ def include_routers_from_package(app: FastAPI, package: str, prefix: str = "") -
except Exception as e:
# 启动期容错:打印日志即可,避免因单个模块导致整体失败
print(f"[router:auto] failed to register {module_name}: {e}")
print(f"[router:auto] registered {registered} route modules from {package}")
return registered

View File

@ -25,7 +25,6 @@ export const deleteRecords = async (pagetype: string, names: string[]): Promise<
)
results.push(name)
} catch (error: any) {
console.error(`Failed to delete ${name}:`, error)
failed.push(name)
}
}
@ -38,7 +37,6 @@ export const deleteRecords = async (pagetype: string, names: string[]): Promise<
return { success: false, message: '删除失败' }
}
} catch (error: any) {
console.error('Error in deleteRecords:', error)
return { success: false, message: error.message || '删除失败' }
}
}
@ -58,7 +56,6 @@ export const createRecord = async (pagetype: string, data: Record<string, any>):
const message = response.data?.message
return { success: true, data: message || response.data, message: 'Created successfully' }
} catch (error: any) {
console.error('Error in createRecord:', error)
return { success: false, message: error.response?.data?.message || error.message || '创建失败' }
}
}
@ -120,7 +117,6 @@ export const getCount = async (pagetype: string): Promise<{ success: boolean; co
const count = response.data?.message
return { success: true, count: count || 0 }
} catch (error: any) {
console.error('Error in getCount:', error)
return {
success: false,
count: 0,
@ -147,7 +143,6 @@ export const getLocalJobCount = async (): Promise<{ success: boolean; count?: nu
message: '获取Local Job总数成功'
}
} catch (error: any) {
console.error('Error in getLocalJobCount:', error)
return {
success: false,
count: 0,
@ -186,7 +181,6 @@ export const getRecords = async (pagetype: string, filters: any[] = [], fields:
total: data?.total || data?.data?.length || 0
}
} catch (error: any) {
console.error('Error in getRecords:', error)
return {
success: false,
message: error.response?.data?.message || error.message || '获取记录列表失败'
@ -300,10 +294,7 @@ export const uploadFileToJingrow = async (
// 上传成功后,自动下载图片到本地
const downloadResult = await downloadImageToLocal(result.message.file_url, result.message.file_name)
if (downloadResult.success) {
console.log('图片已下载到本地:', downloadResult.local_path)
uploadResult.local_path = downloadResult.local_path
} else {
console.warn('下载图片到本地失败:', downloadResult.error)
}
return uploadResult
@ -314,7 +305,6 @@ export const uploadFileToJingrow = async (
return { success: false, error: `API请求失败 (HTTP ${response.status}): ${response.statusText}` }
}
} catch (error: any) {
console.error('Upload file error:', error)
return {
success: false,
error: error.response?.data?.message || error.message || '上传文件失败'