Skip to content

Commit 0949a0e

Browse files
committed
更新说明
1 parent 674257b commit 0949a0e

File tree

3 files changed

+259
-53
lines changed

3 files changed

+259
-53
lines changed

.env.example

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,26 @@ PASSWORD=pwd
3737
# 存储配置
3838
# ================================================================
3939

40-
# MongoDB 分布式存储模式配置
40+
# 存储后端优先级: Redis > MongoDB > 本地文件存储
41+
# 系统会自动选择可用的最高优先级存储后端
42+
43+
# Redis 分布式存储模式配置 (最高优先级)
44+
# 设置 REDIS_URI 后自动启用 Redis 模式,性能最佳
45+
46+
# Redis 连接字符串 (设置后启用 Redis 分布式存储模式)
47+
# 本地 Redis: redis://localhost:6379
48+
# 带密码: redis://:password@localhost:6379
49+
# SSL 连接: rediss://default:password@host:6380
50+
# Upstash Redis: rediss://default:token@host:6379
51+
# 默认: 无 (尝试下一个存储后端)
52+
REDIS_URI=redis://localhost:6379
53+
54+
# Redis 数据库索引 (仅在启用 Redis 模式时有效)
55+
# 可选范围: 0-15
56+
# 默认: 0
57+
REDIS_DATABASE=0
58+
59+
# MongoDB 分布式存储模式配置 (第二优先级)
4160
# 设置 MONGODB_URI 后自动启用 MongoDB 模式,不再使用本地文件存储
4261

4362
# MongoDB 连接字符串 (设置后启用 MongoDB 分布式存储模式)
@@ -78,18 +97,6 @@ AUTO_LOAD_ENV_CREDS=false
7897
# 默认: 100
7998
CALLS_PER_ROTATION=100
8099

81-
# ================================================================
82-
# 网络配置
83-
# ================================================================
84-
85-
# HTTP 请求超时时间 (秒)
86-
# 默认: 30.0
87-
HTTP_TIMEOUT=30.0
88-
89-
# 最大连接数
90-
# 默认: 100
91-
MAX_CONNECTIONS=100
92-
93100
# 代理配置 (可选)
94101
# 支持 http, https, socks5 代理
95102
# 格式: http://proxy:port, https://proxy:port, socks5://proxy:port
@@ -167,15 +174,16 @@ ANTI_TRUNCATION_MAX_ATTEMPTS=3
167174
# 环境变量使用说明
168175
# ================================================================
169176

170-
# 1. 存储模式配置:
171-
# - 文件存储模式 (默认): 不设置 MONGODB_URI,数据存储在本地 creds/ 目录
172-
# - MongoDB 分布式模式: 设置 MONGODB_URI,数据存储在 MongoDB 数据库
173-
# - 自动切换: 系统根据 MONGODB_URI 是否设置自动选择存储模式
177+
# 1. 存储模式配置 (按优先级自动选择):
178+
# - Redis 分布式模式 (最高优先级): 设置 REDIS_URI,数据存储在 Redis 数据库,性能最佳
179+
# - MongoDB 分布式模式 (第二优先级): 设置 MONGODB_URI,数据存储在 MongoDB 数据库
180+
# - 文件存储模式 (默认): 不设置上述 URI,数据存储在本地 creds/ 目录
181+
# - 自动切换: 系统根据可用的存储配置自动选择最高优先级的存储后端
174182

175183
# 2. 凭证配置方式 (三选一):
176184
# a) 将 Google 凭证 JSON 文件放在 CREDENTIALS_DIR 目录中 (仅文件模式)
177-
# b) 通过 GOOGLE_CREDENTIALS 等环境变量直接提供
178-
# c) 设置 AUTO_LOAD_ENV_CREDS=true 自动从环境变量加载
185+
# b) 设置 AUTO_LOAD_ENV_CREDS=true,通过 GOOGLE_CREDENTIALS 等环境变量直接提供
186+
# c) 通过面板导入
179187

180188
# 3. 密码配置优先级:
181189
# a) PASSWORD 环境变量 (最高优先级,设置后覆盖其他密码)
@@ -205,25 +213,13 @@ ANTI_TRUNCATION_MAX_ATTEMPTS=3
205213
# - 假流式/gemini-2.5-pro-maxthinking
206214
# - 流式抗截断/gemini-2.5-flash-search
207215

