优化白名单函数实现机制

This commit is contained in:
jingrow 2025-10-29 16:19:34 +08:00
parent eed34b73e4
commit fa42e6bb00

View File

@ -63,51 +63,48 @@ async def handle_request(request: Request, module_path: str):
处理白名单路由请求
"""
try:
# 首先检查是否是白名单函数
# 解析模块路径并先导入模块,确保装饰器执行
module_info = parse_module_path(module_path)
module = import_module(module_info['module_path'])
# 模块导入后再检查是否是白名单函数
if is_whitelisted(module_path):
whitelist_info = get_whitelisted_function(module_path)
func = whitelist_info['function']
# 检查HTTP方法
if request.method not in whitelist_info['methods']:
raise HTTPException(status_code=405, detail=f"Method {request.method} not allowed for this endpoint")
# 检查权限(如果需要)
if not whitelist_info['allow_guest']:
if not await authenticate_request(request, whitelist_info['allow_guest']):
raise HTTPException(status_code=401, detail="Authentication required")
# 获取请求数据
request_data = await request.json()
# 调用函数
result = func(**request_data)
return JSONResponse(content={
"success": True,
"data": result
})
# 如果不是白名单函数,使用原有的动态导入方式
# 解析模块路径
module_info = parse_module_path(module_path)
# 动态导入模块
module = import_module(module_info['module_path'])
# 获取函数
# 非白名单:按原逻辑调用模块函数
function_name = module_info['function_name']
if not hasattr(module, function_name):
raise HTTPException(status_code=404, detail=f"Function {function_name} not found in module {module_info['module_path']}")
func = getattr(module, function_name)
# 获取请求数据
request_data = await request.json()
# 调用函数
result = func(**request_data)
return JSONResponse(content={
"success": True,
"data": result