dify插件初步构建

This commit is contained in:
2025-12-30 13:38:32 +08:00
parent 8011dec826
commit c07fe6b938
27 changed files with 820 additions and 0 deletions

View File

@@ -0,0 +1 @@
# Core模块

View File

@@ -0,0 +1,42 @@
"""自定义异常和异常处理器"""
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from app.schemas.base import ResultDomain
class BusinessException(Exception):
"""业务异常"""
def __init__(self, code: int = 500, message: str = "业务异常"):
self.code = code
self.message = message
class NotFoundException(BusinessException):
"""资源不存在异常"""
def __init__(self, message: str = "资源不存在"):
super().__init__(code=404, message=message)
class ValidationException(BusinessException):
"""参数校验异常"""
def __init__(self, message: str = "参数校验失败"):
super().__init__(code=400, message=message)
def register_exception_handlers(app: FastAPI):
"""注册全局异常处理器"""
@app.exception_handler(BusinessException)
async def business_exception_handler(request: Request, exc: BusinessException):
return JSONResponse(
status_code=200,
content=ResultDomain.fail(message=exc.message, code=exc.code).model_dump()
)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
return JSONResponse(
status_code=500,
content=ResultDomain.fail(message=str(exc), code=500).model_dump()
)

View File

@@ -0,0 +1,26 @@
"""中间件定义"""
import time
import logging
from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware
logger = logging.getLogger(__name__)
class RequestLoggingMiddleware(BaseHTTPMiddleware):
"""请求日志中间件"""
async def dispatch(self, request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
logger.info(
f"{request.method} {request.url.path} "
f"- Status: {response.status_code} "
f"- Time: {process_time:.3f}s"
)
response.headers["X-Process-Time"] = str(process_time)
return response

View File

@@ -0,0 +1,128 @@
"""Redis 服务"""
import json
from typing import Any, Optional, Union
import redis.asyncio as redis
from redis.asyncio import Redis
from app.config import settings
class RedisService:
"""Redis 服务类"""
_client: Optional[Redis] = None
@classmethod
async def init(cls) -> None:
"""初始化 Redis 连接"""
cls._client = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
password=settings.REDIS_PASSWORD or None,
db=settings.REDIS_DB,
decode_responses=True
)
@classmethod
async def close(cls) -> None:
"""关闭 Redis 连接"""
if cls._client:
await cls._client.close()
cls._client = None
@classmethod
def get_client(cls) -> Redis:
"""获取 Redis 客户端"""
if not cls._client:
raise RuntimeError("Redis 未初始化,请先调用 init()")
return cls._client
# ==================== String 操作 ====================
@classmethod
async def get(cls, key: str) -> Optional[str]:
"""获取值"""
return await cls.get_client().get(key)
@classmethod
async def set(cls, key: str, value: Union[str, int, float], expire: Optional[int] = None) -> bool:
"""设置值"""
return await cls.get_client().set(key, value, ex=expire)
@classmethod
async def delete(cls, *keys: str) -> int:
"""删除键"""
return await cls.get_client().delete(*keys)
@classmethod
async def exists(cls, key: str) -> bool:
"""判断键是否存在"""
return await cls.get_client().exists(key) > 0
@classmethod
async def expire(cls, key: str, seconds: int) -> bool:
"""设置过期时间"""
return await cls.get_client().expire(key, seconds)
@classmethod
async def ttl(cls, key: str) -> int:
"""获取剩余过期时间"""
return await cls.get_client().ttl(key)
# ==================== JSON 操作 ====================
@classmethod
async def get_json(cls, key: str) -> Optional[Any]:
"""获取 JSON 值"""
value = await cls.get(key)
return json.loads(value) if value else None
@classmethod
async def set_json(cls, key: str, value: Any, expire: Optional[int] = None) -> bool:
"""设置 JSON 值"""
return await cls.set(key, json.dumps(value, ensure_ascii=False), expire)
# ==================== Hash 操作 ====================
@classmethod
async def hget(cls, name: str, key: str) -> Optional[str]:
"""获取 Hash 字段值"""
return await cls.get_client().hget(name, key)
@classmethod
async def hset(cls, name: str, key: str, value: str) -> int:
"""设置 Hash 字段值"""
return await cls.get_client().hset(name, key, value)
@classmethod
async def hgetall(cls, name: str) -> dict:
"""获取 Hash 所有字段"""
return await cls.get_client().hgetall(name)
@classmethod
async def hdel(cls, name: str, *keys: str) -> int:
"""删除 Hash 字段"""
return await cls.get_client().hdel(name, *keys)
# ==================== List 操作 ====================
@classmethod
async def lpush(cls, key: str, *values: str) -> int:
"""左侧插入列表"""
return await cls.get_client().lpush(key, *values)
@classmethod
async def rpush(cls, key: str, *values: str) -> int:
"""右侧插入列表"""
return await cls.get_client().rpush(key, *values)
@classmethod
async def lrange(cls, key: str, start: int = 0, end: int = -1) -> list:
"""获取列表范围"""
return await cls.get_client().lrange(key, start, end)
@classmethod
async def llen(cls, key: str) -> int:
"""获取列表长度"""
return await cls.get_client().llen(key)