208-
# 8. MongoDB 分布式存储模式:
209-
# - 启用方法: 设置 MONGODB_URI 环境变量
210-
# - 数据迁移: 使用 python mongodb_setup.py migrate 命令
211-
# - 管理工具: 使用 python mongodb_setup.py 交互式界面
212-
# - 适用场景: 多实例部署、集群环境、大规模凭证管理
213-
# - 性能优势: 查询速度提升40-70%,支持高并发访问
214-
215216
# ================================================================
216217
# 配置文件说明
217218
# ================================================================
218219

219220
# 除了环境变量,你还可以使用 TOML 配置文件进行配置
220221
# 配置文件位置: {CREDENTIALS_DIR}/config.toml
221222
#
222-
# 示例 config.toml 内容:
223-
# # MongoDB 分布式存储配置
224-
# mongodb_uri = "mongodb://localhost:27017" # MongoDB 连接字符串
225-
# mongodb_database = "gcli2api" # MongoDB 数据库名称
226-
#
227223
# # 密码配置
228224
# api_password = "your_api_password" # 聊天API密码
229225
# panel_password = "your_panel_password" # 控制面板密码

README.md

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,45 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
290290
- `x-goog-api-key: your_api_password`
291291
- URL 参数:`?key=your_api_password`
292292

293-
## 🍃 MongoDB 分布式存储模式
293+
## 💾 分布式存储模式
294294

295-
### 🌟 新增功能
295+
### 🌟 存储后端优先级
296296

297-
gcli2api 现已支持 **MongoDB 存储模式**,
297+
gcli2api 支持多种存储后端,按优先级自动选择:**Redis > MongoDB > 本地文件**
298+
299+
### ⚡ Redis 分布式存储模式
300+
301+
### ⚙️ 启用 Redis 模式
302+
303+
**步骤 1: 配置 Redis 连接**
304+
```bash
305+
# 本地 Redis
306+
export REDIS_URI="redis://localhost:6379"
307+
308+
# 带密码的 Redis
309+
export REDIS_URI="redis://:password@localhost:6379"
310+
311+
# SSL 连接(推荐生产环境)
312+
export REDIS_URI="rediss://default:password@host:6380"
313+
314+
# Upstash Redis(免费云服务)
315+
export REDIS_URI="rediss://default:token@your-host.upstash.io:6379"
316+
317+
# 可选:自定义数据库索引(默认: 0)
318+
export REDIS_DATABASE="1"
319+
```
320+
321+
**步骤 2: 启动应用**
322+
```bash
323+
# 应用会自动检测 Redis 配置并优先使用 Redis 存储
324+
python web.py
325+
```
326+
327+
### 🍃 MongoDB 分布式存储模式
328+
329+
### 🌟 备选存储方案
330+
331+
如果未配置 Redis,gcli2api 将尝试使用 **MongoDB 存储模式**,
298332

299333
### ⚙️ 启用 MongoDB 模式
300334

@@ -319,22 +353,6 @@ export MONGODB_DATABASE="my_gcli_db"
319353
python web.py
320354
```
321355

322-
### 🔄 数据迁移
323-
324-
提供完整的数据迁移工具,支持本地文件与 MongoDB 之间的双向迁移:
325-
326-
**使用管理脚本(推荐)**
327-
```bash
328-
# 启动交互式管理界面
329-
python mongodb_setup.py
330-
331-
# 直接命令行操作
332-
python mongodb_setup.py status # 查看当前存储状态
333-
python mongodb_setup.py check # 检查 MongoDB 连接
334-
python mongodb_setup.py migrate # 迁移数据到 MongoDB
335-
python mongodb_setup.py export # 从 MongoDB 导出数据
336-
```
337-
338356
**Docker 环境使用 MongoDB**
339357
```bash
340358
# 单机 MongoDB 部署
@@ -562,7 +580,16 @@ export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryP
562580
- `LOG_LEVEL`: 日志级别(DEBUG/INFO/WARNING/ERROR,默认:INFO)
563581
- `LOG_FILE`: 日志文件路径(默认:gcli2api.log)
564582

