# sunjinopensource/sub2api architecture

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

- Interactive diagram: https://gitdiagram.com/sunjinopensource/sub2api
- Repository: https://github.com/sunjinopensource/sub2api

## Overview

Sub2API is an AI API gateway that distributes subscription quota across upstream model providers. Its principal workflow is a client request entering the gateway, being routed to an upstream provider, and returning a response; account, quota, and usage records support that service. The tree also shows separate authentication, administration, payments, monitoring, and image-storage capabilities. Frontend bootstrap and API-client relationships are supported by excerpts; most backend wiring is not sampled, so uncertain runtime connections are shown as dashed and unverified.

Main entry points: [`backend/internal/handler/gateway_handler.go`](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/gateway_handler.go) (AI API gateway), [`frontend/src/api/client.ts`](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/client.ts) (HTTP client), [`backend/internal/handler/openai_gateway_handler.go`](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/openai_gateway_handler.go) (OpenAI-compatible API), [`backend/ent/account_query.go`](https://github.com/sunjinopensource/sub2api/blob/main/backend/ent/account_query.go) (Account records)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web application"]
  node_webapp["Vue application<br/>[App.vue]"]
  node_webbootstrap["Frontend bootstrap<br/>[main.ts]"]
  node_router["Route guards<br/>[index.ts]"]
  node_httpclient["HTTP client<br/>[client.ts]"]
  node_adminmark["UI request markers<br/>[adminUIRequest.ts]"]
  node_tokenrefresh["Token refresh<br/>[tokenRefresh.ts]"]
  node_urlconfig["API URL config<br/>[url.ts]"]
end

subgraph group_gateway["AI gateway"]
  node_gatewayhandler["AI API gateway<br/>[gateway_handler.go]"]
  node_openaihandler["OpenAI-compatible API"]
  node_ccpipeline["Chat pipeline"]
  node_protocolcompat["Protocol conversion"]
end

subgraph group_identity["Identity and accounts"]
  node_authhandler["Authentication<br/>[auth_handler.go]"]
  node_oauthflows["Provider OAuth"]
  node_accounts[("Account records<br/>[account_query.go]")]
  node_subscriptions[("Subscription records")]
end

subgraph group_operations["Platform operations"]
  node_admin["Admin APIs"]
  node_payments["Payments"]
  node_monitoring["Channel monitoring"]
  node_image["Image storage<br/>[image_storage.go]"]
end

node_client(("API client"))
node_upstream{{"Model providers"}}
node_s3[("S3-compatible storage")]
node_database[("Application database")]

node_webbootstrap -->|"mounts"| node_webapp
node_webbootstrap -->|"installs"| node_router
node_httpclient -->|"checks requests"| node_adminmark
node_httpclient -->|"refreshes tokens"| node_tokenrefresh
node_httpclient -->|"gets base URL"| node_urlconfig
node_client -.->|"sends AI requests"| node_gatewayhandler
node_gatewayhandler -.->|"dispatches protocol"| node_openaihandler
node_openaihandler -.->|"uses chat pipeline"| node_ccpipeline
node_ccpipeline -.->|"sends requests"| node_upstream
node_openaihandler -.->|"converts protocols"| node_protocolcompat
node_authhandler -.->|"uses account data"| node_accounts
node_oauthflows -.->|"supports sign-in"| node_authhandler
node_admin -.->|"manages accounts"| node_accounts
node_admin -.->|"manages subscriptions"| node_subscriptions
node_payments -.->|"records payments"| node_database
node_monitoring -.->|"stores monitor data"| node_database
node_image -->|"stores images"| node_s3
node_webapp -.->|"uses API"| node_httpclient
node_gatewayhandler -.->|"selects accounts"| node_accounts
node_gatewayhandler -.->|"applies quota"| node_subscriptions

click node_webapp "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/App.vue"
click node_webbootstrap "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/main.ts"
click node_router "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/router/index.ts"
click node_httpclient "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/client.ts"
click node_adminmark "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/adminUIRequest.ts"
click node_tokenrefresh "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/tokenRefresh.ts"
click node_urlconfig "https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/url.ts"
click node_gatewayhandler "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/gateway_handler.go"
click node_openaihandler "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/openai_gateway_handler.go"
click node_ccpipeline "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/service/openai_gateway_cc_pipeline.go"
click node_protocolcompat "https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/pkg/apicompat"
click node_authhandler "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/auth_handler.go"
click node_oauthflows "https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/handler"
click node_accounts "https://github.com/sunjinopensource/sub2api/blob/main/backend/ent/account_query.go"
click node_subscriptions "https://github.com/sunjinopensource/sub2api/blob/main/backend/ent/usersubscription_query.go"
click node_admin "https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/handler/admin"
click node_payments "https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/payment"
click node_monitoring "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/channel_monitor_v2_handler.go"
click node_image "https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/service/image_storage.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_webapp,node_webbootstrap,node_router,node_httpclient,node_adminmark,node_tokenrefresh,node_urlconfig,node_client toneBlue
class node_gatewayhandler,node_openaihandler,node_ccpipeline,node_protocolcompat,node_s3,node_database toneAmber
class node_authhandler,node_oauthflows,node_accounts,node_subscriptions toneMint
class node_admin,node_payments,node_monitoring,node_image toneRose
class node_upstream toneIndigo
```

## Components

### Web application

- [Vue application](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/App.vue): `frontend/src/App.vue`
- [Frontend bootstrap](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/main.ts): `frontend/src/main.ts`
- [Route guards](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/router/index.ts): `frontend/src/router/index.ts`
- [HTTP client](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/client.ts): `frontend/src/api/client.ts`
- [UI request markers](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/adminUIRequest.ts): `frontend/src/api/adminUIRequest.ts`
- [Token refresh](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/tokenRefresh.ts): `frontend/src/api/tokenRefresh.ts`
- [API URL config](https://github.com/sunjinopensource/sub2api/blob/main/frontend/src/api/url.ts): `frontend/src/api/url.ts`

### AI gateway

- [AI API gateway](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/gateway_handler.go): `backend/internal/handler/gateway_handler.go`
- [OpenAI-compatible API](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/openai_gateway_handler.go): `backend/internal/handler/openai_gateway_handler.go`
- [Chat pipeline](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/service/openai_gateway_cc_pipeline.go): `backend/internal/service/openai_gateway_cc_pipeline.go`
- [Protocol conversion](https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/pkg/apicompat): `backend/internal/pkg/apicompat`

### Identity and accounts

- [Authentication](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/auth_handler.go): `backend/internal/handler/auth_handler.go`
- [Provider OAuth](https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/handler): `backend/internal/handler`
- [Account records](https://github.com/sunjinopensource/sub2api/blob/main/backend/ent/account_query.go): `backend/ent/account_query.go`
- [Subscription records](https://github.com/sunjinopensource/sub2api/blob/main/backend/ent/usersubscription_query.go): `backend/ent/usersubscription_query.go`

### Platform operations

- [Admin APIs](https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/handler/admin): `backend/internal/handler/admin`
- [Payments](https://github.com/sunjinopensource/sub2api/tree/main/backend/internal/payment): `backend/internal/payment`
- [Channel monitoring](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/handler/channel_monitor_v2_handler.go): `backend/internal/handler/channel_monitor_v2_handler.go`
- [Image storage](https://github.com/sunjinopensource/sub2api/blob/main/backend/internal/service/image_storage.go): `backend/internal/service/image_storage.go`

### Other components

- **API client**
- **Model providers**
- **S3-compatible storage**
- **Application database**

## Connections

- Frontend bootstrap → Vue application: mounts (evidence: `frontend/src/main.ts`)
- Frontend bootstrap → Route guards: installs (evidence: `frontend/src/main.ts`)
- HTTP client → UI request markers: checks requests (evidence: `frontend/src/api/client.ts`)
- HTTP client → Token refresh: refreshes tokens (evidence: `frontend/src/api/client.ts`)
- HTTP client → API URL config: gets base URL (evidence: `frontend/src/api/client.ts`)
- API client → AI API gateway: sends AI requests
- AI API gateway → OpenAI-compatible API: dispatches protocol
- OpenAI-compatible API → Chat pipeline: uses chat pipeline
- Chat pipeline → Model providers: sends requests
- OpenAI-compatible API → Protocol conversion: converts protocols
- Authentication → Account records: uses account data
- Provider OAuth → Authentication: supports sign-in
- Admin APIs → Account records: manages accounts
- Admin APIs → Subscription records: manages subscriptions
- Payments → Application database: records payments
- Channel monitoring → Application database: stores monitor data
- Image storage → S3-compatible storage: stores images
- Vue application → HTTP client: uses API
- AI API gateway → Account records: selects accounts
- AI API gateway → Subscription records: applies quota
