MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / initialize

Method initialize

src/storage/mongodb_manager.py:58–90  ·  view source on GitHub ↗

初始化 MongoDB 连接

(self)

Source from the content-addressed store, hash-verified

56 self._redis_enabled: bool = False
57
58 async def initialize(self) -> None:
59 """初始化 MongoDB 连接"""
60 if self._initialized:
61 return
62
63 try:
64 mongodb_uri = os.getenv("MONGODB_URI")
65 if not mongodb_uri:
66 raise ValueError("MONGODB_URI environment variable not set")
67
68 database_name = os.getenv("MONGODB_DATABASE", "gcli2api")
69
70 self._client = AsyncIOMotorClient(mongodb_uri)
71 self._db = self._client[database_name]
72
73 # 测试连接
74 await self._db.command("ping")
75
76 # 创建索引
77 await self._create_indexes()
78
79 # 加载配置到内存
80 await self._load_config_cache()
81
82 self._initialized = True
83 log.info(f"MongoDB storage initialized (database: {database_name})")
84
85 # 尝试初始化 Redis(可选)
86 await self._init_redis()
87
88 except Exception as e:
89 log.error(f"Error initializing MongoDB: {e}")
90 raise
91
92 async def _create_indexes(self):
93 """

Callers

nothing calls this directly

Calls 5

_create_indexesMethod · 0.95
_load_config_cacheMethod · 0.95
_init_redisMethod · 0.95
infoMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected