style: 统一代码格式和字符串引号使用

refactor: 优化代码结构和可读性

feat: 添加http_limit模块实现请求限制功能

fix: 修复异步任务中使用time.sleep的问题

chore: 更新依赖项并添加pytest测试框架

docs: 更新项目描述信息

perf: 优化Redis序列化方式使用JSON替代pickle

test: 添加测试相关配置和依赖
This commit is contained in:
zhangtao
2026-01-17 20:07:25 +08:00
parent 32b8f18b17
commit 1d54dca76c
139 changed files with 4920 additions and 2826 deletions
@@ -22,7 +22,7 @@ ParamsRouter = APIRouter(route_class=OperationLogRoute, prefix="/param", tags=["
@ParamsRouter.get("/detail/{id}", summary="获取参数详情", description="获取参数详情")
async def get_type_detail_controller(
id: Annotated[int, Path(description="参数ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:detail"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:detail"]))],
) -> JSONResponse:
"""
获取参数详情
@@ -39,10 +39,14 @@ async def get_type_detail_controller(
return SuccessResponse(data=result_dict, msg="获取参数详情成功")
@ParamsRouter.get("/key/{config_key}", summary="根据配置键获取参数详情", description="根据配置键获取参数详情")
@ParamsRouter.get(
"/key/{config_key}",
summary="根据配置键获取参数详情",
description="根据配置键获取参数详情",
)
async def get_obj_by_key_controller(
config_key: Annotated[str, Path(description="配置键")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))],
) -> JSONResponse:
"""
根据配置键获取参数详情
@@ -59,10 +63,14 @@ async def get_obj_by_key_controller(
return SuccessResponse(data=result_dict, msg="根据配置键获取参数详情成功")
@ParamsRouter.get("/value/{config_key}", summary="根据配置键获取参数值", description="根据配置键获取参数值")
@ParamsRouter.get(
"/value/{config_key}",
summary="根据配置键获取参数值",
description="根据配置键获取参数值",
)
async def get_config_value_by_key_controller(
config_key: Annotated[str, Path(description="配置键")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))],
) -> JSONResponse:
"""
根据配置键获取参数值
@@ -74,7 +82,9 @@ async def get_config_value_by_key_controller(
返回:
- JSONResponse: 包含参数值的 JSON 响应
"""
result_value = await ParamsService.get_config_value_by_key_service(config_key=config_key, auth=auth)
result_value = await ParamsService.get_config_value_by_key_service(
config_key=config_key, auth=auth
)
log.info(f"根据配置键获取参数值成功 {config_key}")
return SuccessResponse(data=result_value, msg="根据配置键获取参数值成功")
@@ -96,8 +106,14 @@ async def get_obj_list_controller(
返回:
- JSONResponse: 包含参数列表的 JSON 响应
"""
result_dict_list = await ParamsService.get_obj_list_service(auth=auth, search=search, order_by=page.order_by)
result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
result_dict_list = await ParamsService.get_obj_list_service(
auth=auth, search=search, order_by=page.order_by
)
result_dict = await PaginationService.paginate(
data_list=result_dict_list,
page_no=page.page_no,
page_size=page.page_size,
)
log.info("获取参数列表成功")
return SuccessResponse(data=result_dict, msg="查询参数列表成功")
@@ -106,7 +122,7 @@ async def get_obj_list_controller(
async def create_obj_controller(
data: ParamsCreateSchema,
redis: Annotated[Redis, Depends(redis_getter)],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:create"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:create"]))],
) -> JSONResponse:
"""
创建参数
@@ -129,7 +145,7 @@ async def update_objs_controller(
data: ParamsUpdateSchema,
id: Annotated[int, Path(description="参数ID")],
redis: Annotated[Redis, Depends(redis_getter)],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:update"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:update"]))],
) -> JSONResponse:
"""
修改参数
@@ -152,7 +168,7 @@ async def update_objs_controller(
async def delete_obj_controller(
redis: Annotated[Redis, Depends(redis_getter)],
ids: Annotated[list[int], Body(description="ID列表")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:delete"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:delete"]))],
) -> JSONResponse:
"""
删除参数
@@ -170,10 +186,10 @@ async def delete_obj_controller(
return SuccessResponse(msg="删除参数成功")
@ParamsRouter.post('/export', summary="导出参数", description="导出参数")
@ParamsRouter.post("/export", summary="导出参数", description="导出参数")
async def export_obj_list_controller(
search: Annotated[ParamsQueryParam, Depends()],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:export"]))]
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:export"]))],
) -> StreamingResponse:
"""
导出参数
@@ -187,22 +203,21 @@ async def export_obj_list_controller(
"""
result_dict_list = await ParamsService.get_obj_list_service(search=search, auth=auth)
export_result = await ParamsService.export_obj_service(data_list=result_dict_list)
log.info('导出参数成功')
log.info("导出参数成功")
return StreamResponse(
data=bytes2file_response(export_result),
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers={
'Content-Disposition': 'attachment; filename=params.xlsx'
}
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": "attachment; filename=params.xlsx"},
)
@ParamsRouter.post("/upload", summary="上传文件", dependencies=[Depends(AuthPermission(["module_system:param:upload"]))])
async def upload_file_controller(
file: UploadFile,
request: Request
) -> JSONResponse:
@ParamsRouter.post(
"/upload",
summary="上传文件",
dependencies=[Depends(AuthPermission(["module_system:param:upload"]))],
)
async def upload_file_controller(file: UploadFile, request: Request) -> JSONResponse:
"""
上传文件
@@ -215,7 +230,7 @@ async def upload_file_controller(
"""
result_str = await ParamsService.upload_service(base_url=str(request.base_url), file=file)
log.info(f"上传文件: {result_str}")
return SuccessResponse(data=result_str, msg='上传文件成功')
return SuccessResponse(data=result_str, msg="上传文件成功")
@ParamsRouter.get("/info", summary="获取初始化缓存参数", description="获取初始化缓存参数")
@@ -33,7 +33,9 @@ class ParamsCRUD(CRUDBase[ParamsModel, ParamsCreateSchema, ParamsUpdateSchema]):
"""
return await self.get(id=id, preload=preload)
async def get_obj_by_key_crud(self, key: str, preload: list | None = None) -> ParamsModel | None:
async def get_obj_by_key_crud(
self, key: str, preload: list | None = None
) -> ParamsModel | None:
"""
根据key获取配置管理型详情
@@ -46,7 +48,12 @@ class ParamsCRUD(CRUDBase[ParamsModel, ParamsCreateSchema, ParamsUpdateSchema]):
"""
return await self.get(config_key=key, preload=preload)
async def get_obj_list_crud(self, search: dict | None = None, order_by: list | None = None, preload: list | None = None) -> Sequence[ParamsModel]:
async def get_obj_list_crud(
self,
search: dict | None = None,
order_by: list | None = None,
preload: list | None = None,
) -> Sequence[ParamsModel]:
"""
获取配置管理型列表
@@ -8,11 +8,17 @@ class ParamsModel(ModelMixin):
"""
参数配置表
"""
__tablename__: str = "sys_param"
__table_args__: dict[str, str] = ({'comment': '系统参数表'})
__table_args__: dict[str, str] = {"comment": "系统参数表"}
__loader_options__: list[str] = []
config_name: Mapped[str] = mapped_column(String(64), nullable=False, comment='参数名称')
config_key: Mapped[str] = mapped_column(String(500), nullable=False, comment='参数键名')
config_value: Mapped[str | None] = mapped_column(String(500), comment='参数键值')
config_type: Mapped[bool] = mapped_column(Boolean, default=False, nullable=True, comment="系统内置(True:是 False:否)")
config_name: Mapped[str] = mapped_column(String(64), nullable=False, comment="参数名称")
config_key: Mapped[str] = mapped_column(String(500), nullable=False, comment="参数键名")
config_value: Mapped[str | None] = mapped_column(String(500), comment="参数键值")
config_type: Mapped[bool] = mapped_column(
Boolean,
default=False,
nullable=True,
comment="系统内置(True:是 False:否)",
)
@@ -7,6 +7,7 @@ from app.core.validator import DateTimeStr
class ParamsCreateSchema(BaseModel):
"""配置创建模型"""
config_name: str = Field(..., max_length=64, description="参数名称")
config_key: str = Field(..., max_length=500, description="参数键名")
config_value: str | None = Field(default=None, description="参数键值")
@@ -14,13 +15,14 @@ class ParamsCreateSchema(BaseModel):
status: str = Field(default="0", description="状态(True:正常 False:停用)")
description: str | None = Field(default=None, max_length=500, description="描述")
@field_validator('config_key')
@field_validator("config_key")
@classmethod
def _validate_config_key(cls, v: str) -> str:
v = v.strip().lower()
import re
if not re.match(r'^[a-z][a-z0-9_.-]*$', v):
raise ValueError('参数键名必须以小写字母开头,仅包含小写字母/数字/_.-')
if not re.match(r"^[a-z][a-z0-9_.-]*$", v):
raise ValueError("参数键名必须以小写字母开头,仅包含小写字母/数字/_.-")
return v
@@ -30,6 +32,7 @@ class ParamsUpdateSchema(ParamsCreateSchema):
class ParamsOutSchema(ParamsCreateSchema, BaseSchema):
"""配置响应模型"""
model_config = ConfigDict(from_attributes=True)
@@ -43,8 +46,16 @@ class ParamsQueryParam:
config_type: bool | None = Query(None, description="系统内置((True:是 False:否))"),
description: str | None = Query(None, description="描述"),
status: str | None = Query(None, description="是否启用"),
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"])
created_time: list[DateTimeStr] | None = Query(
None,
description="创建时间范围",
examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"],
),
updated_time: list[DateTimeStr] | None = Query(
None,
description="更新时间范围",
examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"],
),
) -> None:
# 模糊查询字段
# 模糊查询字段
@@ -14,13 +14,19 @@ from app.utils.excel_util import ExcelUtil
from app.utils.upload_util import UploadUtil
from .crud import ParamsCRUD
from .schema import ParamsCreateSchema, ParamsOutSchema, ParamsQueryParam, ParamsUpdateSchema
from .schema import (
ParamsCreateSchema,
ParamsOutSchema,
ParamsQueryParam,
ParamsUpdateSchema,
)
class ParamsService:
"""
配置管理模块服务层
"""
@classmethod
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> dict:
"""
@@ -50,7 +56,7 @@ class ParamsService:
"""
obj = await ParamsCRUD(auth).get_obj_by_key_crud(key=config_key)
if not obj:
raise CustomException(msg=f'配置键 {config_key} 不存在')
raise CustomException(msg=f"配置键 {config_key} 不存在")
return ParamsOutSchema.model_validate(obj).model_dump()
@classmethod
@@ -67,11 +73,16 @@ class ParamsService:
"""
obj = await ParamsCRUD(auth).get_obj_by_key_crud(key=config_key)
if not obj:
raise CustomException(msg=f'配置键 {config_key} 不存在')
raise CustomException(msg=f"配置键 {config_key} 不存在")
return obj.config_value
@classmethod
async def get_obj_list_service(cls, auth: AuthSchema, search: ParamsQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
async def get_obj_list_service(
cls,
auth: AuthSchema,
search: ParamsQueryParam | None = None,
order_by: list[dict] | None = None,
) -> list[dict]:
"""
获取配置管理型列表
@@ -85,13 +96,17 @@ class ParamsService:
"""
obj_list = None
if search:
obj_list = await ParamsCRUD(auth).get_obj_list_crud(search=search.__dict__, order_by=order_by)
obj_list = await ParamsCRUD(auth).get_obj_list_crud(
search=search.__dict__, order_by=order_by
)
else:
obj_list = await ParamsCRUD(auth).get_obj_list_crud()
return [ParamsOutSchema.model_validate(obj).model_dump() for obj in obj_list]
@classmethod
async def create_obj_service(cls, auth: AuthSchema, redis: Redis, data: ParamsCreateSchema) -> dict:
async def create_obj_service(
cls, auth: AuthSchema, redis: Redis, data: ParamsCreateSchema
) -> dict:
"""
创建配置管理型
@@ -105,7 +120,7 @@ class ParamsService:
"""
exist_obj = await ParamsCRUD(auth).get(config_key=data.config_key)
if exist_obj:
raise CustomException(msg='创建失败,该配置key已存在')
raise CustomException(msg="创建失败,该配置key已存在")
obj = await ParamsCRUD(auth).create_obj_crud(data=data)
new_obj_dict = ParamsOutSchema.model_validate(obj).model_dump()
@@ -127,7 +142,9 @@ class ParamsService:
return new_obj_dict
@classmethod
async def update_obj_service(cls, auth: AuthSchema, redis: Redis, id: int, data: ParamsUpdateSchema) -> dict:
async def update_obj_service(
cls, auth: AuthSchema, redis: Redis, id: int, data: ParamsUpdateSchema
) -> dict:
"""
更新配置管理型
@@ -142,13 +159,13 @@ class ParamsService:
"""
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
if not exist_obj:
raise CustomException(msg='更新失败,该数系统配置不存在')
raise CustomException(msg="更新失败,该数系统配置不存在")
if exist_obj.config_key != data.config_key:
raise CustomException(msg='更新失败,系统配置key不允许修改')
raise CustomException(msg="更新失败,系统配置key不允许修改")
new_obj = await ParamsCRUD(auth).update_obj_crud(id=id, data=data)
if not new_obj:
raise CustomException(msg='更新失败,系统配置不存在')
raise CustomException(msg="更新失败,系统配置不存在")
new_obj_dict = ParamsOutSchema.model_validate(new_obj).model_dump()
# 同步redis
@@ -182,15 +199,17 @@ class ParamsService:
- None
"""
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
raise CustomException(msg="删除失败,删除对象不能为空")
for id in ids:
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
if not exist_obj:
raise CustomException(msg='删除失败,该数据字典类型不存在')
raise CustomException(msg="删除失败,该数据字典类型不存在")
# 检查是否是否初始化类型
if exist_obj.config_type:
# 如果有字典数据,不能删除
raise CustomException(msg=f'{exist_obj.config_name} 删除失败,系统初始化配置不可以删除')
raise CustomException(
msg=f"{exist_obj.config_name} 删除失败,系统初始化配置不可以删除"
)
await ParamsCRUD(auth).delete_obj_crud(ids=ids)
@@ -219,24 +238,28 @@ class ParamsService:
- bytes: Excel文件二进制数据
"""
mapping_dict = {
'id': '编号',
'config_name': '参数名称',
'config_key': '参数键名',
'config_value': '参数键值',
'config_type': '系统内置((True:是 False:否))',
'description': '备注',
'created_time': '创建时间',
'updated_time': '更新时间',
'created_id': '创建者ID',
'updated_id': '更新者ID',
"id": "编号",
"config_name": "参数名称",
"config_key": "参数键名",
"config_value": "参数键值",
"config_type": "系统内置((True:是 False:否))",
"description": "备注",
"created_time": "创建时间",
"updated_time": "更新时间",
"created_id": "创建者ID",
"updated_id": "更新者ID",
}
# 复制数据并转换状态
data = data_list.copy()
for item in data:
# 处理状态
item['config_type'] = '' if item.get('config_type') else ''
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
item["config_type"] = "" if item.get("config_type") else ""
item["creator"] = (
item.get("creator", {}).get("name", "未知")
if isinstance(item.get("creator"), dict)
else "未知"
)
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
@@ -255,10 +278,10 @@ class ParamsService:
filename, filepath, file_url = await UploadUtil.upload_file(file=file, base_url=base_url)
return UploadResponseSchema(
file_path=f'{filepath}',
file_path=f"{filepath}",
file_name=filename,
origin_name=file.filename,
file_url=f'{file_url}',
file_url=f"{file_url}",
).model_dump()
@classmethod
@@ -282,7 +305,7 @@ class ParamsService:
try:
# 保存到Redis并设置过期时间
for config in config_obj:
redis_key = (f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{config.config_key}")
redis_key = f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{config.config_key}"
config_obj_dict = ParamsOutSchema.model_validate(config).model_dump()
value = json.dumps(config_obj_dict, ensure_ascii=False)
result = await RedisCURD(redis).set(
@@ -338,7 +361,7 @@ class ParamsService:
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:demo_enable",
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:ip_white_list",
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:white_api_list_path",
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:ip_black_list"
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:ip_black_list",
]
# 批量获取配置
@@ -349,20 +372,23 @@ class ParamsService:
"demo_enable": False,
"ip_white_list": [],
"white_api_list_path": [],
"ip_black_list": []
"ip_black_list": [],
}
# 解析演示模式配置
if config_values[0]:
try:
demo_config = json.loads(config_values[0])
config_result["demo_enable"] = demo_config.get("config_value", False) if isinstance(demo_config, dict) else False
config_result["demo_enable"] = (
demo_config.get("config_value", False)
if isinstance(demo_config, dict)
else False
)
except json.JSONDecodeError:
log.error("解析演示模式配置失败")
# 解析IP白名单配置
if config_values[1]:
try:
ip_white_config = json.loads(config_values[1])
# 确保是列表类型
@@ -375,7 +401,9 @@ class ParamsService:
try:
white_api_config = json.loads(config_values[2])
# 确保是列表类型
config_result["white_api_list_path"] = json.loads(white_api_config.get("config_value", []))
config_result["white_api_list_path"] = json.loads(
white_api_config.get("config_value", [])
)
except json.JSONDecodeError:
log.error("解析API白名单配置失败")