565-
**MongoDB 配置**
583+
**存储配置(按优先级)**
584+
585+
**Redis 配置(最高优先级)**
586+
- `REDIS_URI`: Redis 连接字符串(设置后启用 Redis 模式)
587+
- 本地:`redis://localhost:6379`
588+
- 带密码:`redis://:password@host:6379`
589+
- SSL:`rediss://default:password@host:6380`
590+
- `REDIS_DATABASE`: Redis 数据库索引(0-15,默认:0)
591+
592+
**MongoDB 配置(第二优先级)**
566593
- `MONGODB_URI`: MongoDB 连接字符串(设置后启用 MongoDB 模式)
567594
- `MONGODB_DATABASE`: MongoDB 数据库名称(默认:gcli2api)
568595

docs/README_EN.md

Lines changed: 185 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
**Convert GeminiCLI to OpenAI and GEMINI API interfaces**
44

5-
---
5+
[中文](../README.md) | English
6+
7+
## 🚀 Quick Deploy
68

7-
[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/templates/FIBY1S?referralCode=su-kaka)
9+
[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/templates/2QLQC2?referralCode=su-kaka)
10+
---
811

912
## ⚠️ License Declaration
1013

@@ -287,6 +290,173 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
287290
- `x-goog-api-key: your_api_password`
288291
- URL parameter: `?key=your_api_password`
289292

