Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 112 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ ghcr.io/su-kaka/gcli2api:latest

### 🌟 存储后端支持

gcli2api 支持两种存储后端:**本地 SQLite(默认)** 和 **MongoDB(云端分布式存储)**
gcli2api 支持三种存储后端:**本地 SQLite(默认)**、**MongoDB(云端分布式存储)** 和 **MySQL(关系型数据库)**

### 📁 本地 SQLite 存储(默认)

Expand Down Expand Up @@ -473,7 +473,103 @@ export MONGODB_URI="mongodb://host1:27017,host2:27017,host3:27017/gcli2api?repli
export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryPreferred"
```

## 🏗️ 技术架构
### 🐬 MySQL 关系型存储模式

**企业级关系数据库方案**

当需要企业级数据库支持、事务处理或与现有MySQL基础设施集成时,可以启用MySQL存储模式。

### ⚙️ 启用 MySQL 模式

**步骤 1: 配置 MySQL 连接**
```bash
# 使用标准 DSN 格式
export MYSQL_DSN="mysql://username:password@hostname:port/database"

# 示例:本地 MySQL
export MYSQL_DSN="mysql://root:mypassword@localhost:3306/gcli2api"

# 示例:云数据库
export MYSQL_DSN="mysql://user:[email protected]:3306/gcli2api"

# 简化示例(使用默认值)
export MYSQL_DSN="mysql://root@localhost/gcli2api"
```

**DSN 格式说明**
- `mysql://`: 协议标识
- `username:password`: 数据库用户名和密码
- `hostname:port`: 数据库主机和端口
- `database`: 数据库名称

**步骤 2: 创建数据库**
```sql
CREATE DATABASE gcli2api CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

**步骤 3: 启动应用**
```bash
# 应用会自动检测 MySQL DSN 配置并使用 MySQL 存储
python web.py
```

**Docker 环境使用 MySQL**
```bash
# 单机 MySQL 部署
docker run -d --name gcli2api \
-e MYSQL_DSN="mysql://root:mypassword@mysql:3306/gcli2api" \
-e API_PASSWORD=your_password \
--network your_network \
ghcr.io/su-kaka/gcli2api:latest

# 使用云 MySQL 服务
docker run -d --name gcli2api \
-e MYSQL_DSN="mysql://user:[email protected]:3306/gcli2api" \
-e API_PASSWORD=your_password \
-p 7861:7861 \
ghcr.io/su-kaka/gcli2api:latest
```

**Docker Compose 示例(MySQL + gcli2api)**
```yaml
version: '3.8'

services:
mysql:
image: mysql:8
container_name: gcli2api-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: password123
MYSQL_DATABASE: gcli2api
volumes:
- mysql_data:/var/lib/mysql
ports:
- "3306:3306"

gcli2api:
image: ghcr.io/su-kaka/gcli2api:latest
container_name: gcli2api
restart: unless-stopped
depends_on:
- mysql
environment:
- MYSQL_DSN=mysql://root:password123@mysql:3306/gcli2api
- API_PASSWORD=your_api_password
- PORT=7861
ports:
- "7861:7861"

volumes:
mysql_data:
```




---

## ️ 技术架构

### 核心模块说明

Expand Down Expand Up @@ -565,27 +661,29 @@ export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryP
- `LOG_FILE`: 日志文件路径(默认:gcli2api.log)

**存储配置**

**SQLite 配置(默认)**
- 无需配置,自动使用本地 SQLite 数据库
- 数据库文件自动创建在项目目录

**MongoDB 配置(可选云端存储)**
- `MONGODB_URI`: MongoDB 连接字符串(设置后启用 MongoDB 模式)
- `MONGODB_DATABASE`: MongoDB 数据库名称(默认:gcli2api)
- `MYSQL_DSN`: MySQL 连接字符串(启用 MySQL 存储模式)
- `MONGODB_URI`: MongoDB 连接字符串(启用 MongoDB 存储模式)
- 默认使用 SQLite 存储(无需配置)

**Docker 使用示例**
```bash
# 使用通用密码
# 使用通用密码和默认存储
docker run -d --name gcli2api \
-e PASSWORD=mypassword \
-e PORT=7861 \
ghcr.io/su-kaka/gcli2api:latest

# 使用分离密码
# 使用 MySQL 存储
docker run -d --name gcli2api \
-e MYSQL_DSN="mysql://root:mypassword@mysql:3306/gcli2api" \
-e PASSWORD=mypassword \
-e PORT=7861 \
ghcr.io/su-kaka/gcli2api:latest

