# jay-luo-jj/sub2api architecture

> Architecture diagram of the GitHub repository jay-luo-jj/sub2api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jay-luo-jj/sub2api
- Repository: https://github.com/jay-luo-jj/sub2api

## Overview

Sub2API is a self-hosted AI API gateway for distributing quota from managed upstream subscriptions. The principal workflow is that a user obtains a platform API key, sends an AI request through the gateway, and receives a forwarded upstream response; the platform authenticates, schedules accounts, and tracks billable usage along the way. Separate capabilities include account and key administration, a web dashboard, and user top-ups through integrated payment providers. The README documents the product flow; sampled source offers limited wiring evidence, so undocumented internal calls are omitted.

Main entry points: [`backend/internal/handler/gateway_handler.go`](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/gateway_handler.go) (Gateway Requests), [`frontend/src/App.vue`](https://github.com/jay-luo-jj/sub2api/blob/main/frontend/src/App.vue) (Web Application), [`backend/internal/handler/usage_handler.go`](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/usage_handler.go) (Usage Tracking), [`backend/internal/payment`](https://github.com/jay-luo-jj/sub2api/tree/main/backend/internal/payment) (Payment System)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_access["User Access"]
  node_web["Web Application<br/>[App.vue]"]
  node_auth["Authentication<br/>[auth_handler.go]"]
  node_keys["API Keys<br/>[api_key_handler.go]"]
end

subgraph group_gateway["AI Gateway"]
  node_gateway_handler["Gateway Requests<br/>[gateway_handler.go]"]
  node_scheduling["Account Scheduling<br/>[channel_repo.go]"]
  node_compat["API Compatibility"]
end

subgraph group_operations["Platform Operations"]
  node_accounts["Upstream Accounts<br/>[account_handler.go]"]
  node_admin_dashboard["Admin Dashboard"]
  node_database[("Platform Data")]
  node_server["Backend Server<br/>[main.go]"]
end

subgraph group_commerce["Billing and Payments"]
  node_usage["Usage Tracking<br/>[usage_handler.go]"]
  node_billing["Billing"]
  node_payments["Payment System"]
end

node_user(("API User"))
node_admin(("Administrator"))
node_upstream(("AI Providers"))
node_payment_providers(("Payment Providers"))

node_user -->|"uses dashboard"| node_web
node_admin -->|"manages platform"| node_web
node_web -.->|"authenticates"| node_auth
node_user -->|"obtains API key"| node_keys
node_admin -->|"manages accounts"| node_accounts
node_user -->|"sends AI request"| node_gateway_handler
node_gateway_handler -.->|"authenticates key"| node_keys
node_gateway_handler -->|"selects account"| node_scheduling
node_gateway_handler -.->|"transforms request"| node_compat
node_gateway_handler -->|"forwards request"| node_upstream
node_gateway_handler -->|"tracks usage"| node_usage
node_usage -->|"calculates charges"| node_billing
node_user -->|"tops up account"| node_payments
node_payments -->|"processes payment"| node_payment_providers
node_admin -->|"monitors platform"| node_admin_dashboard
node_server -.->|"persists platform data"| node_database

click node_web "https://github.com/jay-luo-jj/sub2api/blob/main/frontend/src/App.vue"
click node_auth "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/auth_handler.go"
click node_keys "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/api_key_handler.go"
click node_accounts "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/admin/account_handler.go"
click node_gateway_handler "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/gateway_handler.go"
click node_scheduling "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/repository/channel_repo.go"
click node_compat "https://github.com/jay-luo-jj/sub2api/tree/main/backend/internal/pkg/apicompat"
click node_usage "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/usage_handler.go"
click node_billing "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/repository/usage_billing_repo.go"
click node_payments "https://github.com/jay-luo-jj/sub2api/tree/main/backend/internal/payment"
click node_admin_dashboard "https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/admin/dashboard_handler.go"
click node_database "https://github.com/jay-luo-jj/sub2api/tree/main/backend/ent"
click node_server "https://github.com/jay-luo-jj/sub2api/blob/main/backend/cmd/server/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web,node_auth,node_keys,node_user toneBlue
class node_gateway_handler,node_scheduling,node_compat toneAmber
class node_accounts,node_admin_dashboard,node_database,node_server toneMint
class node_usage,node_billing,node_payments toneRose
class node_admin,node_upstream,node_payment_providers toneIndigo
```

## Components

### User Access

- [Web Application](https://github.com/jay-luo-jj/sub2api/blob/main/frontend/src/App.vue): `frontend/src/App.vue`
- [Authentication](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/auth_handler.go): `backend/internal/handler/auth_handler.go`
- [API Keys](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/api_key_handler.go): `backend/internal/handler/api_key_handler.go`

### AI Gateway

- [Gateway Requests](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/gateway_handler.go): `backend/internal/handler/gateway_handler.go`
- [Account Scheduling](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/repository/channel_repo.go): `backend/internal/repository/channel_repo.go`
- [API Compatibility](https://github.com/jay-luo-jj/sub2api/tree/main/backend/internal/pkg/apicompat): `backend/internal/pkg/apicompat`

### Platform Operations

- [Upstream Accounts](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/admin/account_handler.go): `backend/internal/handler/admin/account_handler.go`
- [Admin Dashboard](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/admin/dashboard_handler.go): `backend/internal/handler/admin/dashboard_handler.go`
- [Platform Data](https://github.com/jay-luo-jj/sub2api/tree/main/backend/ent): `backend/ent`
- [Backend Server](https://github.com/jay-luo-jj/sub2api/blob/main/backend/cmd/server/main.go): `backend/cmd/server/main.go`

### Billing and Payments

- [Usage Tracking](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/handler/usage_handler.go): `backend/internal/handler/usage_handler.go`
- [Billing](https://github.com/jay-luo-jj/sub2api/blob/main/backend/internal/repository/usage_billing_repo.go): `backend/internal/repository/usage_billing_repo.go`
- [Payment System](https://github.com/jay-luo-jj/sub2api/tree/main/backend/internal/payment): `backend/internal/payment`

### Other components

- **API User**
- **Administrator**
- **AI Providers**
- **Payment Providers**

## Connections

- API User → Web Application: uses dashboard (evidence: `README.md`)
- Administrator → Web Application: manages platform (evidence: `README.md`)
- Web Application → Authentication: authenticates
- API User → API Keys: obtains API key (evidence: `README.md`)
- Administrator → Upstream Accounts: manages accounts (evidence: `README.md`)
- API User → Gateway Requests: sends AI request (evidence: `README.md`)
- Gateway Requests → API Keys: authenticates key
- Gateway Requests → Account Scheduling: selects account (evidence: `README.md`)
- Gateway Requests → API Compatibility: transforms request
- Gateway Requests → AI Providers: forwards request (evidence: `README.md`)
- Gateway Requests → Usage Tracking: tracks usage (evidence: `README.md`)
- Usage Tracking → Billing: calculates charges (evidence: `README.md`)
- API User → Payment System: tops up account (evidence: `README.md`)
- Payment System → Payment Providers: processes payment (evidence: `README.md`)
- Administrator → Admin Dashboard: monitors platform (evidence: `README.md`)
- Backend Server → Platform Data: persists platform data