293+
## 💾 Distributed Storage Mode
294+
295+
### 🌟 Storage Backend Priority
296+
297+
gcli2api supports multiple storage backends, automatically selecting by priority: **Redis > MongoDB > Local Files**
298+
299+
### ⚡ Redis Distributed Storage Mode
300+
301+
### ⚙️ Enable Redis Mode
302+
303+
**Step 1: Configure Redis Connection**
304+
```bash
305+
# Local Redis
306+
export REDIS_URI="redis://localhost:6379"
307+
308+
# Redis with password
309+
export REDIS_URI="redis://:password@localhost:6379"
310+
311+
# SSL connection (recommended for production)
312+
export REDIS_URI="rediss://default:password@host:6380"
313+
314+
# Upstash Redis (free cloud service)
315+
export REDIS_URI="rediss://default:token@your-host.upstash.io:6379"
316+
317+
# Optional: Custom database index (default: 0)
318+
export REDIS_DATABASE="1"
319+
```
320+
321+
**Step 2: Start Application**
322+
```bash
323+
# Application will automatically detect Redis configuration and prioritize Redis storage
324+
python web.py
325+
```
326+
327+
### 🍃 MongoDB Distributed Storage Mode
328+
329+
### 🌟 Alternative Storage Solution
330+
331+
If Redis is not configured, gcli2api will attempt to use **MongoDB storage mode**.
332+
333+
### ⚙️ Enable MongoDB Mode
334+
335+
**Step 1: Configure MongoDB Connection**
336+
```bash
337+
# Local MongoDB
338+
export MONGODB_URI="mongodb://localhost:27017"
339+
340+
# MongoDB Atlas cloud service
341+
export MONGODB_URI="mongodb+srv://username:password@cluster.mongodb.net"
342+
343+
# MongoDB with authentication
344+
export MONGODB_URI="mongodb://admin:password@localhost:27017/admin"
345+
346+
# Optional: Custom database name (default: gcli2api)
347+
export MONGODB_DATABASE="my_gcli_db"
348+
```
349+
350+
**Step 2: Start Application**
351+
```bash
352+
# Application will automatically detect MongoDB configuration and use MongoDB storage
353+
python web.py
354+
```
355+
356+
**Docker Environment using MongoDB**
357+
```bash
358+
# Single MongoDB deployment
359+
docker run -d --name gcli2api \
360+
-e MONGODB_URI="mongodb://mongodb:27017" \
361+
-e API_PASSWORD=your_password \
362+
--network your_network \
363+
ghcr.io/su-kaka/gcli2api:latest
364+
365+
# Using MongoDB Atlas
366+
docker run -d --name gcli2api \
367+
-e MONGODB_URI="mongodb+srv://user:pass@cluster.mongodb.net/gcli2api" \
368+
-e API_PASSWORD=your_password \
369+
-p 7861:7861 \
370+
ghcr.io/su-kaka/gcli2api:latest
371+
```
372+
373+
**Docker Compose Example**
374+
```yaml
375+
version: '3.8'
376+
377+
services:
378+
mongodb:
379+
image: mongo:7
380+
container_name: gcli2api-mongodb
381+
restart: unless-stopped
382+
environment:
383+
MONGO_INITDB_ROOT_USERNAME: admin
384+
MONGO_INITDB_ROOT_PASSWORD: password123
385+
volumes:
386+
- mongodb_data:/data/db
387+
ports:
388+
- "27017:27017"
389+
390+
gcli2api:
391+
image: ghcr.io/su-kaka/gcli2api:latest
392+
container_name: gcli2api
393+
restart: unless-stopped
394+
depends_on:
395+
- mongodb
396+
environment:
397+
- MONGODB_URI=mongodb://admin:password123@mongodb:27017/admin
398+
- MONGODB_DATABASE=gcli2api
399+
- API_PASSWORD=your_api_password
400+
- PORT=7861
401+
ports:
402+
- "7861:7861"
403+
404+
volumes:
405+
mongodb_data:
406+
```
407+
408+
### 🛠️ Troubleshooting
409+
410+
**Common Issue Solutions**
411+
412+
```bash
413+
# Check MongoDB connection
414+
python mongodb_setup.py check
415+
416+
# View detailed status information
417+
python mongodb_setup.py status
418+
419+
# Verify data migration results
420+
python -c "
421+
import asyncio
422+
from src.storage_adapter import get_storage_adapter
423+
424+
async def test():
425+
storage = await get_storage_adapter()
426+
info = await storage.get_backend_info()
427+
print(f'Current mode: {info[\"backend_type\"]}')
428+
if info['backend_type'] == 'mongodb':
429+
print(f'Database: {info.get(\"database_name\", \"Unknown\")}')
430+
431+
asyncio.run(test())
432+
"
433+
```
434+
435+
**Migration Failure Handling**
436+
```bash
437+
# If migration is interrupted, re-run
438+
python mongodb_setup.py migrate
439+
440+
# To rollback to file mode, remove MONGODB_URI environment variable
441+
unset MONGODB_URI
442+
# Then export data from MongoDB
443+
python mongodb_setup.py export
444+
```
445+
446+
### 🔧 Advanced Configuration
447+
448+
**MongoDB Connection Optimization**
449+
```bash
450+
# Connection pool and timeout configuration
451+
export MONGODB_URI="mongodb://localhost:27017?maxPoolSize=10&serverSelectionTimeoutMS=5000"
452+
453+
# Replica set configuration
454+
export MONGODB_URI="mongodb://host1:27017,host2:27017,host3:27017/gcli2api?replicaSet=myReplicaSet"
455+
456+
# Read-write separation configuration
457+
export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryPreferred"
458+
```
459+
290460
## 🏗️ Technical Architecture
291461

292462
### Core Module Description
@@ -378,6 +548,19 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
378548
- `LOG_LEVEL`: Log level (DEBUG/INFO/WARNING/ERROR, default: INFO)
379549
- `LOG_FILE`: Log file path (default: gcli2api.log)
380550

551+
**Storage Configuration (by priority)**
552+
553+
**Redis Configuration (Highest Priority)**
554+
- `REDIS_URI`: Redis connection string (enables Redis mode when set)
555+
- Local: `redis://localhost:6379`
556+
- With password: `redis://:password@host:6379`
557+
- SSL: `rediss://default:password@host:6380`
558+
- `REDIS_DATABASE`: Redis database index (0-15, default: 0)
559+
560+
**MongoDB Configuration (Second Priority)**
561+
- `MONGODB_URI`: MongoDB connection string (enables MongoDB mode when set)
562+
- `MONGODB_DATABASE`: MongoDB database name (default: gcli2api)
563+
381564
**Credential Configuration**
382565

383566
Support importing multiple credentials using `GCLI_CREDS_*` environment variables:

0 commit comments

Comments
 (0)