Sign inSign up

host1syan/sub2api

By host1syan

Updated 3 months ago

Image
0

169

host1syan/sub2api repository overview

Sub2API - AI API 网关平台

全合一 Docker 镜像 — Sub2API + PostgreSQL + Redis 运行在单个容器中。

GitHub: github.com/Wei-Shaw/sub2api

Docker Hub: host1syan/sub2api


概述

Sub2API 是一个 AI API 网关平台,用于分管理和分发 AI 产品订阅的 API 配额。用户通过平台生成的 API Key 访问上游 AI 服务,平台负责身份验证、计费、负载均衡和请求转发。

功能特性

  • 多账号管理 — 支持多种上游账号类型(OAuth、API Key)
  • API Key 分发 — 为用户生成和管理 API Key
  • 精确计费 — Token 级别的用量追踪和成本计算
  • 智能调度 — 智能账号选择与会话粘性
  • 并发控制 — 按用户和按账号设置并发限制
  • 速率限制 — 可配置的请求和 Token 速率限制
  • 内置支付系统 — 支持 EasyPay、支付宝、微信支付、Stripe
  • 管理后台 — Web 界面监控和管理

快速开始

docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

打开浏览器访问 http://localhost:8080。管理员密码会自动生成,查看方式:

docker logs sub2api | grep -i "admin password"

Docker Compose 部署(推荐)

mkdir sub2api && cd sub2api

cat > docker-compose.yml << 'EOF'
version: '3.8'

