Skip to content

Commit af40cdc

Browse files
committed
增加postgres储存
1 parent 698c26c commit af40cdc

File tree

8 files changed

+359
-8
lines changed

8 files changed

+359
-8
lines changed

.env.example

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ REDIS_URI=redis://localhost:6379
5656
# 默认: 0
5757
REDIS_DATABASE=0
5858

59+
# ----------------------------
60+
# Postgres (关系型存储,优先级低于 Redis)
61+
# 配置 POSTGRES_DSN,用于在未启用 Redis 时作为分布式存储
62+
# 示例 DSN: postgresql://user:password@localhost:5432/gcli2api
63+
# ----------------------------
64+
# POSTGRES_DSN=postgresql://user:password@localhost:5432/gcli2api
65+
5966
# MongoDB 分布式存储模式配置 (第二优先级)
6067
# 设置 MONGODB_URI 后自动启用 MongoDB 模式,不再使用本地文件存储
6168

@@ -239,4 +246,4 @@ ANTI_TRUNCATION_MAX_ATTEMPTS=3
239246
#
240247
# [auto_ban]
241248
# auto_ban_enabled = false
242-
# auto_ban_error_codes = [400, 403]
249+
# auto_ban_error_codes = [400, 403]

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
296296

297297
### 🌟 存储后端优先级
298298

299-
gcli2api 支持多种存储后端,按优先级自动选择:**Redis > MongoDB > 本地文件**
299+
gcli2api 支持多种存储后端,按优先级自动选择:**Redis > Postgres > MongoDB > 本地文件**
300300

301301
### ⚡ Redis 分布式存储模式
302302

@@ -326,6 +326,26 @@ export REDIS_DATABASE="1"
326326
python web.py
327327
```
328328

329+
### 🐘 Postgres 分布式存储模式
330+
331+
如果未配置 Redis,或者你希望使用关系型数据库作为主要存储方案,gcli2api 也支持 Postgres(位于 Redis 之后,优先于 MongoDB)。
332+
333+
⚙️ 启用 Postgres 模式
334+
335+
步骤 1: 配置 Postgres 连接
336+
```bash
337+
# 使用标准 DSN(示例)
338+
export POSTGRES_DSN="postgresql://user:password@localhost:5432/gcli2api"
339+
340+
# 也可以使用 socket 或其他 DSN 格式,取决于你的部署方式
341+
```
342+
343+
步骤 2: 启动应用
344+
```bash
345+
# 应用会自动检测 POSTGRES_DSN 并在 Redis 未启用时优先使用 Postgres 存储
346+
python web.py
347+
```
348+
329349
### 🍃 MongoDB 分布式存储模式
330350

331351
### 🌟 备选存储方案

docs/README_EN.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
294294

295295
### 🌟 Storage Backend Priority
296296

297-
gcli2api supports multiple storage backends, automatically selecting by priority: **Redis > MongoDB > Local Files**
297+
gcli2api supports multiple storage backends, automatically selecting by priority: **Redis > Postgres > MongoDB > Local Files**
298298

299299
### ⚡ Redis Distributed Storage Mode
300300

@@ -324,6 +324,24 @@ export REDIS_DATABASE="1"
324324
python web.py
325325
```
326326

327+
### 🐘 Postgres Distributed Storage Mode
328+
329+
If Redis is not configured, or you prefer a relational database, gcli2api also supports Postgres (it is checked after Redis and before MongoDB).
330+
331+
⚙️ Enable Postgres Mode
332+
333+
Step 1: Configure Postgres DSN
334+
```bash
335+
# Example DSN:
336+
export POSTGRES_DSN="postgresql://user:password@localhost:5432/gcli2api"
337+
```
338+
339+
Step 2: Start Application
340+
```bash
341+
# Application will detect POSTGRES_DSN and use Postgres when Redis is not available
342+
python web.py
343+
```
344+
327345
### 🍃 MongoDB Distributed Storage Mode
328346

329347
### 🌟 Alternative Storage Solution
@@ -814,4 +832,4 @@ This project is for learning and research purposes only. Using this project indi
814832
- Bear all risks and responsibilities of using this project
815833
- Comply with relevant terms of service and legal regulations
816834

817-
The project authors are not responsible for any direct or indirect losses arising from the use of this project.
835+
The project authors are not responsible for any direct or indirect losses arising from the use of this project.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
88
"aiofiles>=24.1.0",
9+
"asyncpg>=0.30.0",
910
"fastapi>=0.116.1",
1011
"httpx[socks]>=0.28.1",
1112
"hypercorn>=0.17.3",

requirements-termux.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ toml
99
PyJWT
1010
oauthlib
1111
motor
12-
redis
12+
redis
13+
asyncpg

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ toml
99
PyJWT
1010
oauthlib
1111
motor
12-
redis
12+
redis
13+
asyncpg

0 commit comments

Comments
 (0)