mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
refactor: 重构服务层方法命名规范
fix: 修复文件下载和删除功能 fix: 修复Redis哈希获取方法返回值类型 fix: 修复权限检查逻辑 perf: 优化部门和服务详情查询性能 perf: 优化角色数据范围显示 style: 清理无用导入和注释 style: 统一CRUD方法命名 docs: 更新main.py中的命令说明 chore: 移动IP定位工具类位置 chore: 更新.gitignore忽略迁移版本文件
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from redis import asyncio as aioredis
|
||||
from redis.asyncio import Redis
|
||||
from redis import exceptions
|
||||
from motor.motor_asyncio import AsyncIOMotorClient
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy import create_engine, Engine
|
||||
@@ -59,14 +60,14 @@ def session_connect() -> AsyncSession:
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"数据库连接失败: {e}")
|
||||
|
||||
async def redis_connect(app: FastAPI, status: bool) -> aioredis.Redis:
|
||||
async def redis_connect(app: FastAPI, status: bool) -> Redis | None:
|
||||
"""创建或关闭Redis连接"""
|
||||
if not settings.REDIS_ENABLE:
|
||||
raise CustomException(msg="请先开启Redis连接", data="请启用 app/core/config.py: REDIS_ENABLE")
|
||||
|
||||
if status:
|
||||
try:
|
||||
rd = await aioredis.from_url(
|
||||
rd = await Redis.from_url(
|
||||
url=settings.REDIS_URI,
|
||||
encoding='utf-8',
|
||||
decode_responses=True,
|
||||
@@ -79,17 +80,17 @@ async def redis_connect(app: FastAPI, status: bool) -> aioredis.Redis:
|
||||
logger.info("Redis连接成功...")
|
||||
return rd
|
||||
raise CustomException(msg="Redis连接失败")
|
||||
except aioredis.AuthenticationError as e:
|
||||
raise aioredis.AuthenticationError(f"Redis认证失败: {e}")
|
||||
except aioredis.TimeoutError as e:
|
||||
raise aioredis.TimeoutError(f"Redis连接超时: {e}")
|
||||
except aioredis.RedisError as e:
|
||||
raise aioredis.RedisError(f"Redis连接错误: {e}")
|
||||
except exceptions.AuthenticationError as e:
|
||||
raise exceptions.AuthenticationError(f"Redis认证失败: {e}")
|
||||
except exceptions.TimeoutError as e:
|
||||
raise exceptions.TimeoutError(f"Redis连接超时: {e}")
|
||||
except exceptions.RedisError as e:
|
||||
raise exceptions.RedisError(f"Redis连接错误: {e}")
|
||||
else:
|
||||
await app.state.redis.close()
|
||||
logger.info('Redis连接已关闭')
|
||||
|
||||
async def mongodb_connect(app: FastAPI, status: bool) -> AsyncIOMotorClient:
|
||||
async def mongodb_connect(app: FastAPI, status: bool) -> AsyncIOMotorClient | None:
|
||||
"""创建或关闭MongoDB连接"""
|
||||
if not settings.MONGO_DB_ENABLE:
|
||||
raise CustomException(msg="请先开启MongoDB连接", data="请启用 app/core/config.py: MONGO_DB_ENABLE")
|
||||
@@ -107,7 +108,7 @@ async def mongodb_connect(app: FastAPI, status: bool) -> AsyncIOMotorClient:
|
||||
app.state.mongo = client[settings.MONGO_DB_NAME]
|
||||
data = await client.server_info()
|
||||
logger.info("MongoDB连接成功...", data)
|
||||
return data
|
||||
return client
|
||||
except Exception as e:
|
||||
raise ValueError(f"MongoDB连接失败: {e}")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user