services:
  sub2api:
    image: host1syan/sub2api:latest
    container_name: sub2api
    restart: unless-stopped
    ports:
      - "8080:8080"
    volumes:
      - sub2api_data:/app/data
    healthcheck:
      test: ["CMD", "wget", "-q", "-T", "5", "-O", "/dev/null", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  sub2api_data:
EOF

docker compose up -d

环境变量

变量名默认值说明
SERVER_PORT8080Web 界面端口
ADMIN_EMAIL[email protected]管理员登录邮箱
ADMIN_PASSWORD(自动生成)管理员密码。设置一次即可固定,不设置则自动生成
DATABASE_PASSWORD(自动生成)PostgreSQL 密码。保存到 /app/data/all-in-one-secrets.env
DATABASE_DBNAMEsub2apiPostgreSQL 数据库名
DATABASE_USERsub2apiPostgreSQL 用户名
DATABASE_PORT5432PostgreSQL 端口
REDIS_PORT6379Redis 端口
REDIS_PASSWORD(空)Redis 密码(可选)
JWT_SECRET(自动生成)JWT 签名密钥。建议设置固定值
TOTP_ENCRYPTION_KEY(自动生成)TOTP 加密密钥。建议设置固定值
TZAsia/Shanghai容器时区
RUN_MODEstandardstandard = 完整 SaaS 功能;simple = 内部使用
SERVER_MODEreleasereleasedebug
AUTO_SETUPtrue首次运行时自动初始化数据库和管理员

数据持久化

所有数据存储在 /app/data 下。务必挂载此卷。

docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

首次启动时,系统会在以下路径自动生成密钥文件:

/app/data/all-in-one-secrets.env

该文件包含自动生成的 DATABASE_PASSWORDJWT_SECRETTOTP_ENCRYPTION_KEY。迁移时请备份此文件。

工作原理

"全合一"镜像将三个服务打包在一个容器中:

服务说明
Sub2APIGo 后端 + 内嵌 Vue 前端
PostgreSQL 18数据库。首次启动时自动初始化
Redis缓存/消息队列。首次启动时自动启动

首次启动时,入口脚本会执行:

  1. 初始化 PostgreSQL(如果数据目录不存在)
  2. 创建数据库用户和数据库
  3. 启动 Redis
  4. AUTO_SETUP=true 启动 Sub2API
  5. 自动运行数据库迁移
  6. 创建管理员账号(密码显示在日志中)

后续启动时跳过初始化,直接使用现有数据。

更新方法

docker pull host1syan/sub2api:latest

# 停止并删除旧容器(数据卷保留)
docker rm -f sub2api

# 使用新镜像重新启动
docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

备份与恢复

所有数据在 /app/data 目录下:

# 备份
docker run --rm -v sub2api_data:/data -v $(pwd):/backup alpine \
  tar czf /backup/sub2api-backup.tar.gz -C /data .

# 恢复
docker run --rm -v sub2api_data:/data -v $(pwd):/backup alpine \
  tar xzf /backup/sub2api-backup.tar.gz -C /data

常见问题排查

容器启动失败 — PostgreSQL 权限错误
# 删除容器和数据,然后重新启动
docker rm -f sub2api
docker volume rm sub2api_data
docker run -d --name sub2api -p 8080:8080 -v sub2api_data:/app/data host1syan/sub2api:latest
忘记管理员密码
docker logs sub2api | grep -i "admin password"
检查容器内 PostgreSQL 状态
docker exec -it sub2api sh -c "su-exec sub2api pg_isready -p 5432"
检查容器内 Redis 状态
docker exec -it sub2api sh -c "redis-cli -p 6379 ping"

支持的平台

平台账号类型
OpenAIAPI Key、OAuth(ChatGPT)、Codex Session
AnthropicAPI Key、OAuth(Claude)、Setup Token
Google GeminiAPI Key、OAuth(Code Assist)

技术栈

组件技术
后端Go 1.25+、Gin、Ent ORM
前端Vue 3.4+、Vite 5+、TailwindCSS
数据库PostgreSQL 18(Alpine)
缓存Redis(Alpine)
基础镜像Alpine 3.21

链接


使用本项目的风险由用户自行承担。使用前请审阅上游 AI 服务提供商的用户协议。作者不对因使用本项目而导致的账号封禁、服务中断、数据丢失或其他任何直接或间接损害承担责任。




Sub2API - AI API Gateway Platform

All-in-One Docker Image — Sub2API + PostgreSQL + Redis in a single container.

GitHub: github.com/Wei-Shaw/sub2api

Docker Hub: host1syan/sub2api


Overview

Sub2API is an AI API gateway platform designed to distribute and manage API quotas from AI product subscriptions. Users access upstream AI services through platform-generated API Keys, while the platform handles authentication, billing, load balancing, and request forwarding.

Features

  • Multi-Account Management — Support multiple upstream account types (OAuth, API Key)
  • API Key Distribution — Generate and manage API Keys for users
  • Precise Billing — Token-level usage tracking and cost calculation
  • Smart Scheduling — Intelligent account selection with sticky sessions
  • Concurrency Control — Per-user and per-account concurrency limits
  • Rate Limiting — Configurable request and token rate limits
  • Built-in Payment System — EasyPay, Alipay, WeChat Pay, Stripe
  • Admin Dashboard — Web interface for monitoring and management

Quick Start

docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

Open http://localhost:8080 in your browser. The admin password will be auto-generated. Check the container logs:

docker logs sub2api | grep -i "admin password"
mkdir sub2api && cd sub2api

cat > docker-compose.yml << 'EOF'
version: '3.8'

services:
  sub2api:
    image: host1syan/sub2api:latest
    container_name: sub2api
    restart: unless-stopped
    ports:
      - "8080:8080"
    volumes:
      - sub2api_data:/app/data
    healthcheck:
      test: ["CMD", "wget", "-q", "-T", "5", "-O", "/dev/null", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  sub2api_data:
EOF

docker compose up -d

Environment Variables

VariableDefaultDescription
SERVER_PORT8080Web UI port
ADMIN_EMAIL[email protected]Admin login email
ADMIN_PASSWORD(auto-generated)Admin password. Set once to fix, omitted to auto-generate
DATABASE_PASSWORD(auto-generated)PostgreSQL password. Saved to /app/data/all-in-one-secrets.env
DATABASE_DBNAMEsub2apiPostgreSQL database name
DATABASE_USERsub2apiPostgreSQL username
DATABASE_PORT5432PostgreSQL port
REDIS_PORT6379Redis port
REDIS_PASSWORD(empty)Redis password (optional)
JWT_SECRET(auto-generated)JWT signing secret. Fixed value recommended
TOTP_ENCRYPTION_KEY(auto-generated)TOTP encryption key. Fixed value recommended
TZAsia/ShanghaiContainer timezone
RUN_MODEstandardstandard = full SaaS; simple = internal use
SERVER_MODEreleaserelease or debug
AUTO_SETUPtrueAuto-initialize database and admin on first run

Data Persistence

All data is stored under /app/data. Always mount this volume.

docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

When you first start the container, a secrets file is auto-generated at:

/app/data/all-in-one-secrets.env

This file contains the auto-generated DATABASE_PASSWORD, JWT_SECRET, and TOTP_ENCRYPTION_KEY. Back up this file when migrating.

How It Works

This "all-in-one" image bundles three services into one container:

ServiceDescription
Sub2APIGo backend + embedded Vue frontend
PostgreSQL 18Database. Initialized automatically on first boot
RedisCache/queue. Started automatically on first boot

On first start, the entrypoint script will:

  1. Initialize PostgreSQL (if no data exists)
  2. Create the database user and database
  3. Start Redis
  4. Launch Sub2API with AUTO_SETUP=true
  5. Run database migrations automatically
  6. Create the admin account (password shown in logs)

On subsequent starts, it skips initialization and uses existing data.

Updating

docker pull host1syan/sub2api:latest

# Stop and remove old container (data volume is kept)
docker rm -f sub2api

# Start with the new image
docker run -d \
  --name sub2api \
  -p 8080:8080 \
  -v sub2api_data:/app/data \
  host1syan/sub2api:latest

Backup & Restore

All data lives in /app/data:

# Backup
docker run --rm -v sub2api_data:/data -v $(pwd):/backup alpine \
  tar czf /backup/sub2api-backup.tar.gz -C /data .

# Restore
docker run --rm -v sub2api_data:/data -v $(pwd):/backup alpine \
  tar xzf /backup/sub2api-backup.tar.gz -C /data

Troubleshooting

Container won't start — PostgreSQL permission error
# Delete the container and its data, then restart fresh
docker rm -f sub2api
docker volume rm sub2api_data
docker run -d --name sub2api -p 8080:8080 -v sub2api_data:/app/data host1syan/sub2api:latest
Forgot admin password
docker logs sub2api | grep -i "admin password"
Check PostgreSQL status inside container
docker exec -it sub2api sh -c "su-exec sub2api pg_isready -p 5432"
Check Redis status inside container
docker exec -it sub2api sh -c "redis-cli -p 6379 ping"

Supported Platforms

PlatformAccount Type
OpenAIAPI Key, OAuth (ChatGPT), Codex Session
AnthropicAPI Key, OAuth (Claude), Setup Token
Google GeminiAPI Key, OAuth (Code Assist)

Tech Stack

ComponentTechnology
BackendGo 1.25+, Gin, Ent ORM
FrontendVue 3.4+, Vite 5+, TailwindCSS
DatabasePostgreSQL 18 (Alpine)
CacheRedis (Alpine)
Base ImageAlpine 3.21

Use this project at your own risk. Please review the terms of service of upstream AI providers before use. All risks are borne solely by the user.

Tag summary

Content type

Image

Digest

sha256:109da78d2

Size

138.3 MB

Last updated

3 months ago

docker pull host1syan/sub2api