# 使用 MongoDB 存储
docker run -d --name gcli2api \
-e API_PASSWORD=my_api_password \
-e PANEL_PASSWORD=my_panel_password \
-e MONGODB_URI="mongodb://admin:password@mongodb:27017/gcli2api" \
-e PASSWORD=mypassword \
-e PORT=7861 \
ghcr.io/su-kaka/gcli2api:latest
```
Expand Down
12 changes: 9 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
version: "3.8"

services:
gcli2api:
Expand Down Expand Up @@ -32,6 +32,9 @@ services:
# - MONGODB_URI=${MONGODB_URI}
# - MONGODB_DATABASE=${MONGODB_DATABASE:-gcli2api}

# Optional: MySQL configuration (for distributed storage)
# - MYSQL_URI=${MONGODB_URI}

# Optional: PostgreSQL configuration (for distributed storage)
# - POSTGRES_DSN=${POSTGRES_DSN}

Expand All @@ -40,12 +43,15 @@ services:
volumes:
- ./data/creds:/app/creds
healthcheck:
test: ["CMD-SHELL", "python -c \"import sys, urllib.request, os; port = os.environ.get('PORT', '7861'); req = urllib.request.Request(f'http://localhost:{port}/v1/models', headers={'Authorization': 'Bearer ' + os.environ.get('PASSWORD', 'pwd')}); sys.exit(0 if urllib.request.urlopen(req, timeout=5).getcode() == 200 else 1)\""]
test:
[
"CMD-SHELL",
'python -c "import sys, urllib.request, os; port = os.environ.get(''PORT'', ''7861''); req = urllib.request.Request(f''http://localhost:{port}/v1/models'', headers={''Authorization'': ''Bearer '' + os.environ.get(''PASSWORD'', ''pwd'')}); sys.exit(0 if urllib.request.urlopen(req, timeout=5).getcode() == 200 else 1)"',
]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

# Example with Redis for distributed storage
# redis:
# image: redis:7-alpine
Expand Down
117 changes: 70 additions & 47 deletions docs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
- Lower section: Antigravity credential batch upload (green theme)
- Separate credential management tabs for each type

## 💾 Data Storage Mode
## 💾 Data Storage Modes

### 🌟 Storage Backend Support

gcli2api supports two storage backends: **Local SQLite (Default)** and **MongoDB (Cloud Distributed Storage)**
gcli2api supports three storage backends: **Local SQLite (Default)**, **MongoDB (Cloud Distributed Storage)**, and **MySQL (Relational Database)**

### 📁 Local SQLite Storage (Default)

Expand All @@ -346,79 +346,95 @@ gcli2api supports two storage backends: **Local SQLite (Default)** and **MongoDB

When multi-instance deployment or cloud storage is needed, MongoDB storage mode can be enabled.

### ⚙️ Enable MongoDB Mode
### 🐬 MySQL Relational Storage Mode

**Step 1: Configure MongoDB Connection**
**Enterprise-grade Relational Database Solution**

When enterprise-level database support, transaction processing, or integration with existing MySQL infrastructure is needed, MySQL storage mode can be enabled.

### ⚙️ Enable MySQL Mode

**Step 1: Configure MySQL Connection**
```bash
# Local MongoDB
export MONGODB_URI="mongodb://localhost:27017"
# Use standard DSN format
export MYSQL_DSN="mysql://username:password@hostname:port/database"

# Example: Local MySQL
export MYSQL_DSN="mysql://root:mypassword@localhost:3306/gcli2api"

# Example: Cloud database
export MYSQL_DSN="mysql://user:[email protected]:3306/gcli2api"

# MongoDB Atlas cloud service
export MONGODB_URI="mongodb+srv://username:[email protected]"
# Simplified example (using defaults)
export MYSQL_DSN="mysql://root@localhost/gcli2api"
```

# MongoDB with authentication
export MONGODB_URI="mongodb://admin:password@localhost:27017/admin"
**DSN Format Explanation**
- `mysql://`: Protocol identifier
- `username:password`: Database username and password
- `hostname:port`: Database host and port
- `database`: Database name

# Optional: Custom database name (default: gcli2api)
export MONGODB_DATABASE="my_gcli_db"
**Step 2: Create Database**
```sql
CREATE DATABASE gcli2api CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

**Step 2: Start Application**
**Step 3: Start Application**
```bash
# Application will automatically detect MongoDB configuration and use MongoDB storage
# Application will automatically detect MySQL DSN configuration and use MySQL storage
python web.py
```

**Docker Environment using MongoDB**
**Docker Environment using MySQL**
```bash
# Single MongoDB deployment
# Single MySQL deployment
docker run -d --name gcli2api \
-e MONGODB_URI="mongodb://mongodb:27017" \
-e MYSQL_DSN="mysql://root:mypassword@mysql:3306/gcli2api" \
-e API_PASSWORD=your_password \
--network your_network \
ghcr.io/su-kaka/gcli2api:latest

# Using MongoDB Atlas
# Using cloud MySQL service
docker run -d --name gcli2api \
-e MONGODB_URI="mongodb+srv://user:pass@cluster.mongodb.net/gcli2api" \
-e MYSQL_DSN="mysql://user:pass@mysql.cloudprovider.com:3306/gcli2api" \
-e API_PASSWORD=your_password \
-p 7861:7861 \
ghcr.io/su-kaka/gcli2api:latest
```

**Docker Compose Example**
**Docker Compose Example (MySQL + gcli2api)**
```yaml
version: '3.8'

services:
mongodb:
image: mongo:7
container_name: gcli2api-mongodb
mysql:
image: mysql:8
container_name: gcli2api-mysql
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password123
MYSQL_ROOT_PASSWORD: password123
MYSQL_DATABASE: gcli2api
volumes:
- mongodb_data:/data/db
- mysql_data:/var/lib/mysql
ports:
- "27017:27017"
- "3306:3306"

gcli2api:
image: ghcr.io/su-kaka/gcli2api:latest
container_name: gcli2api
restart: unless-stopped
depends_on:
- mongodb
- mysql
environment:
- MONGODB_URI=mongodb://admin:password123@mongodb:27017/admin
- MONGODB_DATABASE=gcli2api
- MYSQL_DSN=mysql://root:password123@mysql:3306/gcli2api
- API_PASSWORD=your_api_password
- PORT=7861
ports:
- "7861:7861"

volumes:
mongodb_data:
mysql_data:
```

### 🛠️ Troubleshooting
Expand Down Expand Up @@ -473,6 +489,10 @@ export MONGODB_URI="mongodb://host1:27017,host2:27017,host3:27017/gcli2api?repli
export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryPreferred"
```



---

## 🏗️ Technical Architecture

### Core Module Description
Expand Down Expand Up @@ -565,30 +585,33 @@ export MONGODB_URI="mongodb://localhost:27017/gcli2api?readPreference=secondaryP
- `LOG_FILE`: Log file path (default: gcli2api.log)

**Storage Configuration**
- `MYSQL_DSN`: MySQL connection string (enables MySQL storage mode)
- `MONGODB_URI`: MongoDB connection string (enables MongoDB storage mode)
- Default uses SQLite storage (no configuration required)

**SQLite Configuration (Default)**
- No configuration required, automatically uses local SQLite database
- Database files are automatically created in the project directory

**MongoDB Configuration (Optional Cloud Storage)**
- `MONGODB_URI`: MongoDB connection string (enables MongoDB mode when set)
- `MONGODB_DATABASE`: MongoDB database name (default: gcli2api)

**Docker Usage Example**
**Docker Usage Examples**
```bash
# Using universal password
# Using universal password and default SQLite
docker run -d --name gcli2api \
-e PASSWORD=mypassword \
-e PORT=11451 \
-e GOOGLE_CREDENTIALS="$(cat credential.json | base64 -w 0)" \
-e PORT=7861 \
ghcr.io/su-kaka/gcli2api:latest

# Using separate passwords
# Using separate passwords and MySQL
docker run -d --name gcli2api \
-e API_PASSWORD=my_api_password \
-e MYSQL_DSN="mysql://root:mypassword@mysql:3306/gcli2api" \
-e PASSWORD=mypassword \
-e PANEL_PASSWORD=my_panel_password \
-e PORT=11451 \
-e GOOGLE_CREDENTIALS="$(cat credential.json | base64 -w 0)" \
-e PORT=7861 \
--network my_network \
ghcr.io/su-kaka/gcli2api:latest

# Using separate passwords and MongoDB
docker run -d --name gcli2api \
-e MONGODB_URI="mongodb://admin:password@mongodb:27017/gcli2api" \
-e PASSWORD=mypassword \
-e PORT=7861 \
--network my_network \
ghcr.io/su-kaka/gcli2api:latest
```

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ oauthlib>=3.3.1
motor>=3.7.1
aiosqlite>=0.20.0
pypinyin>=0.51.0
aiomysql>=0.2.0
Loading